def test_binds(self): port1 = find_unused_port() port2 = find_unused_port() addr1 = 'tcp://127.0.0.1:{}'.format(port1) addr2 = 'tcp://127.0.0.1:{}'.format(port2) @asyncio.coroutine def connect(): tr, pr = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, bind=[addr1, addr2]) yield from pr.connected self.assertEqual({addr1, addr2}, tr.bindings()) addr3 = yield from tr.bind('tcp://*:*') self.assertEqual({addr1, addr2, addr3}, tr.bindings()) yield from tr.unbind(addr2) self.assertEqual({addr1, addr3}, tr.bindings()) self.assertIn(addr1, tr.bindings()) self.assertRegex(repr(tr.bindings()), r'{tcp://0.0.0.0:.\d+, tcp://127.0.0.1:\d+}') tr.close() self.loop.run_until_complete(connect())
def test_connects(self): port1 = find_unused_port() port2 = find_unused_port() port3 = find_unused_port() addr1 = 'tcp://127.0.0.1:{}'.format(port1) addr2 = 'tcp://127.0.0.1:{}'.format(port2) addr3 = 'tcp://127.0.0.1:{}'.format(port3) @asyncio.coroutine def go(): tr, pr = yield from aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, connect=[addr1, addr2], loop=self.loop) yield from pr.connected self.assertEqual({addr1, addr2}, tr.connections()) yield from tr.connect(addr3) self.assertEqual({addr1, addr3, addr2}, tr.connections()) yield from tr.disconnect(addr1) self.assertEqual({addr2, addr3}, tr.connections()) tr.close() self.loop.run_until_complete(go())
def test_resume_paused_with_drain(self): port = find_unused_port() @asyncio.coroutine def go(): s1 = yield from aiozmq.create_zmq_stream( zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) self.assertFalse(s1._paused) s1._protocol.pause_writing() @asyncio.coroutine def f(): yield from s1.drain() fut = asyncio.async(f(), loop=self.loop) yield from asyncio.sleep(0.01, loop=self.loop) self.assertTrue(s1._protocol._paused) s1._protocol.resume_writing() self.assertFalse(s1._protocol._paused) yield from fut s1.close() self.loop.run_until_complete(go())
def go(): port = find_unused_port() server = yield from aiozmq.rpc.serve_rpc( MyHandler(self.loop), bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) tr, pr = yield from create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, connect='tcp://127.0.0.1:{}'.format(port), loop=self.loop) with log_hook('aiozmq.rpc', self.err_queue): tr.write([struct.pack('=HHLd', 1, 2, 3, 4), msgpack.packb((1, 2)), b'bad_kwargs']) ret = yield from self.err_queue.get() self.assertEqual(logging.CRITICAL, ret.levelno) self.assertEqual("Cannot unpack %r", ret.msg) self.assertEqual( ([mock.ANY, mock.ANY, mock.ANY, b'bad_kwargs'],), ret.args) self.assertIsNotNone(ret.exc_info) self.assertTrue(pr.received.empty()) server.close()
def test_feed_cancelled_msg(self): port = find_unused_port() @asyncio.coroutine def go(): s1 = yield from aiozmq.create_zmq_stream( zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) def f(): yield from s1.read() t1 = asyncio.async(f(), loop=self.loop) # to run f() up to yield from yield from asyncio.sleep(0.001, loop=self.loop) t1.cancel() s1.feed_msg([b'data']) yield from asyncio.sleep(0.001, loop=self.loop) with self.assertRaises(asyncio.CancelledError): t1.result() self.assertEqual(4, s1._queue_len) self.assertEqual((4, [b'data']), s1._queue.popleft()) self.loop.run_until_complete(go())
def test_close_on_cancelled_reading(self): port = find_unused_port() @asyncio.coroutine def go(): s1 = yield from aiozmq.create_zmq_stream( zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) def f(): yield from s1.read() t1 = asyncio.async(f(), loop=self.loop) # to run f() up to yield from yield from asyncio.sleep(0.001, loop=self.loop) t1.cancel() s1.feed_closing() yield from asyncio.sleep(0.001, loop=self.loop) with self.assertRaises(asyncio.CancelledError): t1.result() self.loop.run_until_complete(go())
def test_pause_reading(self): port = find_unused_port() @asyncio.coroutine def go(): s1 = yield from aiozmq.create_zmq_stream( zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) s2 = yield from aiozmq.create_zmq_stream( zmq.ROUTER, connect='tcp://127.0.0.1:{}'.format(port), loop=self.loop) s2.set_read_buffer_limits(high=5) s1.write([b'request']) yield from asyncio.sleep(0.01, loop=self.loop) self.assertTrue(s2._paused) msg = yield from s2.read() self.assertEqual([mock.ANY, b'request'], msg) self.assertFalse(s2._paused) self.loop.run_until_complete(go())
def go(): port = find_unused_port() tr, pr = yield from create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) client = yield from aiozmq.rpc.connect_rpc( connect='tcp://127.0.0.1:{}'.format(port), loop=self.loop) with log_hook('aiozmq.rpc', self.err_queue): tr.write([struct.pack('=HHLd?', 1, 2, 3, 4, True), b'bad_answer']) ret = yield from self.err_queue.get() self.assertEqual(logging.CRITICAL, ret.levelno) self.assertEqual("Cannot unpack %r", ret.msg) self.assertEqual( ([mock.ANY, b'bad_answer'],), ret.args) self.assertIsNotNone(ret.exc_info) client.close() tr.close()
def test_exception_after_drain(self): port = find_unused_port() @asyncio.coroutine def go(): s1 = yield from aiozmq.create_zmq_stream( zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) self.assertFalse(s1._paused) s1._protocol.pause_writing() @asyncio.coroutine def f(): yield from s1.drain() fut = asyncio.async(f(), loop=self.loop) yield from asyncio.sleep(0.01, loop=self.loop) exc = RuntimeError("exception") s1._protocol.connection_lost(exc) with self.assertRaises(RuntimeError) as cm: yield from fut self.assertIs(cm.exception, exc) self.loop.run_until_complete(go())
def test_disconnect_from_nonbinded_addr(self): port = find_unused_port() addr = 'tcp://127.0.0.1:{}'.format(port) @asyncio.coroutine def go(): tr, pr = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.SUB, connect=addr) yield from pr.connected self.assertEqual({addr}, tr.connections()) with self.assertRaises(OSError) as ctx: yield from tr.disconnect('ipc:///some-addr') # non-bound addr # TODO: check travis build and remove skip when test passed. if (ctx.exception.errno == errno.EAGAIN and os.environ.get('TRAVIS')): raise unittest.SkipTest("Travis has a bug, it returns " "EAGAIN for unknown endpoint") self.assertEqual(errno.ENOENT, ctx.exception.errno) self.assertEqual({addr}, tr.connections()) self.loop.run_until_complete(go())
def go(): port = find_unused_port() tr, pr = yield from create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) client = yield from aiozmq.rpc.connect_rpc( connect='tcp://127.0.0.1:{}'.format(port), loop=self.loop) with log_hook('aiozmq.rpc', self.err_queue): tr.write([struct.pack('=HHLd?', 1, 2, 34435, 4, True), msgpack.packb((1, 2))]) ret = yield from self.err_queue.get() self.assertEqual(logging.CRITICAL, ret.levelno) self.assertEqual("Unknown answer id: %d (%d %d %f %d) -> %s", ret.msg) self.assertEqual( (mock.ANY, 1, 2, 4.0, True, (1, 2)), ret.args) self.assertIsNone(ret.exc_info) client.close()
def test_unknown_format_at_client(self): port = find_unused_port() @asyncio.coroutine def go(): tr, pr = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port)) client = yield from aiozmq.rpc.connect_rpc( connect='tcp://127.0.0.1:{}'.format(port), loop=self.loop) with log_hook('aiozmq.rpc', self.err_queue): tr.write([b'invalid', b'structure']) ret = yield from self.err_queue.get() self.assertEqual(logging.CRITICAL, ret.levelno) self.assertEqual("Cannot unpack %r", ret.msg) self.assertEqual( ((b'invalid', b'structure'),), ret.args) self.assertIsNotNone(ret.exc_info) client.close() self.loop.run_until_complete(go())
def test_default_event_loop(self): port = find_unused_port() asyncio.set_event_loop_policy(aiozmq.ZmqEventLoopPolicy()) self.addCleanup(asyncio.set_event_loop_policy, None) self.addCleanup(asyncio.set_event_loop, None) @asyncio.coroutine def create(): server = yield from aiozmq.rpc.serve_rpc( MyHandler(self.loop), bind='tcp://127.0.0.1:{}'.format(port), loop=None) client = yield from aiozmq.rpc.connect_rpc( connect='tcp://127.0.0.1:{}'.format(port), loop=None) return client, server self.loop = loop = asyncio.get_event_loop() self.client, self.server = loop.run_until_complete(create()) @asyncio.coroutine def communicate(): ret = yield from self.client.call.func(1) self.assertEqual(2, ret) loop.run_until_complete(communicate())
def test_unknown_format_at_server(self): port = find_unused_port() @asyncio.coroutine def go(): server = yield from aiozmq.rpc.serve_rpc( MyHandler(self.loop), bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) tr, pr = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, connect='tcp://127.0.0.1:{}'.format(port)) yield from asyncio.sleep(0.001, loop=self.loop) with log_hook('aiozmq.rpc', self.err_queue): tr.write([b'invalid', b'structure']) ret = yield from self.err_queue.get() self.assertEqual(logging.CRITICAL, ret.levelno) self.assertEqual("Cannot unpack %r", ret.msg) self.assertEqual(((mock.ANY, b'invalid', b'structure'),), ret.args) self.assertIsNotNone(ret.exc_info) self.assertTrue(pr.received.empty()) server.close() self.loop.run_until_complete(go())
def test_close_on_error(self): port = find_unused_port() tr1, pr1 = self.loop.run_until_complete(aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop)) handler = mock.Mock() self.loop.set_exception_handler(handler) sock = tr1.get_extra_info('zmq_socket') sock.close() tr1.write([b'data']) self.assertTrue(tr1._closing) handler.assert_called_with( self.loop, {'protocol': pr1, 'exception': mock.ANY, 'transport': tr1, 'message': 'Fatal write error on zmq socket transport'}) # expecting 'Socket operation on non-socket' if sys.platform == 'darwin': errno = 38 else: errno = 88 check_errno(errno, handler.call_args[0][1]['exception'])
def test_using_external_zmq_context(self): port = find_unused_port() @asyncio.coroutine def go(): st, sp = yield from aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.ROUTER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) yield from sp.connected addr = list(st.bindings())[0] ct, cp = yield from aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, connect=addr, loop=self.loop) yield from cp.connected ct.close() yield from cp.closed st.close() yield from sp.closed self.loop.run_until_complete(go())
def test_default_event_loop(self): asyncio.set_event_loop(self.loop) port = find_unused_port() tr1, pr1 = self.loop.run_until_complete(aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, bind='tcp://127.0.0.1:{}'.format(port))) self.assertIs(self.loop, tr1._loop) tr1.close()
def test_pub_sub(self): port = find_unused_port() @asyncio.coroutine def connect_pub(): tr1, pr1 = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.PUB, bind='tcp://127.0.0.1:{}'.format(port)) self.assertEqual('CONNECTED', pr1.state) yield from pr1.connected return tr1, pr1 tr1, pr1 = self.loop.run_until_complete(connect_pub()) @asyncio.coroutine def connect_sub(): tr2, pr2 = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.SUB, connect='tcp://127.0.0.1:{}'.format(port)) self.assertEqual('CONNECTED', pr2.state) yield from pr2.connected tr2.setsockopt(zmq.SUBSCRIBE, b'node_id') return tr2, pr2 tr2, pr2 = self.loop.run_until_complete(connect_sub()) @asyncio.coroutine def communicate(): for i in range(5): tr1.write([b'node_id', b'publish']) try: request = yield from asyncio.wait_for(pr2.received.get(), 0.1, loop=self.loop) self.assertEqual([b'node_id', b'publish'], request) break except asyncio.TimeoutError: pass else: raise AssertionError("Cannot get message in subscriber") self.loop.run_until_complete(communicate()) @asyncio.coroutine def closing(): tr1.close() tr2.close() yield from pr1.closed self.assertEqual('CLOSED', pr1.state) yield from pr2.closed self.assertEqual('CLOSED', pr2.state) self.loop.run_until_complete(closing())
def test_resume_reading_not_paused(self): port = find_unused_port() tr1, pr1 = self.loop.run_until_complete(aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop)) with self.assertRaises(RuntimeError): tr1.resume_reading() tr1.close()
def test_default_event_loop(self): asyncio.set_event_loop(self.loop) port = find_unused_port() tr1, pr1 = self.loop.run_until_complete( aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, bind='tcp://127.0.0.1:{}'.format(port))) self.assertIs(self.loop, tr1._loop) tr1.close()
def create(): port = find_unused_port() server = yield from aiozmq.rpc.serve_rpc( MyHandler(self.loop), bind='tcp://127.0.0.1:{}'.format(port), loop=None) client = yield from aiozmq.rpc.connect_rpc( connect='tcp://127.0.0.1:{}'.format(port), loop=None) return client, server
def test_close_closing(self): port = find_unused_port() tr1, pr1 = self.loop.run_until_complete(aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop)) tr1.close() self.assertTrue(tr1._closing) tr1.close() self.assertTrue(tr1._closing)
def test_close_paused_connection(self): port = find_unused_port() async def go(): s1 = await aiozmq.create_zmq_stream( zmq.DEALER, bind="tcp://127.0.0.1:{}".format(port)) s1._protocol.pause_writing() s1.close() self.loop.run_until_complete(go())
def create(): port = find_unused_port() server = yield from aiozmq.rpc.serve_rpc( MyHandler(self.loop), bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop, log_exceptions=log_exceptions) client = yield from aiozmq.rpc.connect_rpc( connect='tcp://127.0.0.1:{}'.format(port), loop=self.loop, error_table=error_table, timeout=timeout) return client, server
def test_serve_bad_subscription(self): port = find_unused_port() async def create(): with self.assertRaises(TypeError): await aiozmq.rpc.serve_pubsub( {}, bind="tcp://127.0.0.1:{}".format(port), subscribe=123, ) self.loop.run_until_complete(create())
def test_drain(self): port = find_unused_port() @asyncio.coroutine def go(): s1 = yield from aiozmq.create_zmq_stream( zmq.REP, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) yield from s1.drain() self.loop.run_until_complete(go())
def test_dealer_router(self): port = find_unused_port() @asyncio.coroutine def connect_req(): tr1, pr1 = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port)) self.assertEqual('CONNECTED', pr1.state) yield from pr1.connected return tr1, pr1 tr1, pr1 = self.loop.run_until_complete(connect_req()) @asyncio.coroutine def connect_rep(): tr2, pr2 = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.ROUTER, connect='tcp://127.0.0.1:{}'.format(port)) self.assertEqual('CONNECTED', pr2.state) yield from pr2.connected return tr2, pr2 tr2, pr2 = self.loop.run_until_complete(connect_rep()) @asyncio.coroutine def communicate(): tr1.write([b'request']) request = yield from pr2.received.get() self.assertEqual(( mock.ANY, b'request', ), request) tr2.write([request[0], b'answer']) answer = yield from pr1.received.get() self.assertEqual((b'answer', ), answer) self.loop.run_until_complete(communicate()) @asyncio.coroutine def closing(): tr1.close() tr2.close() yield from pr1.closed self.assertEqual('CLOSED', pr1.state) yield from pr2.closed self.assertEqual('CLOSED', pr2.state) self.loop.run_until_complete(closing())
def test_socket_monitor(self): port = find_unused_port() @asyncio.coroutine def go(): # Create server and bind st, sp = yield from aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.ROUTER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) yield from sp.wait_ready addr = list(st.bindings())[0] # Create client but don't connect it yet. ct, cp = yield from aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, loop=self.loop) yield from cp.wait_ready # Establish an event monitor on the client socket yield from ct.enable_monitor() # Now that the socket event monitor is established, connect # the client to the server which will generate some events. yield from ct.connect(addr) yield from asyncio.sleep(0.1, loop=self.loop) yield from ct.disconnect(addr) yield from asyncio.sleep(0.1, loop=self.loop) yield from ct.connect(addr) # Send a message to the server. The server should respond and # this is used to compete the wait_done future. ct.write([b'Hello']) yield from cp.wait_done yield from ct.disable_monitor() ct.close() yield from cp.wait_closed st.close() yield from sp.wait_closed # Confirm that the events received by the monitor were valid. self.assertGreater(cp.events_received.qsize(), 0) while not cp.events_received.empty(): event = yield from cp.events_received.get() self.assertIn(event.event, ZMQ_EVENTS) self.loop.run_until_complete(go())
def test_monitor(self): port = find_unused_port() @asyncio.coroutine def go(): addr = 'tcp://127.0.0.1:{}'.format(port) s1 = yield from aiozmq.create_zmq_stream( zmq.ROUTER, bind=addr, loop=self.loop) @asyncio.coroutine def f(s, events): try: while True: event = yield from s.read_event() events.append(event) except aiozmq.ZmqStreamClosed: pass s2 = yield from aiozmq.create_zmq_stream( zmq.DEALER, loop=self.loop) events = [] t = asyncio.async(f(s2, events), loop=self.loop) yield from s2.transport.enable_monitor() yield from s2.transport.connect(addr) yield from s2.transport.disconnect(addr) yield from s2.transport.connect(addr) s2.write([b'request']) req = yield from s1.read() self.assertEqual([mock.ANY, b'request'], req) s1.write([req[0], b'answer']) answer = yield from s2.read() self.assertEqual([b'answer'], answer) s2.close() s1.close() yield from t # Confirm that the events received by the monitor were valid. self.assertGreater(len(events), 0) while len(events): event = events.pop() self.assertIsInstance(event, SocketEvent) self.assertIn(event.event, ZMQ_EVENTS) self.loop.run_until_complete(go())
def test_connects(self): port1 = find_unused_port() port2 = find_unused_port() port3 = find_unused_port() addr1 = 'tcp://127.0.0.1:{}'.format(port1) addr2 = 'tcp://127.0.0.1:{}'.format(port2) addr3 = 'tcp://127.0.0.1:{}'.format(port3) @asyncio.coroutine def go(): tr, pr = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, connect=[addr1, addr2]) yield from pr.connected self.assertEqual({addr1, addr2}, tr.connections()) yield from tr.connect(addr3) self.assertEqual({addr1, addr3, addr2}, tr.connections()) yield from tr.disconnect(addr1) self.assertEqual({addr2, addr3}, tr.connections()) tr.close() self.loop.run_until_complete(go())
def test_serve_bad_subscription(self): port = find_unused_port() @asyncio.coroutine def create(): with self.assertRaises(TypeError): yield from aiozmq.rpc.serve_pubsub( {}, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop, subscribe=123) self.loop.run_until_complete(create())
def test_double_force_close(self): port = find_unused_port() tr1, pr1 = self.loop.run_until_complete(aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop)) handler = mock.Mock() self.loop.set_exception_handler(handler) err = RuntimeError('error') tr1._fatal_error(err) tr1._fatal_error(err) self.loop.run_until_complete(pr1.closed)
def test_pub_sub(self): port = find_unused_port() @asyncio.coroutine def connect_pub(): tr1, pr1 = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.PUB, bind='tcp://127.0.0.1:{}'.format(port)) self.assertEqual('CONNECTED', pr1.state) yield from pr1.connected return tr1, pr1 tr1, pr1 = self.loop.run_until_complete(connect_pub()) @asyncio.coroutine def connect_sub(): tr2, pr2 = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.SUB, connect='tcp://127.0.0.1:{}'.format(port)) self.assertEqual('CONNECTED', pr2.state) yield from pr2.connected tr2.setsockopt(zmq.SUBSCRIBE, b'node_id') return tr2, pr2 tr2, pr2 = self.loop.run_until_complete(connect_sub()) @asyncio.coroutine def communicate(): tr1.write([b'node_id', b'publish']) request = yield from pr2.received.get() self.assertEqual((b'node_id', b'publish'), request) # Sorry, sleep is required to get rid of sporadic hangs # without that 0MQ not always establishes tcp connection # and waiting for message from sub socket hangs. time.sleep(0.1) self.loop.run_until_complete(communicate()) @asyncio.coroutine def closing(): tr1.close() tr2.close() yield from pr1.closed self.assertEqual('CLOSED', pr1.state) yield from pr2.closed self.assertEqual('CLOSED', pr2.state) self.loop.run_until_complete(closing())
def test_pub_sub(self): port = find_unused_port() @asyncio.coroutine def connect_pub(): tr1, pr1 = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.PUB, bind='tcp://127.0.0.1:{}'.format(port)) self.assertEqual('CONNECTED', pr1.state) yield from pr1.connected return tr1, pr1 tr1, pr1 = self.loop.run_until_complete(connect_pub()) @asyncio.coroutine def connect_sub(): tr2, pr2 = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.SUB, connect='tcp://127.0.0.1:{}'.format(port)) self.assertEqual('CONNECTED', pr2.state) yield from pr2.connected tr2.setsockopt(zmq.SUBSCRIBE, b'node_id') return tr2, pr2 tr2, pr2 = self.loop.run_until_complete(connect_sub()) @asyncio.coroutine def communicate(): tr1.write([b'node_id', b'publish']) request = yield from pr2.received.get() self.assertEqual([b'node_id', b'publish'], request) # Sorry, sleep is required to get rid of sporadic hangs # without that 0MQ not always establishes tcp connection # and waiting for message from sub socket hangs. time.sleep(0.1) self.loop.run_until_complete(communicate()) @asyncio.coroutine def closing(): tr1.close() tr2.close() yield from pr1.closed self.assertEqual('CLOSED', pr1.state) yield from pr2.closed self.assertEqual('CLOSED', pr2.state) self.loop.run_until_complete(closing())
def test_unsubscribe_from_fd_on_error(self): port = find_unused_port() tr1, pr1 = self.loop.run_until_complete(aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop)) handler = mock.Mock() self.loop.set_exception_handler(handler) sock = tr1.get_extra_info('zmq_socket') sock.close() tr1.write([b'data']) with self.assertRaises(KeyError): self.loop._selector.get_key(tr1._fd)
def test_drain_after_closing(self): port = find_unused_port() async def go(): s1 = await aiozmq.create_zmq_stream( zmq.DEALER, bind="tcp://127.0.0.1:{}".format(port)) s1.close() await asyncio.sleep(0) with self.assertRaises(ConnectionResetError): await s1.drain() self.loop.run_until_complete(go())
def test_getsockopt(self): port = find_unused_port() async def coro(): tr, pr = await aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, bind="tcp://127.0.0.1:{}".format(port), ) await pr.connected self.assertEqual(zmq.DEALER, tr.getsockopt(zmq.TYPE)) return tr, pr self.loop.run_until_complete(coro())
def test_getsockopt(self): port = find_unused_port() @asyncio.coroutine def coro(): tr, pr = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port)) yield from pr.connected self.assertEqual(zmq.DEALER, tr.getsockopt(zmq.TYPE)) return tr, pr self.loop.run_until_complete(coro())
def make_rpc_pair(self): port = find_unused_port() async def create(): server = await aiozmq.rpc.serve_rpc( MyHandler(), bind="tcp://127.0.0.1:{}".format(port)) client = await aiozmq.rpc.connect_rpc( connect="tcp://127.0.0.1:{}".format(port)) return client, server self.client, self.server = self.loop.run_until_complete(create()) return self.client, self.server
def test_close_paused_connection(self): port = find_unused_port() @asyncio.coroutine def go(): s1 = yield from aiozmq.create_zmq_stream( zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) s1._protocol.pause_writing() s1.close() self.loop.run_until_complete(go())
def test_create_zmq_connection_dns_in_connect(self): port = find_unused_port() async def connect(): addr = "tcp://localhost:{}".format(port) tr, pr = await aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.SUB, connect=addr ) await pr.connected self.assertEqual({addr}, tr.connections()) tr.close() self.loop.run_until_complete(connect())
def test_extra_zmq_type(self): port = find_unused_port() async def coro(): tr, pr = await aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, bind="tcp://127.0.0.1:{}".format(port), ) await pr.connected self.assertEqual(zmq.DEALER, tr.get_extra_info("zmq_type")) tr.close() self.loop.run_until_complete(coro())
def test_pause_reading(self): port = find_unused_port() tr1, pr1 = self.loop.run_until_complete( aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, bind="tcp://127.0.0.1:{}".format(port), ) ) self.assertFalse(tr1._paused) tr1.pause_reading() self.assertTrue(tr1._paused) tr1.resume_reading() self.assertFalse(tr1._paused) tr1.close()
def test_connects(self): port1 = find_unused_port() port2 = find_unused_port() port3 = find_unused_port() addr1 = "tcp://127.0.0.1:{}".format(port1) addr2 = "tcp://127.0.0.1:{}".format(port2) addr3 = "tcp://127.0.0.1:{}".format(port3) async def go(): tr, pr = await aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.REQ, connect=[addr1, addr2], ) await pr.connected self.assertEqual({addr1, addr2}, tr.connections()) await tr.connect(addr3) self.assertEqual({addr1, addr3, addr2}, tr.connections()) await tr.disconnect(addr1) self.assertEqual({addr2, addr3}, tr.connections()) tr.close() self.loop.run_until_complete(go())
def make_rpc_pair(self): port = find_unused_port() @asyncio.coroutine def create(): server = yield from aiozmq.rpc.serve_rpc( RootHandler(), bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) client = yield from aiozmq.rpc.connect_rpc( connect='tcp://127.0.0.1:{}'.format(port), loop=self.loop) return client, server self.client, self.server = self.loop.run_until_complete(create()) return self.client, self.server
def test_extra_zmq_type(self): port = find_unused_port() @asyncio.coroutine def coro(): tr, pr = yield from aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) yield from pr.connected self.assertEqual(zmq.DEALER, tr.get_extra_info('zmq_type')) tr.close() self.loop.run_until_complete(coro())
def test_getsockopt_badopt(self): port = find_unused_port() @asyncio.coroutine def connect(): tr, pr = yield from self.loop.create_zmq_connection( lambda: Protocol(self.loop), zmq.SUB, connect='tcp://127.0.0.1:{}'.format(port)) yield from pr.connected return tr, pr tr, pr = self.loop.run_until_complete(connect()) with self.assertRaises(OSError) as ctx: tr.getsockopt(1111) # invalid option self.assertEqual(zmq.EINVAL, ctx.exception.errno)
def test___repr__(self): port = find_unused_port() @asyncio.coroutine def coro(): tr, pr = yield from aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.DEALER, bind='tcp://127.0.0.1:{}'.format(port), loop=self.loop) yield from pr.connected self.assertRegex( repr(tr), '<ZmqTransport sock=<[^>]+> ' 'type=DEALER read=idle write=<idle, bufsize=0>>') tr.close() self.loop.run_until_complete(coro())
def test_setsockopt_badopt(self): port = find_unused_port() async def connect(): tr, pr = await aiozmq.create_zmq_connection( lambda: Protocol(self.loop), zmq.SUB, connect="tcp://127.0.0.1:{}".format(port), ) await pr.connected return tr, pr tr, pr = self.loop.run_until_complete(connect()) with self.assertRaises(OSError) as ctx: tr.setsockopt(1111, 1) # invalid option self.assertEqual(zmq.EINVAL, ctx.exception.errno)