コード例 #1
0
    def test_on_message_logs_exception(self, _, _1, json_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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)
コード例 #2
0
 def setUp(self):
     super(TestSystemWSCommsServerAndClient, self).setUp()
     self.process2 = Process("proc2", self.sf)
     self.block2 = Block()
     ClientController(self.process2, self.block2, 'hello')
     self.cc = WSClientComms("cc", self.process2,
                             "ws://localhost:%s/ws" % self.socket)
     self.process2.start()
     self.cc.start()
コード例 #3
0
 def test_subscribe_initial(self, _, _2):
     self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
     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)
コード例 #4
0
    def test_start(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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)
コード例 #5
0
    def test_wait(self, _, _2):
        spawnable_mocks = [MagicMock(), MagicMock()]
        timeout = MagicMock()

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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)
コード例 #6
0
    def test_init(self, ioloop_mock, connect_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

        self.assertEqual(self.p, self.WS.process)
        self.assertEqual("test/url", self.WS.url)
        self.assertEqual(ioloop_mock.current(), self.WS.loop)
        connect_mock.assert_called_once_with(
            self.WS.url,
            callback=self.WS.subscribe_server_blocks,
            on_message_callback=self.WS.on_message)
        self.assertEqual(connect_mock(), self.WS.conn)
コード例 #7
0
    def test_init(self, ioloop_mock, connect_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

        self.assertEqual("TestWebSocket", self.WS.name)
        self.assertEqual(self.p, self.WS.process)
        self.assertEqual("test/url", self.WS.url)
        self.assertEqual(ioloop_mock.current(), self.WS.loop)
        connect_mock.assert_called_once_with(
            self.WS.url, on_message_callback=self.WS.on_message)
        self.assertEqual(connect_mock(), self.WS.conn)
        self.assertIsNone(self.WS._loop_spawned)
コード例 #8
0
    def test_start(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        spawn_mock = MagicMock()
        self.WS.process.spawn.return_value = spawn_mock

        self.WS.start_recv_loop()

        self.WS.process.spawn.assert_called_once_with(loop_mock.start)
        self.assertEqual(spawn_mock, self.WS._loop_spawned)
コード例 #9
0
    def test_stop(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        self.WS._loop_spawned = MagicMock()
        self.WS.stop_recv_loop()

        loop_mock.add_callback.assert_called_once_with(
            ioloop_mock.current().stop)
        self.WS._loop_spawned.wait.assert_called_once_with()
コード例 #10
0
    def test_stop(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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()
コード例 #11
0
 def test_server_with_malcolm_client(self):
     self.cc = WSClientComms("cc", self.process, "ws://localhost:8888/ws")
     self.cc.start()
     # Wait for comms to be connected
     while not self.cc.conn.done():
         time.sleep(0.001)
     # Don't add to process as we already have a block of that name
     block2 = Block("hello")
     ClientController(self.process, block2, self.cc)
     ret = block2.say_hello("me2")
     self.assertEqual(ret, dict(greeting="Hello me2"))
     self.cc.stop()
コード例 #12
0
    def test_send_to_server(self, _, connect_mock, json_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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)
コード例 #13
0
class TestSystemWSComms(unittest.TestCase):
    def setUp(self):
        sync_factory = SyncFactory("sync")
        self.process = Process("proc", sync_factory)
        block = Block("hello")
        self.process.add_block(block)
        HelloController(block)
        self.sc = WSServerComms("sc", self.process, 8888)
        self.process.start()
        self.sc.start()

    def tearDown(self):
        self.sc.stop()
        self.process.stop()

    @gen.coroutine
    def send_message(self):
        conn = yield websocket_connect("ws://localhost:8888/ws")
        req = dict(
            type="Post",
            id=0,
            endpoint=["hello", "say_hello"],
            parameters=dict(
                name="me"
            )
        )
        conn.write_message(json.dumps(req))
        resp = yield conn.read_message()
        resp = json.loads(resp)
        self.assertEqual(resp, dict(
            id=0,
            type="Return",
            value=dict(
                greeting="Hello me"
            )
        ))
        conn.close()

    def test_server_and_simple_client(self):
        self.send_message()

    def test_server_with_malcolm_client(self):
        self.cc = WSClientComms("cc", self.process, "ws://localhost:8888/ws")
        self.cc.start()
        # Wait for comms to be connected
        while not self.cc.conn.done():
            time.sleep(0.001)
        # Don't add to process as we already have a block of that name
        block2 = Block("hello")
        ClientController(self.process, block2, self.cc)
        ret = block2.say_hello("me2")
        self.assertEqual(ret, dict(greeting="Hello me2"))
        self.cc.stop()
コード例 #14
0
    def test_init(self, ioloop_mock, connect_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

        self.assertEqual(self.p, self.WS.process)
        self.assertEqual("test/url", self.WS.url)
        self.assertEqual(ioloop_mock.current(), self.WS.loop)
        connect_mock.assert_called_once_with(self.WS.url, callback=self.WS.subscribe_server_blocks, on_message_callback=self.WS.on_message)
        self.assertEqual(connect_mock(), self.WS.conn)
コード例 #15
0
 def setUp(self):
     super(TestSystemWSCommsServerAndClient, self).setUp()
     self.process2 = Process("proc2", self.sf)
     self.block2 = Block()
     ClientController(self.process2, self.block2, 'hello')
     self.cc = WSClientComms("cc", self.process2, "ws://localhost:%s/ws" %
                             self.socket)
     self.process2.start()
     self.cc.start()
コード例 #16
0
    def test_on_message(self, _, _1, json_mock, response_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

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

        response = MagicMock()
        response.id_ = 1
        response_mock.from_dict.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)
        response_mock.from_dict.assert_called_once_with(message_dict)
        request_mock.response_queue.put.assert_called_once_with(response)
コード例 #17
0
    def test_start(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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)
コード例 #18
0
 def test_subscribe_initial(self, _, _2):
     self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
     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)
コード例 #19
0
    def test_on_message_logs_exception(self, _, _1, json_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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)
コード例 #20
0
class TestSystemWSComms(unittest.TestCase):
    def setUp(self):
        sync_factory = SyncFactory("sync")
        self.process = Process("proc", sync_factory)
        block = Block("hello")
        self.process.add_block(block)
        HelloController(block)
        self.sc = WSServerComms("sc", self.process, 8888)
        self.process.start()
        self.sc.start()

    def tearDown(self):
        self.sc.stop()
        self.process.stop()

    @gen.coroutine
    def send_message(self):
        conn = yield websocket_connect("ws://localhost:8888/ws")
        req = dict(type="Post",
                   id=0,
                   endpoint=["hello", "say_hello"],
                   parameters=dict(name="me"))
        conn.write_message(json.dumps(req))
        resp = yield conn.read_message()
        resp = json.loads(resp)
        self.assertEqual(
            resp, dict(id=0, type="Return", value=dict(greeting="Hello me")))
        conn.close()

    def test_server_and_simple_client(self):
        self.send_message()

    def test_server_with_malcolm_client(self):
        self.cc = WSClientComms("cc", self.process, "ws://localhost:8888/ws")
        self.cc.start()
        # Wait for comms to be connected
        while not self.cc.conn.done():
            time.sleep(0.001)
        # Don't add to process as we already have a block of that name
        block2 = Block("hello")
        ClientController(self.process, block2, self.cc)
        ret = block2.say_hello("me2")
        self.assertEqual(ret, dict(greeting="Hello me2"))
        self.cc.stop()
コード例 #21
0
    def test_stop(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        self.WS._loop_spawned = MagicMock()
        self.WS.stop_recv_loop()

        loop_mock.add_callback.assert_called_once_with(ioloop_mock.current().stop)
        self.WS._loop_spawned.wait.assert_called_once_with()
コード例 #22
0
    def test_init(self, ioloop_mock, connect_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

        self.assertEqual("TestWebSocket", self.WS.name)
        self.assertEqual(self.p, self.WS.process)
        self.assertEqual("test/url", self.WS.url)
        self.assertEqual(ioloop_mock.current(), self.WS.loop)
        connect_mock.assert_called_once_with(self.WS.url, on_message_callback=self.WS.on_message)
        self.assertEqual(connect_mock(), self.WS.conn)
        self.assertIsNone(self.WS._loop_spawned)
コード例 #23
0
    def test_wait(self, _, _2):
        spawnable_mocks = [MagicMock(), MagicMock()]
        timeout = MagicMock()

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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)
コード例 #24
0
    def test_stop(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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()
コード例 #25
0
    def test_start(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        spawn_mock = MagicMock()
        self.WS.process.spawn.return_value = spawn_mock

        self.WS.start_recv_loop()

        self.WS.process.spawn.assert_called_once_with(loop_mock.start)
        self.assertEqual(spawn_mock, self.WS._loop_spawned)
コード例 #26
0
 def test_server_with_malcolm_client(self):
     self.cc = WSClientComms("cc", self.process, "ws://localhost:8888/ws")
     self.cc.start()
     # Wait for comms to be connected
     while not self.cc.conn.done():
         time.sleep(0.001)
     # Don't add to process as we already have a block of that name
     block2 = Block("hello")
     ClientController(self.process, block2, self.cc)
     ret = block2.say_hello("me2")
     self.assertEqual(ret, dict(greeting="Hello me2"))
     self.cc.stop()
コード例 #27
0
    def test_send_to_server(self, _, connect_mock, json_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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)
コード例 #28
0
class TestSystemWSCommsServerAndClient(TestSystemWSCommsServerOnly):
    socket = 8882

    def setUp(self):
        super(TestSystemWSCommsServerAndClient, self).setUp()
        self.process2 = Process("proc2", self.sf)
        self.block2 = Block()
        ClientController(self.process2, self.block2, 'hello')
        self.cc = WSClientComms("cc", self.process2,
                                "ws://localhost:%s/ws" % self.socket)
        self.process2.start()
        self.cc.start()

    def tearDown(self):
        super(TestSystemWSCommsServerAndClient, self).tearDown()
        self.cc.stop()
        self.cc.wait()
        self.process2.stop()

    def test_server_with_malcolm_client(self):
        # Normally we would wait for it to be connected here, but it isn't
        # attached to a process so just sleep for a bit
        time.sleep(0.5)
        ret = self.block2.say_hello("me2")
        self.assertEqual(ret, dict(greeting="Hello me2"))
コード例 #29
0
class TestSystemWSCommsServerAndClient(TestSystemWSCommsServerOnly):
    socket = 8882

    def setUp(self):
        super(TestSystemWSCommsServerAndClient, self).setUp()
        self.process2 = Process("proc2", self.sf)
        self.block2 = Block()
        ClientController(self.process2, self.block2, 'hello')
        self.cc = WSClientComms("cc", self.process2, "ws://localhost:%s/ws" %
                                self.socket)
        self.process2.start()
        self.cc.start()

    def tearDown(self):
        super(TestSystemWSCommsServerAndClient, self).tearDown()
        self.cc.stop()
        self.cc.wait()
        self.process2.stop()

    def test_server_with_malcolm_client(self):
        # Normally we would wait for it to be connected here, but it isn't
        # attached to a process so just sleep for a bit
        time.sleep(0.5)
        ret = self.block2.say_hello("me2")
        self.assertEqual(ret, dict(greeting="Hello me2"))
コード例 #30
0
    def test_on_message(self, _, _1, json_mock, response_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

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

        response = MagicMock()
        response.id_ = 1
        response_mock.from_dict.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)
        response_mock.from_dict.assert_called_once_with(message_dict)
        request_mock.response_queue.put.assert_called_once_with(response)
コード例 #31
0
class TestWSClientComms(unittest.TestCase):

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

    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_init(self, ioloop_mock, connect_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

        self.assertEqual(self.p, self.WS.process)
        self.assertEqual("test/url", self.WS.url)
        self.assertEqual(ioloop_mock.current(), self.WS.loop)
        connect_mock.assert_called_once_with(self.WS.url, callback=self.WS.subscribe_server_blocks, on_message_callback=self.WS.on_message)
        self.assertEqual(connect_mock(), self.WS.conn)

    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_subscribe_initial(self, _, _2):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.Serializable')
    @patch('malcolm.wscomms.wsclientcomms.json')
    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_on_message(self, _, _1, json_mock, serializable_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

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

        response = MagicMock()
        response.id_ = 1
        serializable_mock.from_dict.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)
        serializable_mock.from_dict.assert_called_once_with(message_dict)
        request_mock.response_queue.put.assert_called_once_with(response)

    @patch('malcolm.wscomms.wsclientcomms.json')
    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_on_message_logs_exception(self, _, _1, json_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.json')
    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_send_to_server(self, _, connect_mock, json_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_start(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_stop(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_wait(self, _, _2):
        spawnable_mocks = [MagicMock(), MagicMock()]
        timeout = MagicMock()

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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)
コード例 #32
0
class TestWSClientComms(unittest.TestCase):
    def setUp(self):
        self.p = MagicMock()

    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_init(self, ioloop_mock, connect_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

        self.assertEqual(self.p, self.WS.process)
        self.assertEqual("test/url", self.WS.url)
        self.assertEqual(ioloop_mock.current(), self.WS.loop)
        connect_mock.assert_called_once_with(
            self.WS.url,
            callback=self.WS.subscribe_server_blocks,
            on_message_callback=self.WS.on_message)
        self.assertEqual(connect_mock(), self.WS.conn)

    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_subscribe_initial(self, _, _2):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.Serializable')
    @patch('malcolm.wscomms.wsclientcomms.json')
    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_on_message(self, _, _1, json_mock, serializable_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

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

        response = MagicMock()
        response.id_ = 1
        serializable_mock.from_dict.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)
        serializable_mock.from_dict.assert_called_once_with(message_dict)
        request_mock.response_queue.put.assert_called_once_with(response)

    @patch('malcolm.wscomms.wsclientcomms.json')
    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_on_message_logs_exception(self, _, _1, json_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.json')
    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_send_to_server(self, _, connect_mock, json_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_start(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_stop(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_wait(self, _, _2):
        spawnable_mocks = [MagicMock(), MagicMock()]
        timeout = MagicMock()

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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)
コード例 #33
0
class TestWSServerComms(unittest.TestCase):

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

    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_init(self, ioloop_mock, connect_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

        self.assertEqual("TestWebSocket", self.WS.name)
        self.assertEqual(self.p, self.WS.process)
        self.assertEqual("test/url", self.WS.url)
        self.assertEqual(ioloop_mock.current(), self.WS.loop)
        connect_mock.assert_called_once_with(self.WS.url, on_message_callback=self.WS.on_message)
        self.assertEqual(connect_mock(), self.WS.conn)
        self.assertIsNone(self.WS._loop_spawned)

    @patch('malcolm.wscomms.wsclientcomms.Response')
    @patch('malcolm.wscomms.wsclientcomms.json')
    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_on_message(self, _, _1, json_mock, response_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

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

        response = MagicMock()
        response.id_ = 1
        response_mock.from_dict.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)
        response_mock.from_dict.assert_called_once_with(message_dict)
        request_mock.response_queue.put.assert_called_once_with(response)

    @patch('malcolm.wscomms.wsclientcomms.json')
    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_send_to_server(self, _, connect_mock, json_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_start(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        spawn_mock = MagicMock()
        self.WS.process.spawn.return_value = spawn_mock

        self.WS.start_recv_loop()

        self.WS.process.spawn.assert_called_once_with(loop_mock.start)
        self.assertEqual(spawn_mock, self.WS._loop_spawned)

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

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        self.WS._loop_spawned = MagicMock()
        self.WS.stop_recv_loop()

        loop_mock.add_callback.assert_called_once_with(ioloop_mock.current().stop)
        self.WS._loop_spawned.wait.assert_called_once_with()
コード例 #34
0
class TestWSServerComms(unittest.TestCase):
    def setUp(self):
        self.p = MagicMock()

    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_init(self, ioloop_mock, connect_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

        self.assertEqual("TestWebSocket", self.WS.name)
        self.assertEqual(self.p, self.WS.process)
        self.assertEqual("test/url", self.WS.url)
        self.assertEqual(ioloop_mock.current(), self.WS.loop)
        connect_mock.assert_called_once_with(
            self.WS.url, on_message_callback=self.WS.on_message)
        self.assertEqual(connect_mock(), self.WS.conn)
        self.assertIsNone(self.WS._loop_spawned)

    @patch('malcolm.wscomms.wsclientcomms.Response')
    @patch('malcolm.wscomms.wsclientcomms.json')
    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_on_message(self, _, _1, json_mock, response_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")

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

        response = MagicMock()
        response.id_ = 1
        response_mock.from_dict.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)
        response_mock.from_dict.assert_called_once_with(message_dict)
        request_mock.response_queue.put.assert_called_once_with(response)

    @patch('malcolm.wscomms.wsclientcomms.json')
    @patch('malcolm.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_send_to_server(self, _, connect_mock, json_mock):
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        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.wscomms.wsclientcomms.websocket_connect')
    @patch('malcolm.wscomms.wsclientcomms.IOLoop')
    def test_start(self, ioloop_mock, _):
        loop_mock = MagicMock()
        ioloop_mock.current.return_value = loop_mock
        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        spawn_mock = MagicMock()
        self.WS.process.spawn.return_value = spawn_mock

        self.WS.start_recv_loop()

        self.WS.process.spawn.assert_called_once_with(loop_mock.start)
        self.assertEqual(spawn_mock, self.WS._loop_spawned)

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

        self.WS = WSClientComms("TestWebSocket", self.p, "test/url")
        self.WS._loop_spawned = MagicMock()
        self.WS.stop_recv_loop()

        loop_mock.add_callback.assert_called_once_with(
            ioloop_mock.current().stop)
        self.WS._loop_spawned.wait.assert_called_once_with()