示例#1
0
     def test_subscribe_async(self):
          nc = Client()
          msgs = []

          @tornado.gen.coroutine
          def subscription_handler(msg):
               # Callback dispatched asynchronously and a coroutine
               # so it does not block.
               if msg.subject == "tests.1":
                    yield tornado.gen.sleep(0.5)
               if msg.subject == "tests.3":
                    yield tornado.gen.sleep(0.2)
               msgs.append(msg)

          yield nc.connect(io_loop=self.io_loop)
          sid = yield nc.subscribe_async("tests.>", cb=subscription_handler)

          for i in range(0, 5):
               yield nc.publish("tests.{0}".format(i), b'bar')

          # Wait a bit for messages to be received.
          yield tornado.gen.sleep(4.0)
          self.assertEqual(5, len(msgs))
          self.assertEqual("tests.1", msgs[4].subject)
          self.assertEqual("tests.3", msgs[3].subject)
          yield nc.close()
示例#2
0
     def test_subscribe_sync(self):
          nc = Client()
          msgs = []

          @tornado.gen.coroutine
          def subscription_handler(msg):
               # Futures for subscription are each processed
               # in sequence.
               if msg.subject == "tests.1":
                    yield tornado.gen.sleep(1.0)
               if msg.subject == "tests.3":
                    yield tornado.gen.sleep(1.0)
               msgs.append(msg)

          yield nc.connect(io_loop=self.io_loop)
          sid = yield nc.subscribe("tests.>", cb=subscription_handler)

          for i in range(0, 5):
               yield nc.publish("tests.{0}".format(i), b'bar')

          # Wait a bit for messages to be received.
          yield tornado.gen.sleep(4.0)
          self.assertEqual(5, len(msgs))
          self.assertEqual("tests.1", msgs[1].subject)
          self.assertEqual("tests.3", msgs[3].subject)
          yield nc.close()
示例#3
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")
示例#4
0
     def test_subscribe_async_non_coro(self):
          nc = Client()
          msgs = []

          def subscription_handler(msg):
               # Dispatched asynchronously but would be received in sequence...
               msgs.append(msg)

          yield nc.connect(io_loop=self.io_loop)
          sid = yield nc.subscribe_async("tests.>", cb=subscription_handler)

          for i in range(0, 5):
               yield nc.publish("tests.{0}".format(i), b'bar')

          # Wait a bit for messages to be received.
          yield tornado.gen.sleep(4.0)
          self.assertEqual(5, len(msgs))
          self.assertEqual("tests.1", msgs[1].subject)
          self.assertEqual("tests.3", msgs[3].subject)
          yield nc.close()
示例#5
0
     def test_subscribe_sync_non_coro(self):
          nc = Client()
          msgs = []

          def subscription_handler(msg):
               # Callback blocks so dispatched in sequence.
               if msg.subject == "tests.1":
                    time.sleep(0.5)
               if msg.subject == "tests.3":
                    time.sleep(0.2)
               msgs.append(msg)

          yield nc.connect(io_loop=self.io_loop)
          sid = yield nc.subscribe("tests.>", cb=subscription_handler)

          for i in range(0, 5):
               yield nc.publish("tests.{0}".format(i), b'bar')

          # Wait a bit for messages to be received.
          yield tornado.gen.sleep(4.0)
          self.assertEqual(5, len(msgs))
          self.assertEqual("tests.1", msgs[1].subject)
          self.assertEqual("tests.3", msgs[3].subject)
          yield nc.close()