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)
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 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)
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)
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)
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)
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)
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)
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()
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()
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()
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)
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()
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)
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 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)
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)
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)
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)
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()
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()
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)
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)
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()
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)
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()
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)
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"))
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"))
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)
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)
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)
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()
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()