Skip to content

Commit 6174228

Browse files
committed
Add a connect_to parameter to zerorpc.Client
For the most common case: c = zerorpc.Client() c.connect('tcp://blabla:42') It's now possible to use a shorter form: c = zerorpc.Client('tcp://blabla:42')
1 parent af9c8b0 commit 6174228

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

tests/test_client.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# -*- coding: utf-8 -*-
2+
# Open Source Initiative OSI - The MIT License (MIT):Licensing
3+
#
4+
# The MIT License (MIT)
5+
# Copyright (c) 2012 DotCloud Inc ([email protected])
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
8+
# this software and associated documentation files (the "Software"), to deal in
9+
# the Software without restriction, including without limitation the rights to
10+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11+
# of the Software, and to permit persons to whom the Software is furnished to do
12+
# so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
25+
26+
import gevent
27+
28+
import zerorpc
29+
from testutils import teardown, random_ipc_endpoint
30+
31+
def test_client_connect():
32+
endpoint = random_ipc_endpoint()
33+
34+
class MySrv(zerorpc.Server):
35+
36+
def lolita(self):
37+
return 42
38+
39+
srv = MySrv()
40+
srv.bind(endpoint)
41+
gevent.spawn(srv.run)
42+
43+
client = zerorpc.Client()
44+
client.connect(endpoint)
45+
46+
assert client.lolita() == 42
47+
48+
def test_client_quick_connect():
49+
endpoint = random_ipc_endpoint()
50+
51+
class MySrv(zerorpc.Server):
52+
53+
def lolita(self):
54+
return 42
55+
56+
srv = MySrv()
57+
srv.bind(endpoint)
58+
gevent.spawn(srv.run)
59+
60+
client = zerorpc.Client(endpoint)
61+
62+
assert client.lolita() == 42

zerorpc/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,13 @@ def close(self):
345345
class Client(SocketBase, ClientBase):
346346
patterns = [PatternReqStream(), PatternReqRep()]
347347

348-
def __init__(self, context=None, timeout=30, heartbeat=5,
348+
def __init__(self, connect_to=None, context=None, timeout=30, heartbeat=5,
349349
passive_heartbeat=False):
350350
SocketBase.__init__(self, zmq.XREQ, context=context)
351351
ClientBase.__init__(self, self._events, Client.patterns, context,
352352
timeout, heartbeat, passive_heartbeat)
353+
if connect_to:
354+
self.connect(connect_to)
353355

354356
def close(self):
355357
ClientBase.close(self)

0 commit comments

Comments
 (0)