def test_publish_request(self): nc = Client() yield nc.connect(io_loop=self.io_loop) self.assertEqual(Client.CONNECTED, nc._status) info_keys = nc._server_info.keys() self.assertTrue(len(info_keys) > 0) inbox = new_inbox() yield nc.publish_request("help.1", inbox, "hello") yield nc.publish_request("help.2", inbox, "world") yield tornado.gen.sleep(1.0) 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.decode("utf-8")) self.assertEqual(10, varz['in_bytes']) self.assertEqual(0, varz['out_bytes']) self.assertEqual(2, varz['in_msgs']) self.assertEqual(0, varz['out_msgs']) self.assertEqual(0, nc.stats['in_bytes']) self.assertEqual(10, nc.stats['out_bytes']) self.assertEqual(0, nc.stats['in_msgs']) self.assertEqual(2, nc.stats['out_msgs'])
def main(): nc = NATS() # Establish connection to the server. options = { "verbose": True, "servers": ["nats://127.0.0.1:4222"] } yield nc.connect(**options) def discover(msg=None): print("[Received]: %s" % msg.data) sid = yield nc.subscribe("discover", "", discover) # Only interested in 2 messages. yield nc.auto_unsubscribe(sid, 2) yield nc.publish("discover", "A") yield nc.publish("discover", "B") # Following two messages won't be received. yield nc.publish("discover", "C") yield nc.publish("discover", "D") # Request/Response def help_request_handler(msg): print("[Received]: %s" % msg.data) nc.publish(msg.reply.decode(), "OK, I can help!") # Susbcription using distributed queue yield nc.subscribe("help", "workers", help_request_handler) try: # Expect a single request and timeout after 500 ms response = yield nc.timed_request("help", "Hi, need help!", 500) print("[Response]: %s" % response.data) except tornado.gen.TimeoutError as e: print("Timeout! Need to retry...") # Customize number of responses to receive def many_responses(msg=None): print("[Response]: %s" % msg.data) yield nc.request("help", "please", expected=2, cb=many_responses) # Publish inbox my_inbox = new_inbox() yield nc.subscribe(my_inbox) yield nc.publish_request("help", my_inbox, "I can help too!") loop = tornado.ioloop.IOLoop.instance() yield tornado.gen.Task(loop.add_timeout, time.time() + 1) try: start = datetime.now() # Make roundtrip to the server and timeout after 1000 ms yield nc.flush(1000) end = datetime.now() print("Latency: %d µs" % (end.microsecond - start.microsecond)) except tornado.gen.TimeoutError as e: print("Timeout! Roundtrip too slow...")
def __init__(self, nats_client, subject, inbox=""): """Create a new instance of FStatelessNatsTornadoServer Args: nats_client: connected instance of nats.io.Client subject: subject to publish to """ super(FNatsTransport, self).__init__(request_size_limit=_NATS_MAX_MESSAGE_SIZE) self._nats_client = nats_client self._subject = subject self._inbox = inbox or new_inbox() self._is_open = False self._sub_id = None
def test_subscribe(self): nc = Client() options = { "io_loop": self.io_loop } yield nc.connect(**options) self.assertEqual(Client.CONNECTED, nc._status) info_keys = nc._server_info.keys() self.assertTrue(len(info_keys) > 0) inbox = new_inbox() yield nc.subscribe("help.1") yield nc.subscribe("help.2") yield tornado.gen.sleep(0.5) http = tornado.httpclient.AsyncHTTPClient() response = yield http.fetch('http://127.0.0.1:%d/connz' % self.server_pool[0].http_port) result = json.loads(response.body.decode("utf-8")) connz = result['connections'][0] self.assertEqual(2, connz['subscriptions'])
try: # Expect a single request and timeout after 500 ms response = yield nc.timed_request("help", "Hi, need help!", 500) print("[Response]: %s" % response.data) except tornado.gen.TimeoutError, e: print("Timeout! Need to retry...") # Customize number of responses to receive def many_responses(msg=None): print("[Response]: %s" % msg.data) yield nc.request("help", "please", expected=2, cb=many_responses) # Publish inbox my_inbox = new_inbox() yield nc.subscribe(my_inbox) yield nc.publish_request("help", my_inbox, "I can help too!") loop = tornado.ioloop.IOLoop.instance() yield tornado.gen.Task(loop.add_timeout, time.time() + 1) try: start = datetime.now() # Make roundtrip to the server and timeout after 1000 ms yield nc.flush(1000) end = datetime.now() print("Latency: %d µs" % (end.microsecond - start.microsecond)) except tornado.gen.TimeoutError, e: print("Timeout! Roundtrip too slow...") if __name__ == '__main__':
# Expect a single request and timeout after 500 ms response = yield nc.timed_request("help", "Hi, need help!", timeout=0.500) print("[Response]: %s" % response.data) except tornado.gen.TimeoutError, e: print("Timeout! Need to retry...") # Customize number of responses to receive def many_responses(msg=None): print("[Response]: %s" % msg.data) yield nc.request("help", "please", expected=2, cb=many_responses) # Publish inbox my_inbox = new_inbox() yield nc.subscribe(my_inbox) yield nc.publish_request("help", my_inbox, "I can help too!") loop = tornado.ioloop.IOLoop.instance() yield tornado.gen.Task(loop.add_timeout, time.time() + 1) try: start = datetime.now() # Make roundtrip to the server and timeout after 1 second yield nc.flush(1) end = datetime.now() print("Latency: %d µs" % (end.microsecond - start.microsecond)) except tornado.gen.TimeoutError, e: print("Timeout! Roundtrip too slow...")
def tests_generate_new_inbox(self): inbox = new_inbox() self.assertTrue(inbox.startswith(INBOX_PREFIX)) min_expected_len = len(INBOX_PREFIX) self.assertTrue(len(inbox) > min_expected_len)