def setUp(self): self.topic = 'foo' self.host = '127.0.0.1' self.port = 4150 super().setUp() self.http_writer = NsqdHttpWriter( self.host, self.port+1, loop=self.loop) create_topic_res = self.loop.run_until_complete( self.http_writer.create_topic(self.topic)) print("create_topic_res", create_topic_res) self.assertEqual(create_topic_res, "") self.auth_secret = 'test_secret'
async def test_create_channel(self): conn = NsqdHttpWriter('127.0.0.1', 4151, loop=self.loop) res = await conn.create_topic('zap') self.assertEqual('', res) res = await conn.create_channel('zap', 'bar') self.assertEqual('', res) await conn.close()
async def test_http_mpublish(self): http_writer = NsqdHttpWriter("127.0.0.1", 4151, loop=self.loop) messages = ['baz:1', b'baz:2', 3.14, 42] ok = await http_writer.mpub('http_baz', *messages) self.assertEqual(ok, 'OK') await http_writer.close()
async def test_ok(self): conn = NsqdHttpWriter('127.0.0.1', 4151, loop=self.loop) res = await conn.ping() self.assertEqual(res, 'OK') await conn.close()
async def test_delete_topic(self): conn = NsqdHttpWriter('127.0.0.1', 4151, loop=self.loop) res = await conn.delete_topic('foo2') self.assertEqual('', res) await conn.close()
async def test_mpub(self): conn = NsqdHttpWriter('127.0.0.1', 4151, loop=self.loop) res = await conn.mpub('baz', 'baz_msg:1', 'baz_msg:1') self.assertEqual('OK', res) await conn.close()
async def test_stats(self): conn = NsqdHttpWriter('127.0.0.1', 4151, loop=self.loop) res = await conn.stats() self.assertIn('version', res) await conn.close()
async def test_http_publish(self): http_writer = NsqdHttpWriter("127.0.0.1", 4151, loop=self.loop) ok = await http_writer.pub('http_baz', 'producer msg') self.assertEqual(ok, 'OK')
class NsqConnectionTest(BaseTest): def setUp(self): self.topic = 'foo' self.host = '127.0.0.1' self.port = 4150 super().setUp() self.http_writer = NsqdHttpWriter( self.host, self.port+1, loop=self.loop) create_topic_res = self.loop.run_until_complete( self.http_writer.create_topic(self.topic)) print("create_topic_res", create_topic_res) self.assertEqual(create_topic_res, "") self.auth_secret = 'test_secret' def tearDown(self): super().tearDown() @run_until_complete async def test_basic_instance(self): host, port = '127.0.0.1', 4150 conn = await create_connection(host=host, port=port, loop=self.loop) self.assertIsInstance(conn, TcpConnection) self.assertTrue('TcpConnection' in conn.__repr__()) self.assertTrue(host in conn.endpoint) self.assertTrue(str(port) in conn.endpoint) conn.close() self.assertEqual(conn.closed, True) @run_until_complete async def test_tls(self): conn = await create_connection(host=self.host, port=self.port, loop=self.loop) config = {'feature_negotiation': True, 'tls_v1': True, 'snappy': False, 'deflate': False } res = await conn.identify(**config) self.assertTrue(res) conn.close() @run_until_complete async def test_snappy(self): print("test_snappy 1") conn = await create_connection(host=self.host, port=self.port, loop=self.loop) print("test_snappy conn") config = {'feature_negotiation': True, 'tls_v1': False, 'snappy': True, 'deflate': False } self.assertIsInstance(conn._parser, Reader) config_res = await conn.identify(**config) print("test_snappy config", config_res) self.assertIsInstance(conn._parser, SnappyReader) config_res = json.loads(_convert_to_str(config_res)) if config_res.get('auth_required') is True: await conn.auth(self.auth_secret) print("test_snappy") await self._pub_sub_rdy_fin(conn) conn.close() @run_until_complete async def test_deflate(self): conn = await create_connection(host=self.host, port=self.port, loop=self.loop) config = {'feature_negotiation': True, 'tls_v1': False, 'snappy': False, 'deflate': True } self.assertIsInstance(conn._parser, Reader) nego_res = await conn.identify(**config) print(nego_res) self.assertIsInstance(conn._parser, DeflateReader) nego_res = json.loads(_convert_to_str(nego_res)) if nego_res.get('auth_required') is True: await conn.auth(self.auth_secret) await self._pub_sub_rdy_fin(conn) conn.close() @asyncio.coroutine async def _pub_sub_rdy_fin(self, conn): print("start _pub_sub_rdy_fin") print(conn.closed) ok = await conn.execute('PUB', 'foo', data=b'msg foo') print("_pub_sub_rdy_fin pub data", ok) self.assertEqual(ok, b'OK') await conn.execute(b'SUB', 'foo', 'bar') await conn.execute(b'RDY', 1) print("starting to get msg") msg = await conn._queue.get() print("get message", msg) self.assertEqual(msg.processed, False) await msg.fin() self.assertEqual(msg.processed, True) await conn.execute(b'CLS') @run_until_complete async def test_message(self): conn = await create_connection(host=self.host, port=self.port, loop=self.loop) resp = await conn.identify(feature_negotiation=True) resp = json.loads(_convert_to_str(resp)) if resp.get('auth_required') is True: await conn.auth(self.auth_secret) ok = await conn.execute(b'PUB', self.topic, data=b'boom') self.assertEqual(ok, b'OK') res = await conn.execute(b'SUB', self.topic, 'boom') self.assertEqual(res, b"OK") await conn.execute(b'RDY', 1) msg = await conn._queue.get() self.assertEqual(msg.processed, False) await msg.touch() self.assertEqual(msg.processed, False) await msg.req(1) self.assertEqual(msg.processed, True) await conn.execute(b'RDY', 1) new_msg = await conn._queue.get() res = await new_msg.fin() self.assertEqual(res, b"OK") self.assertEqual(msg.processed, True) await conn.execute(b'CLS') conn.close()