async def test_parse_unknown_long_protocol_line(self): nc = MockNatsClient() msgs = 0 async def payload_test(sid, subject, reply, payload): nonlocal msgs msgs += 1 params = { "subject": "hello", "queue": None, "cb": payload_test, "future": None, } sub = Subscription(**params) nc._subs[1] = sub ps = Parser(nc) reply = 'A' * 2043 # FIXME: Malformed long protocol lines will not be detected # by the client, so we rely on the ping/pong interval # from the client to give up instead. data = '''PING\r\nWRONG hello 1 %s''' % reply await ps.parse(data.encode()) await ps.parse(b'''AAAAA 0''') self.assertEqual(ps.state, AWAITING_CONTROL_LINE) await ps.parse(b'''\r\n\r\n''') await ps.parse(b'''\r\n\r\n''')
async def test_parse_msg(self): nc = MockNatsClient() expected = b'hello world!' async def payload_test(sid, subject, reply, payload): self.assertEqual(payload, expected) params = { "subject": "hello", "queue": None, "cb": payload_test, "future": None, } sub = Subscription(**params) nc._subs[1] = sub ps = Parser(nc) data = b'MSG hello 1 world 12\r\n' await ps.parse(data) self.assertEqual(len(ps.buf), 0) self.assertEqual(len(ps.msg_arg.keys()), 3) self.assertEqual(ps.msg_arg["subject"], b'hello') self.assertEqual(ps.msg_arg["reply"], b'world') self.assertEqual(ps.msg_arg["sid"], 1) self.assertEqual(ps.needed, 12) self.assertEqual(ps.state, AWAITING_MSG_PAYLOAD) await ps.parse(b'hello world!') self.assertEqual(len(ps.buf), 12) self.assertEqual(ps.state, AWAITING_MSG_PAYLOAD) data = b'\r\n' await ps.parse(data) self.assertEqual(len(ps.buf), 0) self.assertEqual(ps.state, AWAITING_CONTROL_LINE)
async def test_parse_msg_long_subject_reply(self): nc = MockNatsClient() msgs = 0 async def payload_test(sid, subject, reply, payload): nonlocal msgs msgs += 1 params = { "subject": "hello", "queue": None, "cb": payload_test, "future": None, } sub = Subscription(**params) nc._subs[1] = sub ps = Parser(nc) reply = 'A' * 2043 data = '''PING\r\nMSG hello 1 %s''' % reply await ps.parse(data.encode()) await ps.parse(b'''AAAAA 0\r\n\r\nMSG hello 1 world 0''') self.assertEqual(msgs, 1) self.assertEqual(len(ps.buf), 19) self.assertEqual(ps.state, AWAITING_CONTROL_LINE) await ps.parse(b'''\r\n\r\n''') self.assertEqual(msgs, 2)