Exemplo n.º 1
0
     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")
Exemplo n.º 2
0
     def test_custom_ping_interval(self):

          class Parser():
               def __init__(self, nc, t):
                    self.nc = nc
                    self.t = t

               def read(self, data=''):
                    self.t.assertEqual(1, len(self.nc._pongs))
                    self.nc._process_pong()
                    self.t.assertEqual(0, len(self.nc._pongs))

          nc = Client()
          nc._ps = Parser(nc, self)
          yield nc.connect(io_loop=self.io_loop, ping_interval=0.01)
          yield tornado.gen.sleep(1)
          self.assertEqual(3, nc._pings_outstanding)
          self.assertFalse(nc.is_connected())
          self.assertTrue(nc.is_reconnecting())
Exemplo n.º 3
0
     def test_auth_pending_bytes_handling(self):
          nc = Client()

          class Component:

               def __init__(self, nc):
                    self.nc = nc
                    self.errors = []
                    self.written = 0
                    self.max_messages = 2000
                    self.disconnected_at = 0
                    self.pending_bytes_when_closed = 0
                    self.pending_bytes_when_reconnected = 0

               def error_cb(self, err):
                    self.errors.append(err)

               def disconnected_cb(self):
                    self.disconnected_at = self.written
                    self.pending_bytes_when_closed = len(self.nc._pending)

               def reconnected_cb(self):
                    self.pending_bytes_when_reconnected = len(self.nc._pending)

               @tornado.gen.coroutine
               def publisher(self):
                    for i in range(0, self.max_messages):
                         yield self.nc.publish("foo", "{0},".format(i))
                         # yield self.nc.flush()
                         self.written += 1
                         yield tornado.gen.sleep(0.0001)

          c = Component(nc)

          options = {
               "dont_randomize": True,
               "servers": [
                    "nats://*****:*****@127.0.0.1:4223",
                    "nats://*****:*****@127.0.0.1:4224"
                    ],
               "io_loop": self.io_loop,
               "reconnected_cb": c.reconnected_cb,
               "disconnected_cb": c.disconnected_cb,
               "error_cb": c.error_cb,
               }
          yield c.nc.connect(**options)
          self.assertEqual(True, nc._server_info["auth_required"])

          log = Log()
          yield c.nc.subscribe("foo",  "", log.persist)
          self.io_loop.spawn_callback(c.publisher)

          yield tornado.gen.sleep(0.5)
          orig_gnatsd = self.server_pool.pop(0)
          orig_gnatsd.finish()

          try:
               a = nc._current_server
               # Wait for reconnect logic kick in...
               yield tornado.gen.sleep(5)
          finally:
               b = nc._current_server
               self.assertNotEqual(a.uri, b.uri)

          # Should have reconnected already
          self.assertTrue(nc.is_connected())
          self.assertFalse(nc.is_reconnecting())

          # Wait a bit until it flushes all...
          yield tornado.gen.sleep(1)

          http = tornado.httpclient.AsyncHTTPClient()
          response = yield http.fetch('http://127.0.0.1:8224/connz')
          result = json.loads(response.body.decode("utf-8"))
          connz = result['connections'][0]
          self.assertEqual(c.max_messages - c.disconnected_at, connz['in_msgs'])
          self.assertTrue(c.pending_bytes_when_reconnected > c.pending_bytes_when_closed)
Exemplo n.º 4
0
     def test_auth_connect(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)

          http = tornado.httpclient.AsyncHTTPClient()
          response = yield http.fetch('http://127.0.0.1:8223/connz')
          result = json.loads(response.body.decode("utf-8"))
          connz = result['connections'][0]
          self.assertEqual(3, connz['subscriptions'])
          self.assertEqual(1, connz['in_msgs'])
          self.assertEqual(5, connz['in_bytes'])

          yield nc.publish("foo", "world")
          yield tornado.gen.sleep(0.5)
          response = yield http.fetch('http://127.0.0.1:8223/connz')
          result = json.loads(response.body.decode("utf-8"))
          connz = result['connections'][0]
          self.assertEqual(3, connz['subscriptions'])
          self.assertEqual(2, connz['in_msgs'])
          self.assertEqual(10, connz['in_bytes'])

          orig_gnatsd = self.server_pool.pop(0)
          orig_gnatsd.finish()

          try:
               a = nc._current_server
               # Wait for reconnect logic kick in...
               yield tornado.gen.sleep(5)
          finally:
               b = nc._current_server
               self.assertNotEqual(a.uri, b.uri)

          self.assertTrue(nc.is_connected())
          self.assertFalse(nc.is_reconnecting())

          http = tornado.httpclient.AsyncHTTPClient()
          response = yield http.fetch('http://127.0.0.1:8224/connz')
          result = json.loads(response.body.decode("utf-8"))
          connz = result['connections'][0]
          self.assertEqual(3, connz['subscriptions'])
          self.assertEqual(0, connz['in_msgs'])
          self.assertEqual(0, connz['in_bytes'])

          yield nc.publish("foo", "!!!")
          yield tornado.gen.sleep(0.5)
          response = yield http.fetch('http://127.0.0.1:8224/connz')
          result = json.loads(response.body.decode("utf-8"))
          connz = result['connections'][0]
          self.assertEqual(3, connz['subscriptions'])
          self.assertEqual(1, connz['in_msgs'])
          self.assertEqual(3, connz['in_bytes'])

          full_msg = ''
          for msg in log.records[b'foo']:
               full_msg += msg.data.decode("utf-8")

          self.assertEqual('helloworld!!!', full_msg)