def test_close_connection(self): nc = Client() options = { "dont_randomize": True, "servers": [ "nats://*****:*****@127.0.0.1:4223", "nats://*****:*****@127.0.0.1:4224" ], "io_loop": self.io_loop } yield nc.connect(**options) self.assertEqual(True, nc._server_info["auth_required"]) log = Log() sid_1 = yield nc.subscribe("foo", "", log.persist) self.assertEqual(sid_1, 1) sid_2 = yield nc.subscribe("bar", "", log.persist) self.assertEqual(sid_2, 2) sid_3 = yield nc.subscribe("quux", "", log.persist) self.assertEqual(sid_3, 3) yield nc.publish("foo", "hello") yield tornado.gen.sleep(1.0) # Done yield nc.close() orig_gnatsd = self.server_pool.pop(0) orig_gnatsd.finish() try: a = nc._current_server # Wait and assert that we don't reconnect. yield tornado.gen.sleep(3) finally: b = nc._current_server self.assertEqual(a.uri, b.uri) self.assertFalse(nc.is_connected) self.assertFalse(nc.is_reconnecting) self.assertTrue(nc.is_closed) with(self.assertRaises(ErrConnectionClosed)): yield nc.publish("hello", "world") with(self.assertRaises(ErrConnectionClosed)): yield nc.flush() with(self.assertRaises(ErrConnectionClosed)): yield nc.subscribe("hello", "worker") with(self.assertRaises(ErrConnectionClosed)): yield nc.publish_request("hello", "inbox", "world") with(self.assertRaises(ErrConnectionClosed)): yield nc.request("hello", "world") with(self.assertRaises(ErrConnectionClosed)): yield nc.timed_request("hello", "world")
def test_timed_request_timeout(self): class Parser(): def __init__(self, nc, t): self.nc = nc self.t = t def parse(self, data=''): self.nc._process_pong() nc = Client() nc._ps = Parser(nc, self) yield nc.connect(io_loop=self.io_loop) with self.assertRaises(tornado.gen.TimeoutError): yield nc.timed_request("hello", "world", timeout=0.5)
def test_timed_request(self): nc = Client() yield nc.connect(io_loop=self.io_loop) class Component: def __init__(self, nc): self.nc = nc @tornado.gen.coroutine def respond(self, msg=None): yield self.nc.publish(msg.reply, "ok:1") yield self.nc.publish(msg.reply, "ok:2") yield self.nc.publish(msg.reply, "ok:3") log = Log() c = Component(nc) yield nc.subscribe(">", "", log.persist) yield nc.subscribe("help", "", c.respond) reply = yield nc.timed_request("help", "please") self.assertEqual("ok:1", reply.data) http = tornado.httpclient.AsyncHTTPClient() response = yield http.fetch('http://127.0.0.1:%d/varz' % self.server_pool[0].http_port) varz = json.loads(response.body) self.assertEqual(18, varz['in_bytes']) self.assertEqual(28, varz['out_bytes']) self.assertEqual(4, varz['in_msgs']) self.assertEqual(6, varz['out_msgs']) self.assertEqual(2, len(log.records.keys())) self.assertEqual("please", log.records['help'][0].data) self.assertEqual(28, nc.stats['in_bytes']) self.assertEqual(18, nc.stats['out_bytes']) self.assertEqual(6, nc.stats['in_msgs']) self.assertEqual(4, nc.stats['out_msgs']) full_msg = '' for msg in log.records['help']: full_msg += msg.data self.assertEqual('please', full_msg)