def test_assert_right_proto_type(self): 'should return True if the msg if matched the reg of given proto' with patch.dict(Protocol.PROTOCOL_TABLE, { 'proto' : 'content' }, clear=True): msg = 'content' self.assertTrue(Protocol.assert_protocol_type(msg, 'proto'))
def test_protocol_regular_in_table(self): 'should return the proto regular if in protocol table' with patch.dict(Protocol.PROTOCOL_TABLE, { 'proto' : 'content' }, clear=True): self.assertEqual(Protocol.protocol_regular('proto'), re.compile('content'))
def test_nonexist_proto(self): 'should return False if proto not in table' with patch.dict(Protocol.PROTOCOL_TABLE, { 'proto' : 'content' }, clear=True): msg = 'content' self.assertFalse(Protocol.assert_protocol_type(msg, 'nonexist'))
def test_not_matched(self): 'should return the un-matched part of message' with patch.dict(Protocol.PROTOCOL_TABLE, { 'proto' : 'content' }, clear=True): msg = 'content_not_matched' self.assertEqual(Protocol.not_matched('proto', msg), '_not_matched')
def test_assert_wrong_proto_type(self): 'should return False if msg not matched the reg of given proto' with patch.dict(Protocol.PROTOCOL_TABLE, { 'proto' : 'content' }, clear=True): msg = 'somemsg' self.assertFalse(Protocol.assert_protocol_type(msg, 'proto'))
def test_not_matched(self): 'should return the un-matched part of message' with patch.dict(Protocol.PROTOCOL_TABLE, {'proto': 'content'}, clear=True): msg = 'content_not_matched' self.assertEqual(Protocol.not_matched('proto', msg), '_not_matched')
def _process_info(self, info): '''\ process server infomation message; Params: ===== info: nats server information ''' server_info = json.loads(info) if server_info["auth_required"]: conn_opts = self.conn.get_connection_options() self.conn.send_command(Protocol.connect_command(conn_opts), True)
def test_protocol_regular_in_table(self): 'should return the proto regular if in protocol table' with patch.dict(Protocol.PROTOCOL_TABLE, {'proto': 'content'}, clear=True): self.assertEqual(Protocol.protocol_regular('proto'), re.compile('content'))
def test_version(self): 'should return the protocol version' #with patch.dict(Protocol, PROTOCOL_VERSION='0-0-0-0'): version = Protocol.version() self.assertEqual(version, '0.5.0.beta.12')
def test_connect_command(self): 'should return the connect command' opts = {'user': '******', 'pass': '******'} self.assertEqual(Protocol.connect_command(opts), 'CONNECT {"user": "******", "pass": "******"}\r\n')
def test_pong_response(self): 'should return the pong protocol' self.assertEqual(Protocol.pong_response(), 'PONG\r\n')
def on_ping_request(self): 'handler when ping request received' self.pings += 1 self.conn.send_command(Protocol.pong_response())
def test_nonexist_proto(self): 'should return False if proto not in table' with patch.dict(Protocol.PROTOCOL_TABLE, {'proto': 'content'}, clear=True): msg = 'content' self.assertFalse(Protocol.assert_protocol_type(msg, 'nonexist'))
def test_assert_right_proto_type(self): 'should return True if the msg if matched the reg of given proto' with patch.dict(Protocol.PROTOCOL_TABLE, {'proto': 'content'}, clear=True): msg = 'content' self.assertTrue(Protocol.assert_protocol_type(msg, 'proto'))
def test_connect_command(self): 'should return the connect command' opts = {'user' : 'a', 'pass' : 'b'} self.assertEqual(Protocol.connect_command(opts), 'CONNECT {"user": "******", "pass": "******"}\r\n')
def test_assert_wrong_proto_type(self): 'should return False if msg not matched the reg of given proto' with patch.dict(Protocol.PROTOCOL_TABLE, {'proto': 'content'}, clear=True): msg = 'somemsg' self.assertFalse(Protocol.assert_protocol_type(msg, 'proto'))
def send_ping(self): "send ping request to nats server" self.pings_outstanding += 1 self.conn.send_command(Protocol.ping_request()) self.queue_server_rt(self.process_pong) self.conn.flush_pending()
def queue_server_rt(self, blk): 'queue server response' if not blk: return None self.pongs.put(blk) self.conn.send_command(Protocol.ping_request())
def test_ping_request(self): 'should return the ping protocol' self.assertEqual(Protocol.ping_request(), 'PING\r\n')