def test_decode_command_string(self):
        # test strings
        string_in = 'play?some_value=7&another_value=2'
        command = 'play'
        kwargs = dict(some_value='7', another_value='2')

        actual_command, actual_kwargs = decode_command_string(string_in)

        self.assertEqual(command, actual_command)
        self.assertEqual(kwargs, actual_kwargs)

        # test ints, floats, bools, and none
        string_in = 'play?some_int=int:7&some_float=float:2.0&some_none' \
                    '=NoneType:&some_true=bool:True&some_false=bool:False'
        command = 'play'
        kwargs = dict(some_int=7,
                      some_float=2.0,
                      some_none=None,
                      some_true=True,
                      some_false=False)

        actual_command, actual_kwargs = decode_command_string(string_in)

        self.assertEqual(command, actual_command)
        self.assertEqual(kwargs, actual_kwargs)
    def test_decode_command_string(self):
        # test strings
        string_in = 'play?some_value=7&another_value=2'
        command = 'play'
        kwargs = dict(some_value='7', another_value='2')

        actual_command, actual_kwargs = decode_command_string(string_in)

        self.assertEqual(command, actual_command)
        self.assertEqual(kwargs, actual_kwargs)

        # test ints, floats, bools, and none
        string_in = 'play?some_int=int:7&some_float=float:2.0&some_none' \
                    '=NoneType:&some_true=bool:True&some_false=bool:False'
        command = 'play'
        kwargs = dict(some_int=7, some_float=2.0, some_none=None,
                      some_true=True, some_false=False)

        actual_command, actual_kwargs = decode_command_string(string_in)

        self.assertEqual(command, actual_command)
        self.assertEqual(kwargs, actual_kwargs)

        string_in = "machine_variable?name=player1_score&prev_value=int:132990&value=&change=bool:True"
        actual_command, actual_kwargs = decode_command_string(string_in)
        self.assertEqual("machine_variable", actual_command)
        self.assertEqual({'prev_value': 132990, 'value': '', 'name': 'player1_score', 'change': True}, actual_kwargs)
Example #3
0
    def testConnect(self):
        # check that server was opened
        self.assertTrue(self.mock_server.is_bound)

        # add client
        client = MockQueueSocket()
        self.mock_server.add_client(client)
        self.advance_time_and_run()

        # check hello
        self.assertEqual(1, len(client.send_queue))
        cmd, kwargs = decode_command_string(
            client.send_queue.pop()[0:-1].decode())
        self.assertEqual("hello", cmd)

        # test trigger
        self.mock_event("test_event")
        client.recv_queue.append(
            (encode_command_string("trigger", name="test_event") +
             '\n').encode())
        self.advance_time_and_run()
        self.assertEqual(1, self._events['test_event'])

        # register for event/trigger
        client.recv_queue.append(
            (encode_command_string("register_trigger", event="test_trigger") +
             '\n').encode())
        self.advance_time_and_run()
        self.assertEqual(0, len(client.send_queue))

        # post trigger event
        self.post_event("test_trigger")
        self.advance_time_and_run()
        self.assertEqual(1, len(client.send_queue))
        cmd, kwargs = decode_command_string(
            client.send_queue.pop()[0:-1].decode())
        self.assertEqual("trigger", cmd)
        self.assertEqual("test_trigger", kwargs['name'])

        # send goodbye. machine should continue to run.
        client.close = MagicMock()
        client.recv_queue.append(
            (encode_command_string("goodbye") + '\n').encode())
        self.advance_time_and_run()

        client.close.assert_called_with()

        self.assertFalse(self.machine._done)
Example #4
0
 def test_roundtrip(self):
     test_kwargs = {
         "registered_handlers": [{
             "calling_context":
             "logicblock_baby_bonus_hit{count%10==5}"
         }]
     }
     encoded_cmd = encode_command_string("test", **test_kwargs)
     decoded_cmd, decoded_cmd_kwargs = decode_command_string(encoded_cmd)
     self.assertEqual(decoded_cmd_kwargs, test_kwargs)
    def test_json_encoding_decoding_with_hashtags(self):
        # see: https://github.com/missionpinball/mpf/issues/1495
        command = 'play'
        kwargs = dict()
        kwargs['dict1'] = dict(key1='value1 #', key2='value2 #')

        encoded_string = encode_command_string(command, **kwargs)
        decoded_command, decoded_dict = decode_command_string(encoded_string)

        self.assertEqual('play', decoded_command)
        self.assertIn('dict1', decoded_dict)
        self.assertEqual(decoded_dict['dict1']['key1'], 'value1 #')
        self.assertEqual(decoded_dict['dict1']['key2'], 'value2 #')
Example #6
0
    def test_json_encoding_decoding(self):
        # test with dicts
        command = 'play'
        kwargs = dict()
        kwargs['dict1'] = dict(key1='value1', key2='value2')
        kwargs['dict2'] = dict(key3='value3', key4='value4')

        encoded_string = encode_command_string(command, **kwargs)
        decoded_command, decoded_dict = decode_command_string(encoded_string)

        self.assertEqual('play', decoded_command)
        self.assertIn('dict1', decoded_dict)
        self.assertIn('dict2', decoded_dict)
        self.assertEqual(decoded_dict['dict1']['key1'], 'value1')
        self.assertEqual(decoded_dict['dict1']['key2'], 'value2')
        self.assertEqual(decoded_dict['dict2']['key3'], 'value3')
        self.assertEqual(decoded_dict['dict2']['key4'], 'value4')

        # test with list
        command = 'play'
        kwargs = dict()
        kwargs['dict1'] = dict(key1='value1', key2='value2')
        kwargs['dict2'] = list()
        kwargs['dict2'].append(dict(key3='value3', key4='value4'))
        kwargs['dict2'].append(dict(key3='value5', key4='value6'))

        encoded_string = encode_command_string(command, **kwargs)
        decoded_command, decoded_dict = decode_command_string(encoded_string)

        self.assertEqual('play', decoded_command)
        self.assertIn('dict1', decoded_dict)
        self.assertIn('dict2', decoded_dict)
        self.assertEqual(decoded_dict['dict1']['key1'], 'value1')
        self.assertEqual(decoded_dict['dict1']['key2'], 'value2')
        self.assertEqual(decoded_dict['dict2'][0],
                         dict(key3='value3', key4='value4'))
        self.assertEqual(decoded_dict['dict2'][1],
                         dict(key3='value5', key4='value6'))
Example #7
0
    def receive_bcp_message(self, msg):
        """Receives an incoming BCP message to be processed.

        Note this method is intended for testing. Usually BCP messages are
        handled by the BCP Server thread, but for test purposes it's possible
        to run mpf-mc without the BCP Server, so in that case you can use this
        method to send BCP messages into the mpf-mc.

        Args:
            msg: A string of the BCP message (in the standard BCP format:
                command?param1=value1&param2=value2...

        """
        cmd, kwargs = bcp.decode_command_string(msg)
        self.receive_queue.put((cmd, kwargs))
Example #8
0
    def process_received_message(self, message):
        """Puts a received BCP message into the receiving queue.

        Args:
            message: The incoming BCP message

        """
        self.log.debug('Received "%s"', message)

        try:
            cmd, kwargs = bcp.decode_command_string(message)
            self.receive_queue.put((cmd, kwargs))
        except ValueError:
            self.log.error("DECODE BCP ERROR. Message: %s", message)
            raise
Example #9
0
    def process_received_message(self, message):
        """Puts a received BCP message into the receiving queue.

        Args:
            message: The incoming BCP message

        """
        self.log.debug('Received "%s"', message)
        if self.caching_enabled and not self.simulate:
            time = datetime.now() - self.last_time
            message_tmr = math.floor(time.microseconds / 1000)
            self.last_time = datetime.now()
            self.cache_file.write(str(message_tmr) + "," + message + "\n")

        try:
            cmd, kwargs = bcp.decode_command_string(message)
            self.receive_queue.put((cmd, kwargs))
        except ValueError:
            self.log.error("DECODE BCP ERROR. Message: %s", message)
            raise
Example #10
0
 async def _get_and_decode(client) -> Tuple[str, dict]:
     data = await client.send_queue.get()
     return decode_command_string(data[0:-1].decode())
Example #11
0
 def _get_and_decode(self,
                     client) -> Generator[int, None, Tuple[str, dict]]:
     data = yield from client.send_queue.get()
     return decode_command_string(data[0:-1].decode())
Example #12
0
 def _get_and_decode(self, client) -> Tuple[str, dict]:
     data = client.send_queue.get_nowait()
     return decode_command_string(data[0:-1].decode())