def test_reply_ok(self): """Test reply checking.""" self.assertEqual(Message.reply("foo", "ok").reply_ok(), True) self.assertEqual(Message.reply("foo", "ok", 1).reply_ok(), True) self.assertEqual(Message.reply("foo", "fail").reply_ok(), False) self.assertEqual(Message.reply("foo", "fail", "ok").reply_ok(), False) self.assertEqual(Message.request("foo", "ok").reply_ok(), False)
def test_str(self): msg = Message.reply('fail', 'on fire', mid=234) self.assertEqual(str(msg), '!fail[234] on\\_fire') # Expect slighty different results in case of invalid UTF-8 bytes for # PY2's native byte string compared to PY3's unicode msg = Message.reply('ok', b'invalid utf-8\xff\x00') if future.utils.PY2: self.assertEqual(str(msg), '!ok invalid\_utf-8\xff\\0') else: with self.assertRaises(UnicodeDecodeError): str(msg)
def test_reply_attributes(self): """Test reply message attributes.""" msg = Message.reply('hello', 'world', mid=None) self.assertEqual(msg.mtype, Message.REPLY) self.assertEqual(msg.name, 'hello') self.assertEqual(msg.arguments, [b'world']) self.assertIsNone(msg.mid)
def test_stop_cleanup(self): yield self.client.until_protocol() mid = 564 future_reply = self.client.future_request(Message.request( 'slow-command', 1, mid=mid)) # Stop client self.client.stop() reply, informs = yield future_reply self.assertEqual(reply, Message.reply( 'slow-command', 'fail', 'Client stopped before reply was received', mid=mid))
def test_disconnect_cleanup(self): yield self.client.until_protocol() mid = 55 future_reply = self.client.future_request(Message.request( 'slow-command', 1, mid=mid)) # Force a disconnect self.client._disconnect() reply, informs = yield future_reply self.assertEqual(reply, Message.reply( 'slow-command', 'fail', 'Connection closed before reply was received', mid=mid))
def test_stop_cleanup(self): self.client.wait_protocol(timeout=1) mid = 56 future_reply = Future() self.client.ioloop.add_callback( lambda : gen.chain_future(self.client.future_request(Message.request( 'slow-command', 1, mid=mid)), future_reply)) # Force a disconnect self.client.stop() reply, informs = future_reply.result(timeout=1) self.assertEqual(reply, Message.reply( 'slow-command', 'fail', 'Client stopped before reply was received', mid=mid))
def test_equality(self): class AlwaysEqual(object): def __eq__(self, other): return True msg = Message.inform("foo", "a", "b") assert msg == Message.inform("foo", "a", "b") assert msg != Message.request("foo", "a", "b") assert msg != Message.inform("bar", "a", "b") assert msg != Message.inform("foo", "a", "b", "c") assert msg != Message.reply("foo", "a", "b") assert msg != 3 assert msg == AlwaysEqual()
def test_stop_cleanup(self): yield self.client.until_protocol() mid = 564 future_reply = self.client.future_request( Message.request('slow-command', 1, mid=mid)) # Stop client self.client.stop() reply, informs = yield future_reply self.assertEqual( reply, Message.reply('slow-command', 'fail', 'Client stopped before reply was received', mid=mid))
def test_disconnect_cleanup(self): yield self.client.until_protocol() mid = 55 future_reply = self.client.future_request( Message.request('slow-command', 1, mid=mid)) # Force a disconnect self.client._disconnect() reply, informs = yield future_reply self.assertEqual( reply, Message.reply('slow-command', 'fail', 'Connection closed before reply was received', mid=mid))
def test_stop_cleanup(self): self.client.wait_protocol(timeout=1) mid = 56 future_reply = Future() self.client.ioloop.add_callback(lambda: gen.chain_future( self.client.future_request( Message.request('slow-command', 1, mid=mid)), future_reply)) # Force a disconnect self.client.stop() reply, informs = future_reply.result(timeout=1) self.assertEqual( reply, Message.reply('slow-command', 'fail', 'Client stopped before reply was received', mid=mid))
def test_repr(self): msg = Message.reply('ok', 'café', b'_bin ary\xff\x00\n\r\t\\\x1b', 123, 4.5, True, False, '', 'z' * 1100) # storing Message.arguments as byte string results in slightly different # reprs for PY2 compared to PY3. if future.utils.PY2: expected = ("<Message reply ok (caf\xc3\xa9, " "_bin\\_ary\xff\\0\\n\\r\\t\\\\\\e, " "123, 4.5, 1, 0, , " "{}..." ")>".format('z' * 1000)) else: expected = (r"<Message reply ok (b'caf\xc3\xa9', " r"b'_bin\\_ary\xff\\0\\n\\r\\t\\\\\\e', " r"b'123', b'4.5', b'1', b'0', b'', " r"b'{}...'" r")>".format('z' * 1000)) self.assertEqual(repr(msg), expected)
def future_request(self, msg, timeout=None, use_mid=None): """Send a request message, with future replies. Parameters ---------- msg : Message object The request Message to send. timeout : float in seconds How long to wait for a reply. The default is the the timeout set when creating the AsyncClient. use_mid : boolean, optional Whether to use message IDs. Default is to use message IDs if the server supports them. Returns ------- A tornado.concurrent.Future that resolves with: reply : Message object The reply message received. informs : list of Message objects A list of the inform messages received. """ mid = self._get_mid_and_update_msg(msg, use_mid) if msg.name in self.request_handlers: req = FakeClientRequestConnection(self.client_connection, msg) reply_msg = yield tornado.gen.maybe_future( self.request_handlers[msg.name](req, msg)) reply_informs = req.informs_sent else: reply_msg = Message.reply(msg.name, 'ok') reply_informs = [] reply_msg.mid = mid raise Return((reply_msg, reply_informs))
def future_request(self, msg, timeout=None, use_mid=None): """Send a request messsage, with future replies. Parameters ---------- msg : Message object The request Message to send. timeout : float in seconds How long to wait for a reply. The default is the the timeout set when creating the AsyncClient. use_mid : boolean, optional Whether to use message IDs. Default is to use message IDs if the server supports them. Returns ------- A tornado.concurrent.Future that resolves with: reply : Message object The reply message received. informs : list of Message objects A list of the inform messages received. """ mid = self._get_mid_and_update_msg(msg, use_mid) if msg.name in self.request_handlers: req = FakeClientRequestConnection(self.client_connection, msg) reply_msg = yield tornado.gen.maybe_future(self.request_handlers[msg.name](req, msg)) reply_informs = req.informs_sent else: reply_msg = Message.reply(msg.name, "ok") reply_informs = [] reply_msg.mid = mid raise Return((reply_msg, reply_informs))
def test_reply(self): """Test reply method.""" self.assertEqual(bytes(Message.reply("foo")), b"!foo") self.assertEqual(bytes(Message.reply("foo", mid=123)), b"!foo[123]") self.assertEqual(bytes(Message.reply("foo", "a", "b", mid=123)), b"!foo[123] a b")
def test_bytes_mid(self): msg = Message.reply('fail', 'on fire', mid=234) self.assertEqual(bytes(msg), b'!fail[234] on\\_fire')