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() with assert_raises(zerorpc.LostRemote): client_hbchan.recv() print 'CLIENT LOST SERVER :)' client_hbchan.close() server.close() client.close()
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) event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=TIME_FACTOR * 2) gevent.sleep(TIME_FACTOR * 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_close_client_bufchan(): 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) client_bufchan.emit('openthat', None) event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=1) server_bufchan = zerorpc.BufferedChannel(server_hbchan) server_bufchan.recv() gevent.sleep(3) print 'CLOSE CLIENT SOCKET!!!' client_bufchan.close() with assert_raises(zerorpc.LostRemote): server_bufchan.recv() print 'SERVER LOST CLIENT :)' server_bufchan.close() server.close() client.close()
def test_close_client_bufchan(): 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) client_bufchan.emit('openthat', None) event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=TIME_FACTOR * 2) server_bufchan = zerorpc.BufferedChannel(server_hbchan) server_bufchan.recv() gevent.sleep(TIME_FACTOR * 3) print('CLOSE CLIENT SOCKET!!!') client_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('SERVER LOST CLIENT :)') server_bufchan.close() server.close() client.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_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 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, inqueue_size=100) if sys.version_info < (2, 7): def _do_with_assert_raises(): for x in range(200): server_bufchan.emit('coucou', x, timeout=0) # will fail when x == 1 assert_raises(zerorpc.TimeoutExpired, _do_with_assert_raises) else: with assert_raises(zerorpc.TimeoutExpired): for x in range(200): server_bufchan.emit('coucou', x, timeout=0) # will fail when x == 1 server_bufchan.emit('coucou', 1) # block until receiver is ready if sys.version_info < (2, 7): def _do_with_assert_raises(): for x in range(2, 200): server_bufchan.emit('coucou', x, timeout=0) # will fail when x == 100 assert_raises(zerorpc.TimeoutExpired, _do_with_assert_raises) else: with assert_raises(zerorpc.TimeoutExpired): for x in range(2, 200): server_bufchan.emit('coucou', x, timeout=0) # will fail when x == 100 for x in range(read_cnt.value, 200): server_bufchan.emit('coucou', x) # block until receiver is ready server_bufchan.close()
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) if sys.version_info < (2, 7): def _do_with_assert_raises(): for x in range(20): event = server_bufchan.recv() assert event.name == 'sleep' gevent.sleep(TIME_FACTOR * event.args[0]) server_bufchan.emit('OK', event.args) assert_raises(zerorpc.LostRemote, _do_with_assert_raises) else: with assert_raises(zerorpc.LostRemote): for x in range(20): event = server_bufchan.recv() assert event.name == 'sleep' gevent.sleep(TIME_FACTOR * event.args[0]) server_bufchan.emit('OK', event.args) server_bufchan.close()
def server_do(): event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=2) if sys.version_info < (2, 7): def _do_with_assert_raises(): for x in xrange(20): event = server_hbchan.recv() print event assert event.name == 'sleep' gevent.sleep(event.args[0]) server_hbchan.emit('OK', event.args) assert_raises(zerorpc.LostRemote, _do_with_assert_raises) else: with assert_raises(zerorpc.LostRemote): for x in xrange(20): # 服务器接口大量的Event, 但是中途发现: client 关闭, 心跳结束,然后服务器的请求也就终止 event = server_hbchan.recv() print event assert event.name == 'sleep' gevent.sleep(event.args[0]) server_hbchan.emit('OK', event.args) server_hbchan.close()
def server_do(): event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=2) for x in xrange(10): event = server_hbchan.recv() assert event.name == 'add' server_hbchan.emit('OK', (sum(event.args), )) server_hbchan.close()
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 event.args == (x + x * x, ) client_hbchan.close()
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()
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(20): 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()
def client_do(): client_channel = client.channel() client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=TIME_FACTOR * 2) client_bufchan = zerorpc.BufferedChannel(client_hbchan, inqueue_size=100) for x in range(200): event = client_bufchan.recv() assert event.name == 'coucou' assert event.args == x read_cnt.value += 1 client_bufchan.close()
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),)) server_bufchan.close()
def test_do_some_req_rep(): 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(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() client_task = gevent.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() server_task = gevent.spawn(server_do) server_task.get() client_task.get() 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 client_do(): client_channel = client.channel() client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=2) with assert_raises(zerorpc.TimeoutExpired): for x in xrange(10): client_hbchan.emit('sleep', (x, )) event = client_hbchan.recv(timeout=3) assert event.name == 'OK' assert event.args == (x, ) client_hbchan.close()
def server_do(): event = server.recv() server_channel = server.channel(event) server_hbchan = zerorpc.HeartBeatOnChannel(server_channel, freq=2) with assert_raises(zerorpc.LostRemote): for x in xrange(20): event = server_hbchan.recv() assert event.name == 'sleep' gevent.sleep(event.args[0]) server_hbchan.emit('OK', event.args) server_hbchan.close()
def client_do(): print 'running' 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 event.args == (x + x * x, ) client_hbchan.emit('add', (x, x * x)) with assert_raises(zerorpc.LostRemote): event = client_hbchan.recv() client_hbchan.close()
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), )) with assert_raises(zerorpc.LostRemote): event = server_bufchan.recv() server_bufchan.close()
def test_close_server_hbchan(): endpoint = random_ipc_endpoint() # 创建Server server_events = zerorpc.Events(zmq.ROUTER) server_events.bind(endpoint) server = zerorpc.ChannelMultiplexer(server_events) # 创建Client 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 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 client_do(): print 'running' 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.emit('add', (x, x * x)) if sys.version_info < (2, 7): assert_raises(zerorpc.LostRemote, client_hbchan.recv) else: with assert_raises(zerorpc.LostRemote): client_hbchan.recv() client_hbchan.close()
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()
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()
def client_do(): client_channel = client.channel() client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=TIME_FACTOR * 2) client_bufchan = zerorpc.BufferedChannel(client_hbchan) if sys.version_info < (2, 7): def _do_with_assert_raises(): for x in range(10): client_bufchan.emit('sleep', (x,)) event = client_bufchan.recv(timeout=TIME_FACTOR * 3) assert event.name == 'OK' assert list(event.args) == [x] assert_raises(zerorpc.TimeoutExpired, _do_with_assert_raises) else: with assert_raises(zerorpc.TimeoutExpired): for x in range(10): client_bufchan.emit('sleep', (x,)) event = client_bufchan.recv(timeout=TIME_FACTOR * 3) assert event.name == 'OK' assert list(event.args) == [x] client_bufchan.close()
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 client_do(): # client创建一个chanel, 并在上面创建 HeartBeatOnChannel # response_to # Each consecutive event on a channel will have the header field "response_to" set to the channel id: # 第一个Event的Id, 之后所有的Event, 无论是在服务器还是在Client, 都共享相同的数据 # client_channel = client.channel() # 主动心跳? client_hbchan = zerorpc.HeartBeatOnChannel(client_channel, freq=2) if sys.version_info < (2, 7): def _do_with_assert_raises(): for x in xrange(10): # client发送Event到server, 并且等待服务器的返回 client_hbchan.emit('sleep', (x, )) event = client_hbchan.recv( timeout=3 ) # x: 0, 1, 2正常返回, x: 3+ 返回异常, TimeoutExpired # 心跳的作用? print event assert event.name == 'OK' assert list(event.args) == [x] assert_raises(zerorpc.TimeoutExpired, _do_with_assert_raises) else: with assert_raises(zerorpc.TimeoutExpired): for x in xrange(10): client_hbchan.emit('sleep', (x, )) event = client_hbchan.recv(timeout=3) print event assert event.name == 'OK' assert list(event.args) == [x] client_hbchan.close()