def test_invalid_subscription_type(self): nc = NATS() with self.assertRaises(NatsError): yield from nc.subscribe("hello", cb=None, future=None) with self.assertRaises(NatsError): yield from nc.subscribe_async("hello", cb=None)
def test_subscribe_async_without_coroutine_unsupported(self): nc = NATS() msgs = [] def subscription_handler(msg): if msg.subject == "tests.1": time.sleep(0.5) if msg.subject == "tests.3": time.sleep(0.2) msgs.append(msg) yield from nc.connect(io_loop=self.loop) with self.assertRaises(NatsError): sid = yield from nc.subscribe_async("tests.>", cb=subscription_handler) yield from nc.close()
def test_async_await_subscribe_sync(self): nc = NATS() msgs = [] async def subscription_handler(msg): if msg.subject == "tests.1": await asyncio.sleep(0.5, loop=self.loop) if msg.subject == "tests.3": await asyncio.sleep(0.2, loop=self.loop) msgs.append(msg) yield from nc.connect(io_loop=self.loop) sid = yield from nc.subscribe_async("tests.>", cb=subscription_handler) for i in range(0, 5): yield from nc.publish("tests.{}".format(i), b'bar') # Wait a bit for messages to be received. yield from asyncio.sleep(1, loop=self.loop) self.assertEqual(5, len(msgs)) self.assertEqual("tests.1", msgs[4].subject) self.assertEqual("tests.3", msgs[3].subject) yield from nc.close()
def main(loop): parser = argparse.ArgumentParser() parser.add_argument('-n', '--count', default=DEFAULT_NUM_MSGS, type=int) parser.add_argument('-S', '--subject', default='test') parser.add_argument('-t', '--subtype', default='sync') parser.add_argument('--servers', default=[], action='append') args = parser.parse_args() servers = args.servers if len(args.servers) < 1: servers = ["nats://127.0.0.1:4222"] opts = { "servers": servers, "io_loop": loop, "allow_reconnect": False } # Make sure we're connected to a server first... nc = NATS() try: yield from nc.connect(**opts) except Exception as e: sys.stderr.write("ERROR: {0}".format(e)) show_usage_and_die() received = 0 start = None @asyncio.coroutine def handler(msg): nonlocal received nonlocal start received += 1 # Measure time from when we get the first message. if received == 1: start = time.monotonic() if (received % HASH_MODULO) == 0: sys.stdout.write("*") sys.stdout.flush() if args.subtype == 'sync': yield from nc.subscribe(args.subject, cb=handler) elif args.subtype == 'async': yield from nc.subscribe_async(args.subject, cb=handler) else: sys.stderr.write("ERROR: Unsupported type of subscription {0}".format(e)) show_usage_and_die() print("Waiting for {} messages on [{}]...".format(args.count, args.subject)) try: # Additional roundtrip with server to ensure everything has been # processed by the server already. yield from nc.flush() except ErrTimeout: print("Server flush timeout after {0}".format(DEFAULT_FLUSH_TIMEOUT)) while received < args.count: yield from asyncio.sleep(0.1, loop=loop) elapsed = time.monotonic() - start print("\nTest completed : {0} msgs/sec sent".format(args.count/elapsed)) print("Received {0} messages ({1} msgs/sec)".format(received, received/elapsed)) yield from nc.close()