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)
async def _send_init(self, **kwargs): self.client = MockQueueSocket(self.loop) await self.mock_server.add_client(self.client) # check hello cmd, args = await self._get_and_decode(self.client) self.assertEqual("hello", cmd) self._encode_and_send("start")
def test_virtual_pinball(self): # connect a client client = MockQueueSocket(self.loop) self.loop.run_until_complete(self.mock_server.add_client(client)) # check hello cmd, args = self.loop.run_until_complete(self._get_and_decode(client)) self.assertEqual("hello", cmd) self._encode_and_send(client, "monitor_start", category="switches") self._encode_and_send(client, "monitor_start", category="drivers") self._encode_and_send(client, "monitor_start", category="devices") self.advance_time_and_run() client.send_queue = asyncio.Queue() self.machine.lights["test_light1"].on() cmd, args = self.loop.run_until_complete(self._get_and_decode(client)) self.assertEqual("device", cmd) self.assertEqual("test_light1", args['name']) self.assertEqual("light", args['type']) self.assertEqual({'color': [255, 255, 255]}, args['state']) self.machine.coils["c_test"].pulse() self.advance_time_and_run() cmd, args = self.loop.run_until_complete(self._get_and_decode(client)) self.assertEqual("driver_event", cmd) self.assertEqual("pulse", args['action']) self.assertEqual("c_test", args['name']) self.assertEqual("0-0", args['number']) self.assertEqual(23, args['pulse_ms']) self.assertEqual(1.0, args['pulse_power']) self.machine.coils["c_test_allow_enable"].enable() cmd, args = self.loop.run_until_complete(self._get_and_decode(client)) self.assertEqual("driver_event", cmd) self.assertEqual("enable", args['action']) self.assertEqual("c_test_allow_enable", args['name']) self.assertEqual("0-1", args['number']) self.assertEqual(23, args['pulse_ms']) self.assertEqual(1.0, args['pulse_power']) self.assertEqual(1.0, args['hold_power']) self.machine.coils["c_test_allow_enable"].disable() cmd, args = self.loop.run_until_complete(self._get_and_decode(client)) self.assertEqual("driver_event", cmd) self.assertEqual("disable", args['action']) self.assertEqual("c_test_allow_enable", args['name']) self.assertEqual("0-1", args['number']) self.machine.flippers["f_test_single"].enable() cmd, args = self.loop.run_until_complete(self._get_and_decode(client)) self.assertEqual("device", cmd) self.assertEqual("f_test_single", args['name']) self.assertEqual("flipper", args['type']) self.assertEqual({"enabled": True}, args['state']) cmd, args = self.loop.run_until_complete(self._get_and_decode(client)) self.assertEqual("driver_event", cmd) self.assertEqual({'enable_switch_invert': False, 'coil_name': 'c_flipper_main', 'coil_pulse_power': 1.0, 'coil_hold_power': 'HoldSettings(power=0.375)', 'enable_switch_name': 's_flipper', 'enable_switch_number': '0-3', 'coil_pulse_ms': 10, 'coil_number': '0-3', 'action': 'pulse_on_hit_and_enable_and_release', 'coil_recycle': False, 'enable_switch_debounce': False}, args) self.machine.flippers["f_test_single"].disable() cmd, args = self.loop.run_until_complete(self._get_and_decode(client)) self.assertEqual("driver_event", cmd) self.assertEqual({'coil_number': '0-3', 'enable_switch_invert': False, 'action': 'remove', 'enable_switch_number': '0-3'}, args) cmd, args = self.loop.run_until_complete(self._get_and_decode(client)) self.assertEqual("device", cmd) self.assertEqual("f_test_single", args['name']) self.assertEqual("flipper", args['type']) self.assertEqual({"enabled": False}, args['state']) self.assertSwitchState("s_test_nc", 0) self._encode_and_send(client, "switch", name="s_test_nc", state=1) self.advance_time_and_run() self.assertSwitchState("s_test_nc", 1)
def _mock_loop(self): self.client_socket = MockQueueSocket() self.clock.mock_socket("localhost", 5050, self.client_socket)