本文整理汇总了Python中testutils.random_ipc_endpoint函数的典型用法代码示例。如果您正苦于以下问题:Python random_ipc_endpoint函数的具体用法?Python random_ipc_endpoint怎么用?Python random_ipc_endpoint使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了random_ipc_endpoint函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_task_context_relay_fork
def test_task_context_relay_fork():
endpoint1 = random_ipc_endpoint()
endpoint2 = random_ipc_endpoint()
srv_ctx = zerorpc.Context()
srv_relay_ctx = zerorpc.Context()
cli_ctx = zerorpc.Context()
srv_tracer = Tracer("[server]")
srv_ctx.register_middleware(srv_tracer)
srv_relay_tracer = Tracer("[server_relay]")
srv_relay_ctx.register_middleware(srv_relay_tracer)
cli_tracer = Tracer("[client]")
cli_ctx.register_middleware(cli_tracer)
class Srv:
def echo(self, msg):
return msg
srv = zerorpc.Server(Srv(), context=srv_ctx)
srv.bind(endpoint1)
srv_task = gevent.spawn(srv.run)
c_relay = zerorpc.Client(context=srv_relay_ctx)
c_relay.connect(endpoint1)
class SrvRelay:
def echo(self, msg):
def dothework(msg):
return c_relay.echo(msg) + "relayed"
g = gevent.spawn(zerorpc.fork_task_context(dothework, srv_relay_ctx), "relay" + msg)
print "relaying in separate task:", g
r = g.get()
print "back to main task"
return r
srv_relay = zerorpc.Server(SrvRelay(), context=srv_relay_ctx)
srv_relay.bind(endpoint2)
srv_relay_task = gevent.spawn(srv_relay.run)
c = zerorpc.Client(context=cli_ctx)
c.connect(endpoint2)
assert c.echo("hello") == "relayhellorelayed"
srv_relay.stop()
srv.stop()
srv_relay_task.join()
srv_task.join()
assert cli_tracer._log == [("new", cli_tracer.trace_id)]
assert srv_relay_tracer._log == [
("load", cli_tracer.trace_id),
("reuse", cli_tracer.trace_id),
("load", cli_tracer.trace_id),
("reuse", cli_tracer.trace_id),
("reuse", cli_tracer.trace_id),
]
assert srv_tracer._log == [("load", cli_tracer.trace_id), ("reuse", cli_tracer.trace_id)]
示例2: test_task_context_relay
def test_task_context_relay():
endpoint1 = random_ipc_endpoint()
endpoint2 = random_ipc_endpoint()
srv_ctx = zerorpc.Context()
srv_relay_ctx = zerorpc.Context()
cli_ctx = zerorpc.Context()
srv_tracer = Tracer('[server]')
srv_ctx.register_middleware(srv_tracer)
srv_relay_tracer = Tracer('[server_relay]')
srv_relay_ctx.register_middleware(srv_relay_tracer)
cli_tracer = Tracer('[client]')
cli_ctx.register_middleware(cli_tracer)
class Srv:
def echo(self, msg):
return msg
srv = zerorpc.Server(Srv(), context=srv_ctx)
srv.bind(endpoint1)
srv_task = gevent.spawn(srv.run)
c_relay = zerorpc.Client(context=srv_relay_ctx)
c_relay.connect(endpoint1)
class SrvRelay:
def echo(self, msg):
return c_relay.echo('relay' + msg) + 'relayed'
srv_relay = zerorpc.Server(SrvRelay(), context=srv_relay_ctx)
srv_relay.bind(endpoint2)
srv_relay_task = gevent.spawn(srv_relay.run)
c = zerorpc.Client(context=cli_ctx)
c.connect(endpoint2)
assert c.echo('hello') == 'relayhellorelayed'
srv_relay.stop()
srv.stop()
srv_relay_task.join()
srv_task.join()
assert cli_tracer._log == [
('new', cli_tracer.trace_id),
]
assert srv_relay_tracer._log == [
('load', cli_tracer.trace_id),
('reuse', cli_tracer.trace_id),
('reuse', cli_tracer.trace_id),
]
assert srv_tracer._log == [
('load', cli_tracer.trace_id),
('reuse', cli_tracer.trace_id),
]
示例3: test_config_multiple_endpoints
def test_config_multiple_endpoints():
app = Zask(__name__)
endpoint = random_ipc_endpoint()
another_endpoint = random_ipc_endpoint()
app.config['ZERORPC_SOME_SERVICE'] = {
'1.0': endpoint,
}
app.config['ZERORPC_SOME_SERVICE_2'] = {
'1.0': another_endpoint
}
app.config['ZERORPC_SOME_SERVICE_CLIENT'] = {
'1.0': [
endpoint,
another_endpoint
],
'default': '1.0'
}
rpc = ZeroRPC(app, middlewares=[CONFIG_ENDPOINT_MIDDLEWARE])
class Srv(object):
__version__ = "1.0"
__service_name__ = "some_service"
def hello(self):
return 'i am server 1'
class AnotherSrv(object):
__version__ = "1.0"
__service_name__ = "some_service_2"
def hello(self):
return 'i am server 2'
srv = rpc.Server(Srv())
another_srv = rpc.Server(AnotherSrv())
gevent.spawn(srv.run)
gevent.spawn(another_srv.run)
client = rpc.Client('some_service_client')
for i in range(5):
who_i_am = client.hello()
app.logger.debug(who_i_am)
assert who_i_am == 'i am server 1' or who_i_am == 'i am server 2'
with pytest.raises(MissingConfigException):
client = rpc.Client('some_service_client', version='2.0')
client.close()
srv.close()
another_srv.close()
示例4: test_heartbeat_can_open_channel_server_close
def test_heartbeat_can_open_channel_server_close():
endpoint = random_ipc_endpoint()
server_events = zerorpc.Events(zmq.ROUTER)
server_events.bind(endpoint)
server = zerorpc.ChannelMultiplexer(server_events)
client_events = zerorpc.Events(zmq.DEALER)
client_events.connect(endpoint)
client = zerorpc.ChannelMultiplexer(client_events, ignore_broadcast=True)
client_channel = client.channel()
client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=TIME_FACTOR * 2)
client_bufchan = zerorpc.BufferedChannel(client_hbchan)
event = server.recv()
server_channel = server.channel(event)
server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=TIME_FACTOR * 2)
server_bufchan = zerorpc.BufferedChannel(server_hbchan)
gevent.sleep(TIME_FACTOR * 3)
print 'CLOSE SERVER SOCKET!!!'
server_bufchan.close()
if sys.version_info < (2, 7):
assert_raises(zerorpc.LostRemote, client_bufchan.recv)
else:
with assert_raises(zerorpc.LostRemote):
client_bufchan.recv()
print 'CLIENT LOST SERVER :)'
client_bufchan.close()
server.close()
client.close()
示例5: test_events_channel_both_side
def test_events_channel_both_side():
endpoint = random_ipc_endpoint()
server_events = zerorpc.Events(zmq.XREP)
server_events.bind(endpoint)
server = zerorpc.ChannelMultiplexer(server_events)
client_events = zerorpc.Events(zmq.XREQ)
client_events.connect(endpoint)
client = zerorpc.ChannelMultiplexer(client_events)
client_channel = client.channel()
client_channel.emit("openthat", (42,))
event = server.recv()
print event
assert event.args == (42,)
assert event.name == "openthat"
server_channel = server.channel(event)
server_channel.emit("test", (21,))
event = client_channel.recv()
assert event.args == (21,)
assert event.name == "test"
server_channel.emit("test", (22,))
event = client_channel.recv()
assert event.args == (22,)
assert event.name == "test"
server_events.close()
server_channel.close()
client_channel.close()
client_events.close()
示例6: test1
def test1():
endpoint = random_ipc_endpoint()
def server():
c = zmq.Context()
s = c.socket(zmq.REP)
s.bind(endpoint)
while True:
print 'srv recving...'
r = s.recv()
print 'srv', r
print 'srv sending...'
s.send('world')
s.close()
c.term()
def client():
c = zmq.Context()
s = c.socket(zmq.REQ)
s.connect(endpoint)
print 'cli sending...'
s.send('hello')
print 'cli recving...'
r = s.recv()
print 'cli', r
s.close()
c.term()
s = gevent.spawn(server)
c = gevent.spawn(client)
c.join()
示例7: test_close_client_hbchan
def test_close_client_hbchan():
endpoint = random_ipc_endpoint()
server_events = zerorpc.Events(zmq.ROUTER)
server_events.bind(endpoint)
server = zerorpc.ChannelMultiplexer(server_events)
client_events = zerorpc.Events(zmq.DEALER)
client_events.connect(endpoint)
client = zerorpc.ChannelMultiplexer(client_events, ignore_broadcast=True)
client_channel = client.channel()
client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=2)
client_hbchan.emit('openthat', None)
event = server.recv()
server_channel = server.channel(event)
server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=2)
server_hbchan.recv()
gevent.sleep(3)
print 'CLOSE CLIENT SOCKET!!!'
client_hbchan.close()
if sys.version_info < (2, 7):
assert_raises(zerorpc.LostRemote, server_hbchan.recv)
else:
with assert_raises(zerorpc.LostRemote):
server_hbchan.recv()
print 'SERVER LOST CLIENT :)'
server_hbchan.close()
server.close()
client.close()
示例8: test_hook_client_before_request
def test_hook_client_before_request():
class ClientBeforeRequestMiddleware(object):
def __init__(self):
self.called = False
def client_before_request(self, event):
self.called = True
self.method = event.name
zero_ctx = zerorpc.Context()
endpoint = random_ipc_endpoint()
test_server = zerorpc.Server(EchoModule(), context=zero_ctx)
test_server.bind(endpoint)
test_server_task = gevent.spawn(test_server.run)
test_client = zerorpc.Client(context=zero_ctx)
test_client.connect(endpoint)
assert test_client.echo("test") == "echo: test"
test_middleware = ClientBeforeRequestMiddleware()
zero_ctx.register_middleware(test_middleware)
assert test_middleware.called == False
assert test_client.echo("test") == "echo: test"
assert test_middleware.called == True
assert test_middleware.method == 'echo'
test_server.stop()
test_server_task.join()
示例9: test_events_channel_client_side_server_send_many
def test_events_channel_client_side_server_send_many():
endpoint = random_ipc_endpoint()
server_events = zerorpc.Events(zmq.ROUTER)
server_events.bind(endpoint)
server = zerorpc.ChannelMultiplexer(server_events)
client_events = zerorpc.Events(zmq.DEALER)
client_events.connect(endpoint)
client = zerorpc.ChannelMultiplexer(client_events)
client_channel = client.channel()
client_channel.emit('giveme', (10,))
event = server.recv()
print event
assert list(event.args) == [10]
assert event.header.get('zmqid', None) is not None
for x in xrange(10):
server.emit('someanswer', (x,),
xheader=dict(response_to=event.header['message_id'],
zmqid=event.header['zmqid']))
for x in xrange(10):
event = client_channel.recv()
assert list(event.args) == [x]
示例10: test_server_manual
def test_server_manual():
endpoint = random_ipc_endpoint()
class MySrv(zerorpc.Server):
def lolita(self):
return 42
def add(self, a, b):
return a + b
srv = MySrv()
srv.bind(endpoint)
gevent.spawn(srv.run)
client_events = zerorpc.Events(zmq.XREQ)
client_events.connect(endpoint)
client = zerorpc.ChannelMultiplexer(client_events, ignore_broadcast=True)
client_channel = client.channel()
client_channel.emit('lolita', tuple())
event = client_channel.recv()
assert event.args == (42,)
client_channel.close()
client_channel = client.channel()
client_channel.emit('add', (1, 2))
event = client_channel.recv()
assert event.args == (3,)
client_channel.close()
srv.stop()
示例11: test_client_server_exception
def test_client_server_exception():
endpoint = random_ipc_endpoint()
class MySrv(zerorpc.Server):
def raise_something(self, a):
return a[4]
srv = MySrv()
srv.bind(endpoint)
gevent.spawn(srv.run)
client = zerorpc.Client(timeout=2)
client.connect(endpoint)
if sys.version_info < (2, 7):
def _do_with_assert_raises():
print client.raise_something(42)
assert_raises(zerorpc.RemoteError, _do_with_assert_raises)
else:
with assert_raises(zerorpc.RemoteError):
print client.raise_something(42)
assert client.raise_something(range(5)) == 4
client.close()
srv.close()
示例12: test_sub_events
def test_sub_events():
endpoint = random_ipc_endpoint()
server_events = zerorpc.Events(zmq.ROUTER)
server_events.bind(endpoint)
server = zerorpc.ChannelMultiplexer(server_events)
client_events = zerorpc.Events(zmq.DEALER)
client_events.connect(endpoint)
client = zerorpc.ChannelMultiplexer(client_events, ignore_broadcast=True)
client_channel = client.channel()
client_channel_events = zerorpc.WrappedEvents(client_channel)
client_channel_events.emit("coucou", 42)
event = server.recv()
print event
assert type(event.args) is tuple
assert event.name == "w"
subevent = event.args
print "subevent:", subevent
server_channel = server.channel(event)
server_channel_events = zerorpc.WrappedEvents(server_channel)
server_channel_channel = zerorpc.ChannelMultiplexer(server_channel_events)
event = server_channel_channel.recv()
print event
assert event.name == "coucou"
assert event.args == 42
server_events.close()
client_events.close()
示例13: test_hook_server_before_exec_puller
def test_hook_server_before_exec_puller():
zero_ctx = zerorpc.Context()
trigger = gevent.event.Event()
endpoint = random_ipc_endpoint()
echo_module = EchoModule(trigger)
test_server = zerorpc.Puller(echo_module, context=zero_ctx)
test_server.bind(endpoint)
test_server_task = gevent.spawn(test_server.run)
test_client = zerorpc.Pusher()
test_client.connect(endpoint)
# Test without a middleware
test_client.echo("test")
trigger.wait(timeout=TIME_FACTOR * 2)
assert echo_module.last_msg == "echo: test"
trigger.clear()
# Test with a middleware
test_middleware = ServerBeforeExecMiddleware()
zero_ctx.register_middleware(test_middleware)
assert test_middleware.called == False
test_client.echo("test with a middleware")
trigger.wait(timeout=TIME_FACTOR * 2)
assert echo_module.last_msg == "echo: test with a middleware"
assert test_middleware.called == True
test_server.stop()
test_server_task.join()
示例14: test_hook_server_after_exec_on_error_puller
def test_hook_server_after_exec_on_error_puller():
zero_ctx = zerorpc.Context()
trigger = gevent.event.Event()
endpoint = random_ipc_endpoint()
echo_module = BrokenEchoModule(trigger)
test_server = zerorpc.Puller(echo_module, context=zero_ctx)
test_server.bind(endpoint)
test_server_task = gevent.spawn(test_server.run)
test_client = zerorpc.Pusher()
test_client.connect(endpoint)
test_middleware = ServerAfterExecMiddleware()
zero_ctx.register_middleware(test_middleware)
assert test_middleware.called == False
try:
test_client.echo("test with a middleware")
trigger.wait(timeout=TIME_FACTOR * 2)
except zerorpc.RemoteError:
pass
assert echo_module.last_msg == "Raise"
assert test_middleware.called == False
test_server.stop()
test_server_task.join()
示例15: test_hook_server_after_exec_stream
def test_hook_server_after_exec_stream():
zero_ctx = zerorpc.Context()
endpoint = random_ipc_endpoint()
test_server = zerorpc.Server(EchoModule(), context=zero_ctx)
test_server.bind(endpoint)
test_server_task = gevent.spawn(test_server.run)
test_client = zerorpc.Client()
test_client.connect(endpoint)
# Test without a middleware
for echo in test_client.echoes("test"):
assert echo == "echo: test"
# Test with a middleware
test_middleware = ServerAfterExecMiddleware()
zero_ctx.register_middleware(test_middleware)
assert test_middleware.called == False
it = test_client.echoes("test")
assert next(it) == "echo: test"
assert test_middleware.called == False
for echo in it:
assert echo == "echo: test"
assert test_middleware.called == True
assert test_middleware.request_event_name == 'echoes'
assert test_middleware.reply_event_name == 'STREAM_DONE'
test_server.stop()
test_server_task.join()