def test_parsing_single_line(self): received_data = b"Hello there\n" cmd_type, cmd_response, cmd_len = protocol.parse_text_command( received_data) self.assertEquals(cmd_type, protocol.GEARMAN_COMMAND_TEXT_COMMAND) self.assertEquals(cmd_response, dict(raw_text=received_data.strip())) self.assertEquals(cmd_len, len(received_data))
def test_parsing_without_enough_data(self): received_data = array.array("c", "Hello there") cmd_type, cmd_response, cmd_len = protocol.parse_text_command( received_data) self.assertEquals(cmd_type, None) self.assertEquals(cmd_response, None) self.assertEquals(cmd_len, 0)
def test_parsing_single_line(self): received_data = array.array("b", b"Hello there\n") cmd_type, cmd_response, cmd_len = protocol.parse_text_command(received_data) assert cmd_type == protocol.GEARMAN_COMMAND_TEXT_COMMAND assert cmd_response == { u"raw_text": compat.array_to_bytes(received_data).strip() } assert cmd_len == len(received_data)
def test_parsing_multi_line(self): sentence_one = "Hello there\n" sentence_two = "My name is bob\n" received_data = sentence_one + sentence_two cmd_type, cmd_response, cmd_len = protocol.parse_text_command(received_data) self.assertEquals(cmd_type, protocol.GEARMAN_COMMAND_TEXT_COMMAND) self.assertEquals(cmd_response, dict(raw_text=sentence_one.strip())) self.assertEquals(cmd_len, len(sentence_one))
def test_parsing_single_line(self): received_data = array.array("b", b"Hello there\n") cmd_type, cmd_response, cmd_len = protocol.parse_text_command(received_data) self.assertEqual(cmd_type, protocol.GEARMAN_COMMAND_TEXT_COMMAND) if hasattr(received_data, 'tobytes'): self.assertEqual(cmd_response, dict(raw_text=received_data.tobytes().strip())) else: self.assertEqual(cmd_response, dict(raw_text=received_data.tostring().strip())) self.assertEqual(cmd_len, len(received_data))
def test_parsing_multi_line(self): sentence_one = array.array("b", "Hello there\n".encode()) sentence_two = array.array("b", "My name is bob\n".encode()) received_data = sentence_one + sentence_two cmd_type, cmd_response, cmd_len = protocol.parse_text_command(received_data) self.assertEquals(cmd_type, protocol.GEARMAN_COMMAND_TEXT_COMMAND) self.assertEquals(cmd_response, dict(raw_text=str(sentence_one.tostring().strip(), 'utf-8'))) self.assertEquals(cmd_len, len(sentence_one))
def test_parsing_multi_line(self): sentence_one = array.array("b", b"Hello there\n") sentence_two = array.array("b", b"My name is bob\n") received_data = sentence_one + sentence_two cmd_type, cmd_response, cmd_len = protocol.parse_text_command(received_data) assert cmd_type == protocol.GEARMAN_COMMAND_TEXT_COMMAND assert cmd_response == { u"raw_text": compat.array_to_bytes(sentence_one).strip() } assert cmd_len == len(sentence_one)
def test_parsing_multi_line(self): sentence_one = array.array("b", b"Hello there\n") sentence_two = array.array("b", b"My name is bob\n") received_data = sentence_one + sentence_two cmd_type, cmd_response, cmd_len = protocol.parse_text_command(received_data) self.assertEqual(cmd_type, protocol.GEARMAN_COMMAND_TEXT_COMMAND) if hasattr(sentence_one, 'tobytes'): self.assertEqual(cmd_response, dict(raw_text=sentence_one.tobytes().strip())) else: self.assertEqual(cmd_response, dict(raw_text=sentence_one.tostring().strip())) self.assertEqual(cmd_len, len(sentence_one))
def _unpack_command(self, given_buffer): """Conditionally unpack a binary command or a text based server command""" if not given_buffer: cmd_type, cmd_args, cmd_len = None, None, 0 elif given_buffer[0] == protocol.NULL_CHAR: # We'll be expecting a response if we know we're a client side command cmd_type, cmd_args, cmd_len = protocol.parse_binary_command( given_buffer) else: cmd_type, cmd_args, cmd_len = protocol.parse_text_command( given_buffer) log.msg("%s - recv - %s - %r" % ( id(self), protocol.get_command_name(cmd_type), cmd_args), loglevel=logging.DEBUG) return cmd_type, cmd_args, cmd_len
def _unpack_command(self, given_buffer): """Conditionally unpack a binary command or a text based server command""" assert self._is_client_side is not None, "Ambiguous connection state" if not given_buffer: cmd_type = None cmd_args = None cmd_len = 0 elif given_buffer[0] == NULL_CHAR: # We'll be expecting a response if we know we're a client side command is_response = bool(self._is_client_side) cmd_type, cmd_args, cmd_len = parse_binary_command(given_buffer, is_response=is_response) else: cmd_type, cmd_args, cmd_len = parse_text_command(given_buffer) if _DEBUG_MODE_ and cmd_type is not None: gearman_logger.debug('%s - Recv - %s - %r', hex(id(self)), get_command_name(cmd_type), cmd_args) return cmd_type, cmd_args, cmd_len
def test_parsing_single_line(self): received_data = "Hello there\n" cmd_type, cmd_response, cmd_len = protocol.parse_text_command(received_data) self.assertEquals(cmd_type, protocol.GEARMAN_COMMAND_TEXT_COMMAND) self.assertEquals(cmd_response, dict(raw_text=received_data.strip())) self.assertEquals(cmd_len, len(received_data))
def test_parsing_without_enough_data(self): received_data = "Hello there" cmd_type, cmd_response, cmd_len = protocol.parse_text_command(received_data) self.assertEquals(cmd_type, None) self.assertEquals(cmd_response, None) self.assertEquals(cmd_len, 0)
def test_parsing_single_line(self): received_data = array.array("b", "Hello there\n".encode()) cmd_type, cmd_response, cmd_len = protocol.parse_text_command(received_data) self.assertEquals(cmd_type, protocol.GEARMAN_COMMAND_TEXT_COMMAND) self.assertEquals(cmd_response, dict(raw_text=str(received_data.tostring().strip(), 'utf-8'))) self.assertEquals(cmd_len, len(received_data))
def test_parsing_without_enough_data(self): received_data = array.array("b", b"Hello there") cmd_type, cmd_response, cmd_len = protocol.parse_text_command(received_data) assert cmd_type is None assert cmd_response is None assert cmd_len == 0
def test_parsing_errors(self): received_data = array.array("b", b"Hello\x00there\n") with pytest.raises(ProtocolError): protocol.parse_text_command(received_data)