Ejemplo n.º 1
0
    def test_packing_errors(self):
        # Assert we get an unknown command
        cmd_type = 1234
        cmd_args = dict()
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get a fake command
        cmd_type = protocol.GEARMAN_COMMAND_TEXT_COMMAND
        cmd_args = dict()
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get arg mismatch, got 1, expecting 0
        cmd_type = protocol.GEARMAN_COMMAND_GRAB_JOB
        cmd_args = dict(extra='arguments')
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get arg mismatch, got 0, expecting 1
        cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        cmd_args = dict()
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get arg mismatch (name), got 1, expecting 1
        cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        cmd_args = dict(extra='arguments')
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get a non-string argument
        cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        cmd_args = dict(job_handle=12345)
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get a non-string argument (expecting BYTES)
        # !NOTE! fix this test
        #cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        #cmd_args = dict(job_handle='12345')
        #self.assertRaises(ProtocolError, protocol.pack_binary_command, cmd_type, cmd_args)

        # Assert we check for NULLs in all but the "last" argument, where last depends on the cmd_type.
        #cmd_type = protocol.GEARMAN_COMMAND_SUBMIT_JOB
        #cmd_args = dict(task='funct\x00ion', data='abcd', unique='12345')
        #self.assertRaises(ProtocolError, protocol.pack_binary_command, cmd_type, cmd_args)

        # Assert we check for NULLs in all but the "last" argument, where last depends on the cmd_type.
        cmd_type = protocol.GEARMAN_COMMAND_SUBMIT_JOB
        cmd_args = dict(task='function', data='ab\x00cd', unique='12345')
        protocol.pack_binary_command(
            cmd_type, cmd_args)  # Should not raise, 'data' is last.
Ejemplo n.º 2
0
    def test_packing_no_arg(self):
        cmd_type = protocol.GEARMAN_COMMAND_NOOP
        cmd_args = dict()

        expected_command_buffer = struct.pack(
            '!4sII', bytes(protocol.MAGIC_REQ_STRING, 'utf-8'), cmd_type, 0)
        packed_command_buffer = protocol.pack_binary_command(
            cmd_type, cmd_args)
        self.assertEqual(packed_command_buffer, expected_command_buffer)
Ejemplo n.º 3
0
    def test_packing_response(self):
        # Test packing a response for a job (server side packing)
        cmd_type = protocol.GEARMAN_COMMAND_NO_JOB
        cmd_args = dict()

        expected_command_buffer = struct.pack(
            '!4sII', bytes(protocol.MAGIC_RES_STRING, 'utf-8'), cmd_type, 0)
        packed_command_buffer = protocol.pack_binary_command(cmd_type,
                                                             cmd_args,
                                                             is_response=True)
        self.assertEqual(packed_command_buffer, expected_command_buffer)
Ejemplo n.º 4
0
    def test_packing_single_arg(self):
        cmd_type = protocol.GEARMAN_COMMAND_ECHO_REQ
        cmd_args = dict(data='abcde')

        expected_payload_size = len(cmd_args['data'])
        expected_format = '!4sII%ds' % expected_payload_size

        expected_command_buffer = struct.pack(
            expected_format, bytes(protocol.MAGIC_REQ_STRING, 'utf-8'),
            cmd_type, expected_payload_size, bytes(cmd_args['data'], 'utf-8'))
        packed_command_buffer = protocol.pack_binary_command(
            cmd_type, cmd_args)
        self.assertEqual(packed_command_buffer, expected_command_buffer)
Ejemplo n.º 5
0
    def _pack_command(self, cmd_type, cmd_args):
        """Converts a command to its raw binary format"""
        if cmd_type not in GEARMAN_PARAMS_FOR_COMMAND:
            raise ProtocolError('Unknown command: %r' %
                                get_command_name(cmd_type))

        if _DEBUG_MODE_:
            gearman_logger.debug('%s - Send - %s - %r', hex(id(self)),
                                 get_command_name(cmd_type), cmd_args)

        if cmd_type == GEARMAN_COMMAND_TEXT_COMMAND:
            return pack_text_command(cmd_type, cmd_args)
        else:
            # We'll be sending a response if we know we're a server side
            # command
            is_response = bool(self._is_server_side)
            return pack_binary_command(cmd_type, cmd_args, is_response)
Ejemplo n.º 6
0
    def test_packing_multiple_args(self):
        cmd_type = protocol.GEARMAN_COMMAND_SUBMIT_JOB
        cmd_args = dict(task='function', unique='12345', data='abcd')

        ordered_parameters = [
            cmd_args['task'], cmd_args['unique'], cmd_args['data']
        ]

        expected_payload = protocol.NULL_CHAR.join(ordered_parameters)
        expected_payload_size = len(expected_payload)
        expected_format = '!4sII%ds' % expected_payload_size
        expected_command_buffer = struct.pack(
            expected_format, bytes(protocol.MAGIC_REQ_STRING, 'utf-8'),
            cmd_type, expected_payload_size, bytes(expected_payload, 'utf-8'))

        packed_command_buffer = protocol.pack_binary_command(
            cmd_type, cmd_args)
        self.assertEqual(packed_command_buffer, expected_command_buffer)