class TestWSClientComms(unittest.TestCase):
    def setUp(self):
        self.p = MagicMock()

    @patch('malcolm.comms.websocket.websocketclientcomms.websocket_connect')
    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_init(self, ioloop_mock, connect_mock):
        self.WS = WebsocketClientComms(self.p, params)

        self.assertEqual(self.p, self.WS.process)
        self.assertEqual("ws://*****:*****@patch('malcolm.comms.websocket.websocketclientcomms.websocket_connect')
    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_subscribe_initial(self, _, _2):
        self.WS = WebsocketClientComms(self.p, params)
        self.WS.subscribe_server_blocks("conn")
        self.assertEqual(self.WS.loop.add_callback.call_count, 1)
        request = self.WS.loop.add_callback.call_args[0][1]
        self.assertEqual(request.id, 0)
        self.assertEqual(request.typeid, "malcolm:core/Subscribe:1.0")
        self.assertEqual(request.endpoint, [".", "blocks", "value"])
        self.assertEqual(request.delta, False)

    @patch('malcolm.comms.websocket.websocketclientcomms.deserialize_object')
    @patch('malcolm.comms.websocket.websocketclientcomms.json')
    @patch('malcolm.comms.websocket.websocketclientcomms.websocket_connect')
    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_on_message(self, _, _1, json_mock, deserialize_mock):
        self.WS = WebsocketClientComms(self.p, params)

        message_dict = dict(name="TestMessage")
        json_mock.loads.return_value = message_dict

        response = MagicMock()
        response.id = 1
        deserialize_mock.return_value = response
        request_mock = MagicMock()
        self.WS.requests[1] = request_mock

        self.WS.on_message("TestMessage")

        json_mock.loads.assert_called_once_with("TestMessage",
                                                object_pairs_hook=OrderedDict)
        deserialize_mock.assert_called_once_with(message_dict, Response)
        request_mock.response_queue.put.assert_called_once_with(response)

    @patch('malcolm.comms.websocket.websocketclientcomms.json')
    @patch('malcolm.comms.websocket.websocketclientcomms.websocket_connect')
    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_on_message_logs_exception(self, _, _1, json_mock):
        self.WS = WebsocketClientComms(self.p, params)
        self.WS.log_exception = MagicMock()
        exception = Exception()
        json_mock.loads.side_effect = exception

        self.WS.on_message("test")

        self.WS.log_exception.assert_called_once_with(exception)

    @patch('malcolm.comms.websocket.websocketclientcomms.json')
    @patch('malcolm.comms.websocket.websocketclientcomms.websocket_connect')
    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_send_to_server(self, _, connect_mock, json_mock):
        self.WS = WebsocketClientComms(self.p, params)
        json_mock.reset_mock()
        result_mock = MagicMock()
        connect_mock().result.return_value = result_mock
        dumps_mock = MagicMock()
        json_mock.dumps.return_value = dumps_mock

        request_mock = MagicMock()
        self.WS.send_to_server(request_mock)

        json_mock.dumps.assert_called_once_with(request_mock.to_dict())
        result_mock.write_message.assert_called_once_with(dumps_mock)

    @patch('malcolm.comms.websocket.websocketclientcomms.websocket_connect')
    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_start(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock
        self.WS = WebsocketClientComms(self.p, params)
        self.WS.process.spawn = MagicMock()
        self.WS.start()

        self.assertEqual([call(self.WS.send_loop),
                          call(self.WS.loop.start)],
                         self.WS.process.spawn.call_args_list)

    @patch('malcolm.comms.websocket.websocketclientcomms.websocket_connect')
    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_stop(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock

        self.WS = WebsocketClientComms(self.p, params)
        self.WS.start()
        self.WS.stop()

        loop_mock.add_callback.assert_called_once_with(
            ioloop_mock.current().stop)
        self.WS.process.spawn.return_value.assert_not_called()

    @patch('malcolm.comms.websocket.websocketclientcomms.websocket_connect')
    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_wait(self, _, _2):
        spawnable_mocks = [MagicMock(), MagicMock()]
        timeout = MagicMock()

        self.WS = WebsocketClientComms(self.p, params)
        self.WS.process.spawn = MagicMock(side_effect=spawnable_mocks)
        self.WS.start()
        self.WS.wait(timeout)

        spawnable_mocks[0].wait.assert_called_once_with(timeout=timeout)
        spawnable_mocks[1].wait.assert_called_once_with(timeout=timeout)
Ejemplo n.º 2
0
class TestWSClientComms(unittest.TestCase):
    def setUp(self):
        self.p = MagicMock()

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_init(self, ioloop_mock):
        self.WS = WebsocketClientComms(self.p, params)
        self.assertEqual(self.p, self.WS.process)
        self.assertEqual("ws://*****:*****@patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_subscribe_initial(self, _):
        self.WS = WebsocketClientComms(self.p, params)
        self.WS.send_to_server = MagicMock()
        self.WS.subscribe_server_blocks()
        self.assertEqual(self.WS.send_to_server.call_count, 1)
        request = self.WS.send_to_server.call_args[0][0]
        self.assertEqual(request.id, 0)
        self.assertEqual(request.typeid, "malcolm:core/Subscribe:1.0")
        self.assertEqual(request.endpoint, [".", "blocks", "value"])
        self.assertEqual(request.delta, False)

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_on_message(self, _):
        self.WS = WebsocketClientComms(self.p, params)
        request = MagicMock()
        self.WS.requests[11] = request
        response = Return(11, MagicMock(), "me")
        message = """{
        "typeid": "malcolm:core/Return:1.0",
        "id": 11,
        "value": "me"
        }"""
        self.WS.on_message(message)
        self.assertEquals(request.response_queue.put.call_count, 1)
        actual = request.response_queue.put.call_args[0][0]
        self.assertEquals(actual.to_dict(), response.to_dict())

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_on_message_logs_exception(self, _):
        self.WS = WebsocketClientComms(self.p, params)
        self.WS.log_exception = MagicMock()
        self.WS.on_message("test")
        self.WS.log_exception.assert_called_once_with('on_message(%r) failed',
                                                      "test")

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_send_to_server(self, _):
        self.WS = WebsocketClientComms(self.p, params)
        self.WS.conn = MagicMock()
        request = Get(None, None, ["block", "attr"])
        request.set_id(54)
        self.WS.send_to_server(request)
        self.WS.conn.write_message.assert_called_once_with(
            '{"typeid": "malcolm:core/Get:1.0", "id": 54, "endpoint": ["block", "attr"]}'
        )

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_start(self, ioloop_mock):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock
        self.WS = WebsocketClientComms(self.p, params)
        self.WS.process.spawn = MagicMock()
        self.WS.start()

        self.assertEqual([call(self.WS.send_loop),
                          call(self.WS.loop.start)],
                         self.WS.process.spawn.call_args_list)

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_stop(self, ioloop_mock):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock

        self.WS = WebsocketClientComms(self.p, params)
        self.WS.start()
        loop_mock.reset_mock()
        self.WS.stop()

        loop_mock.add_callback.assert_called_once_with(
            ioloop_mock.current().stop)
        self.WS.process.spawn.return_value.assert_not_called()

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_wait(self, _):
        spawnable_mocks = [MagicMock(), MagicMock()]
        timeout = MagicMock()

        self.WS = WebsocketClientComms(self.p, params)
        self.WS.process.spawn = MagicMock(side_effect=spawnable_mocks)
        self.WS.start()
        self.WS.wait(timeout)

        spawnable_mocks[0].wait.assert_called_once_with(timeout=timeout)
        spawnable_mocks[1].wait.assert_called_once_with(timeout=timeout)
class TestWSClientComms(unittest.TestCase):

    def setUp(self):
        self.p = MagicMock()

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_init(self, ioloop_mock):
        self.WS = WebsocketClientComms(self.p, params)
        self.assertEqual(self.p, self.WS.process)
        self.assertEqual("ws://*****:*****@patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_subscribe_initial(self, _):
        self.WS = WebsocketClientComms(self.p, params)
        self.WS.send_to_server = MagicMock()
        self.WS.subscribe_server_blocks()
        self.assertEqual(self.WS.send_to_server.call_count, 1)
        request = self.WS.send_to_server.call_args[0][0]
        self.assertEqual(request.id, 0)
        self.assertEqual(request.typeid, "malcolm:core/Subscribe:1.0")
        self.assertEqual(request.endpoint, [".", "blocks", "value"])
        self.assertEqual(request.delta, False)

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_on_message(self, _):
        self.WS = WebsocketClientComms(self.p, params)
        request = MagicMock()
        self.WS.requests[11] = request
        response = Return(11, MagicMock(), "me")
        message = """{
        "typeid": "malcolm:core/Return:1.0",
        "id": 11,
        "value": "me"
        }"""
        self.WS.on_message(message)
        self.assertEquals(request.response_queue.put.call_count, 1)
        actual = request.response_queue.put.call_args[0][0]
        self.assertEquals(actual.to_dict(), response.to_dict())

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_on_message_logs_exception(self, _):
        self.WS = WebsocketClientComms(self.p, params)
        self.WS.log_exception = MagicMock()
        self.WS.on_message("test")
        self.WS.log_exception.assert_called_once_with(
            'on_message(%r) failed', "test")

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_send_to_server(self, _):
        self.WS = WebsocketClientComms(self.p, params)
        self.WS.conn = MagicMock()
        request = Get(None, None, ["block", "attr"])
        request.set_id(54)
        self.WS.send_to_server(request)
        self.WS.conn.write_message.assert_called_once_with(
            '{"typeid": "malcolm:core/Get:1.0", "id": 54, "endpoint": ["block", "attr"]}')

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_start(self, ioloop_mock):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock
        self.WS = WebsocketClientComms(self.p, params)
        self.WS.process.spawn = MagicMock()
        self.WS.start()

        self.assertEqual([call(self.WS.send_loop), call(self.WS.loop.start)],
                         self.WS.process.spawn.call_args_list)

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_stop(self, ioloop_mock):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock

        self.WS = WebsocketClientComms(self.p, params)
        self.WS.start()
        loop_mock.reset_mock()
        self.WS.stop()

        loop_mock.add_callback.assert_called_once_with(
            ioloop_mock.current().stop)
        self.WS.process.spawn.return_value.assert_not_called()

    @patch('malcolm.comms.websocket.websocketclientcomms.IOLoop')
    def test_wait(self, _):
        spawnable_mocks = [MagicMock(), MagicMock()]
        timeout = MagicMock()

        self.WS = WebsocketClientComms(self.p, params)
        self.WS.process.spawn = MagicMock(side_effect=spawnable_mocks)
        self.WS.start()
        self.WS.wait(timeout)

        spawnable_mocks[0].wait.assert_called_once_with(timeout=timeout)
        spawnable_mocks[1].wait.assert_called_once_with(timeout=timeout)