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.identity is not None for x in range(10): reply_event = server.new_event('someanswer', (x,), xheader={'response_to': event.header['message_id']}) reply_event.identity = event.identity server.emit_event(reply_event) for x in range(10): event = client_channel.recv() assert list(event.args) == [x]
def test_events_channel_both_side(): 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('openthat', (42,)) event = server.recv() print(event) assert list(event.args) == [42] assert event.name == 'openthat' server_channel = server.channel(event) server_channel.emit('test', (21,)) event = client_channel.recv() assert list(event.args) == [21] assert event.name == 'test' server_channel.emit('test', (22,)) event = client_channel.recv() assert list(event.args) == [22] assert event.name == 'test' server_events.close() server_channel.close() client_channel.close() client_events.close()
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()
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 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 event.args == (x, )
def test_heartbeat_can_open_channel_client_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) # channel的获取: # 主动创建一个Channel, 并且用该Channel发送Event # 被动获取一个Channel, 从已有的Event获取Channel client_channel = client.channel() client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=2) event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=2) gevent.sleep(3) print 'CLOSE CLIENT SOCKET!!!' client_hbchan.close() client.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()
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=2) event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=2) gevent.sleep(3) print 'CLOSE SERVER SOCKET!!!' server_hbchan.close() # 客户端失去心跳 if sys.version_info < (2, 7): assert_raises(zerorpc.LostRemote, client_hbchan.recv) else: with assert_raises(zerorpc.LostRemote): client_hbchan.recv() print 'CLIENT LOST SERVER :)' client_hbchan.close() server.close() client.close()
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 isinstance(event.args, (list, 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()
def test_multiple_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_channel1 = client.channel() client_channel_events1 = zerorpc.WrappedEvents(client_channel1) client_channel2 = zerorpc.BufferedChannel(client.channel()) client_channel_events2 = zerorpc.WrappedEvents(client_channel2) def emitstuff(): client_channel_events1.emit('coucou1', 43) client_channel_events2.emit('coucou2', 44) client_channel_events2.emit('another', 42) gevent.spawn(emitstuff) event = server.recv() print event assert isinstance(event.args, (list, tuple)) assert event.name == 'w' subevent = event.args print 'subevent:', subevent server_channel = server.channel(event) server_channel_events = zerorpc.WrappedEvents(server_channel) event = server_channel_events.recv() print 'ch1:', event assert event.name == 'coucou1' assert event.args == 43 event = server.recv() print event assert isinstance(event.args, (list, tuple)) assert event.name == 'w' subevent = event.args print 'subevent:', subevent server_channel = server.channel(event) server_channel_events = zerorpc.BufferedChannel(server_channel) server_channel_events = zerorpc.WrappedEvents(server_channel_events) event = server_channel_events.recv() print 'ch2:', event assert event.name == 'coucou2' assert event.args == 44 event = server_channel_events.recv() print 'ch2:', event assert event.name == 'another' assert event.args == 42 server_events.close() client_events.close()
def test_on_close_if(): """ Test that the on_close_if method does not cause exceptions when the client is slow to recv() data. """ 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_bufchan = zerorpc.BufferedChannel(client_hbchan, inqueue_size=10) event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=2) server_bufchan = zerorpc.BufferedChannel(server_hbchan, inqueue_size=10) seen = [] def is_stream_done(event): return event.name == 'done' def client_do(): while True: event = client_bufchan.recv() if event.name == 'done': return seen.append(event.args) gevent.sleep(0.1) def server_do(): for i in range(0, 10): server_bufchan.emit('blah', (i)) server_bufchan.emit('done', ('bye')) client_bufchan.on_close_if = is_stream_done coro_pool = gevent.pool.Pool() g1 = coro_pool.spawn(client_do) g2 = coro_pool.spawn(server_do) g1.get() # Re-raise any exceptions... g2.get() assert seen == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] client_bufchan.close() server_bufchan.close() client.close() server.close()
def test_events_req_rep(): server = zerorpc.Events(zmq.REP) server.bind(endpoint) client = zerorpc.Events(zmq.REQ) client.connect(endpoint) client.emit('myevent', ('arg1', )) event = server.recv() print(event) assert event.name == 'myevent' assert list(event.args) == ['arg1']
def test_multiple_sub_events(): 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, ignore_broadcast=True) client_channel1 = client.channel() client_channel_events1 = zerorpc.WrappedEvents(client_channel1) client_channel2 = client.channel() client_channel_events2 = zerorpc.WrappedEvents(client_channel2) client_channel_events1.emit('coucou1', 43) client_channel_events2.emit('coucou2', 44) client_channel_events2.emit('another', 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) event = server_channel_events.recv() print event assert event.name == 'coucou1' assert event.args == 43 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) event = server_channel_events.recv() print event assert event.name == 'coucou2' assert event.args == 44 event = server_channel_events.recv() print event assert event.name == 'another' assert event.args == 42 server_events.close() client_events.close()
def test_events_req_rep(): endpoint = random_ipc_endpoint() server = zerorpc.Events(zmq.REP) server.bind(endpoint) client = zerorpc.Events(zmq.REQ) client.connect(endpoint) client.emit('myevent', ('arg1',)) event = server.recv() print event assert event.name == 'myevent' assert event.args == ('arg1',)
def test_do_some_req_rep_lost_client(): 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) def client_do(): client_channel = client.channel() client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=TIME_FACTOR * 2) client_bufchan = zerorpc.BufferedChannel(client_hbchan) for x in range(10): client_bufchan.emit('add', (x, x * x)) event = client_bufchan.recv() assert event.name == 'OK' assert list(event.args) == [x + x * x] client_bufchan.close() coro_pool = gevent.pool.Pool() coro_pool.spawn(client_do) def server_do(): event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=TIME_FACTOR * 2) server_bufchan = zerorpc.BufferedChannel(server_hbchan) for x in range(10): event = server_bufchan.recv() assert event.name == 'add' server_bufchan.emit('OK', (sum(event.args), )) if sys.version_info < (2, 7): assert_raises(zerorpc.LostRemote, server_bufchan.recv) else: with assert_raises(zerorpc.LostRemote): server_bufchan.recv() server_bufchan.close() coro_pool.spawn(server_do) coro_pool.join() client.close() server.close()
def test_congestion_control_server_pushing(): 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, ignore_broadcast=True) client_channel = client.channel() client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=1) client_bufchan = zerorpc.BufferedChannel(client_hbchan) event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=1) server_bufchan = zerorpc.BufferedChannel(server_hbchan) def client_do(): for x in xrange(200): event = client_bufchan.recv() assert event.name == 'coucou' assert event.args == x client_task = gevent.spawn(client_do) def server_do(): with assert_raises(CongestionError): for x in xrange(200): if server_bufchan.emit('coucou', x, block=False) == False: raise CongestionError() # will fail when x == 1 server_bufchan.emit('coucou', 1) # block until receiver is ready with assert_raises(CongestionError): for x in xrange(2, 200): if server_bufchan.emit('coucou', x, block=False) == False: raise CongestionError() # will fail when x == 100 for x in xrange(101, 200): server_bufchan.emit('coucou', x) # block until receiver is ready server_task = gevent.spawn(server_do) server_task.get() client_task.get() client_bufchan.close() client.close() server_task.get() server_bufchan.close() server.close()
def test_do_some_req_rep_lost_client(): 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) def client_do(): client_channel = client.channel() client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=2) for x in xrange(10): client_hbchan.emit('add', (x, x * x)) # 等待事件的发生 event = client_hbchan.recv() assert event.name == 'OK' assert list(event.args) == [x + x * x] client_hbchan.close() client_task = gevent.spawn(client_do) def server_do(): event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=2) for x in xrange(10): # 直接测试通信的协议(不涉及RPC) event = server_hbchan.recv() assert event.name == 'add' server_hbchan.emit('OK', (sum(event.args), )) if sys.version_info < (2, 7): assert_raises(zerorpc.LostRemote, server_hbchan.recv) else: with assert_raises(zerorpc.LostRemote): server_hbchan.recv() server_hbchan.close() server_task = gevent.spawn(server_do) # 等待两个事件的返回 server_task.get() client_task.get() client.close() server.close()
def test_events_push_pull(): server = zerorpc.Events(zmq.PULL) server.bind(endpoint) client = zerorpc.Events(zmq.PUSH) client.connect(endpoint) for x in range(10): client.emit('myevent', (x, )) for x in range(10): event = server.recv() print(event) assert event.name == 'myevent' assert list(event.args) == [x]
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()
def test_exception_compat_v1(): endpoint = random_ipc_endpoint() class MySrv(zerorpc.Server): pass 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) rpccall = client.channel() rpccall.emit('donotexist', tuple()) event = rpccall.recv() print event assert event.name == 'ERR' (name, msg, tb) = event.args print 'detailed error', name, msg, tb assert name == 'NameError' assert msg == 'donotexist' rpccall = client.channel() rpccall.emit('donotexist', tuple(), xheader=dict(v=1)) event = rpccall.recv() print event assert event.name == 'ERR' (msg, ) = event.args print 'msg only', msg assert msg == "NameError('donotexist',)" client_events.close() srv.close()
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 # 运行一个Server srv = MySrv() srv.bind(endpoint) gevent.spawn(srv.run) client_events = zerorpc.Events(zmq.DEALER) client_events.connect(endpoint) client = zerorpc.ChannelMultiplexer(client_events, ignore_broadcast=True) # 通过一个Channel来"手动"发送信息 client_channel = client.channel() client_channel.emit('lolita', tuple()) event = client_channel.recv() assert list(event.args) == [42] client_channel.close() client_channel = client.channel() client_channel.emit('add', (1, 2)) event = client_channel.recv() assert list(event.args) == [3] client_channel.close() srv.stop()
def test_events_push_pull(): endpoint = random_ipc_endpoint() server = zerorpc.Events(zmq.PULL) server.bind(endpoint) client = zerorpc.Events(zmq.PUSH) client.connect(endpoint) for x in xrange(10): client.emit('myevent', (x,)) for x in xrange(10): event = server.recv() print event assert event.name == 'myevent' assert event.args == (x,)
def test_do_some_req_rep_client_timeout(): 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, ignore_broadcast=True) def client_do(): client_channel = client.channel() client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=1) client_bufchan = zerorpc.BufferedChannel(client_hbchan) with assert_raises(zerorpc.TimeoutExpired): for x in xrange(10): client_bufchan.emit('sleep', (x, )) event = client_bufchan.recv(timeout=3) assert event.name == 'OK' assert event.args == (x, ) client_bufchan.close() client_task = gevent.spawn(client_do) def server_do(): event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=1) server_bufchan = zerorpc.BufferedChannel(server_hbchan) with assert_raises(zerorpc.LostRemote): for x in xrange(20): event = server_bufchan.recv() assert event.name == 'sleep' gevent.sleep(event.args[0]) server_bufchan.emit('OK', event.args) server_bufchan.close() server_task = gevent.spawn(server_do) server_task.get() client_task.get() client.close() server.close()
def test_do_some_req_rep_lost_server(): 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, ignore_broadcast=True) def client_do(): print 'running' client_channel = client.channel() client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=1) client_bufchan = zerorpc.BufferedChannel(client_hbchan) for x in xrange(10): client_bufchan.emit('add', (x, x * x)) event = client_bufchan.recv() assert event.name == 'OK' assert event.args == (x + x * x, ) client_bufchan.emit('add', (x, x * x)) with assert_raises(zerorpc.LostRemote): event = client_bufchan.recv() client_bufchan.close() client_task = gevent.spawn(client_do) def server_do(): event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=1) server_bufchan = zerorpc.BufferedChannel(server_hbchan) for x in xrange(10): event = server_bufchan.recv() assert event.name == 'add' server_bufchan.emit('OK', (sum(event.args), )) server_bufchan.close() server_task = gevent.spawn(server_do) server_task.get() client_task.get() client.close() server.close()
def test_events_req_rep2(): server = zerorpc.Events(zmq.REP) server.bind(endpoint) client = zerorpc.Events(zmq.REQ) client.connect(endpoint) for i in range(10): client.emit('myevent' + str(i), (i, )) event = server.recv() print(event) assert event.name == 'myevent' + str(i) assert list(event.args) == [i] server.emit('answser' + str(i * 2), (i * 2, )) event = client.recv() print(event) assert event.name == 'answser' + str(i * 2) assert list(event.args) == [i * 2]
def test_do_some_req_rep(): 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_bufchan = zerorpc.BufferedChannel(client_hbchan) event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=2) server_bufchan = zerorpc.BufferedChannel(server_hbchan) def client_do(): for x in xrange(20): client_bufchan.emit('add', (x, x * x)) event = client_bufchan.recv() assert event.name == 'OK' assert event.args == (x + x * x, ) client_bufchan.close() coro_pool = gevent.pool.Pool() coro_pool.spawn(client_do) def server_do(): for x in xrange(20): event = server_bufchan.recv() assert event.name == 'add' server_bufchan.emit('OK', (sum(event.args), )) server_bufchan.close() coro_pool.spawn(server_do) coro_pool.join() client.close() server.close()
def test_do_some_req_rep(): 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 * 4) event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=TIME_FACTOR * 4) def client_do(): for x in range(20): client_hbchan.emit('add', (x, x * x)) event = client_hbchan.recv() assert event.name == 'OK' assert list(event.args) == [x + x * x] client_hbchan.close() client_task = gevent.spawn(client_do) def server_do(): for x in range(20): event = server_hbchan.recv() assert event.name == 'add' server_hbchan.emit('OK', (sum(event.args), )) server_hbchan.close() server_task = gevent.spawn(server_do) server_task.get() client_task.get() client.close() server.close()
def test_events_req_rep2(): endpoint = random_ipc_endpoint() server = zerorpc.Events(zmq.REP) server.bind(endpoint) client = zerorpc.Events(zmq.REQ) client.connect(endpoint) for i in xrange(10): client.emit('myevent' + str(i), (i,)) event = server.recv() print event assert event.name == 'myevent' + str(i) assert event.args == (i,) server.emit('answser' + str(i * 2), (i * 2,)) event = client.recv() print event assert event.name == 'answser' + str(i * 2) assert event.args == (i * 2,)
def test_events_dealer_router(): server = zerorpc.Events(zmq.ROUTER) server.bind(endpoint) client = zerorpc.Events(zmq.DEALER) client.connect(endpoint) for i in range(6): client.emit('myevent' + str(i), (i, )) event = server.recv() print(event) assert event.name == 'myevent' + str(i) assert list(event.args) == [i] reply_event = server.new_event('answser' + str(i * 2), (i * 2, )) reply_event.identity = event.identity server.emit_event(reply_event) event = client.recv() print(event) assert event.name == 'answser' + str(i * 2) assert list(event.args) == [i * 2]
def test_events_dealer_router(): endpoint = random_ipc_endpoint() server = zerorpc.Events(zmq.ROUTER) server.bind(endpoint) client = zerorpc.Events(zmq.DEALER) client.connect(endpoint) for i in xrange(6): client.emit('myevent' + str(i), (i,)) event = server.recv() print event assert event.name == 'myevent' + str(i) assert event.args == (i,) server.emit('answser' + str(i * 2), (i * 2,), xheader=dict(zmqid=event.header['zmqid'])) event = client.recv() print event assert event.name == 'answser' + str(i * 2) assert event.args == (i * 2,)
def test_heartbeat_can_open_channel_client_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) def server_fn(): event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=TIME_FACTOR * 2) server_bufchan = zerorpc.BufferedChannel(server_hbchan) try: while True: gevent.sleep(1) finally: server_bufchan.close() server_coro = gevent.spawn(server_fn) gevent.sleep(TIME_FACTOR * 3) print('CLOSE CLIENT SOCKET!!!') client_bufchan.close() client.close() if sys.version_info < (2, 7): assert_raises(zerorpc.LostRemote, server_coro.get) else: with assert_raises(zerorpc.LostRemote): server_coro.get() print('SERVER LOST CLIENT :)') server.close()
def test_events_channel_client_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('someevent', (42,)) event = server.recv() print event assert event.args == (42,) assert event.header.get('zmqid', None) is not None server.emit('someanswer', (21,), xheader=dict(response_to=event.header['message_id'], zmqid=event.header['zmqid'])) event = client_channel.recv() assert event.args == (21,)