class TestSystemWSCommsServerOnly(unittest.TestCase): socket = 8881 def setUp(self): self.sf = SyncFactory("sync") self.process = Process("proc", self.sf) block = Block() HelloController(self.process, block, 'hello') self.sc = WSServerComms("sc", self.process, self.socket) self.process.start() self.sc.start() def tearDown(self): self.sc.stop() self.sc.wait() self.process.stop() @gen.coroutine def send_message(self): conn = yield websocket_connect("ws://localhost:%s/ws" % self.socket) 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()
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()
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()
class TestSystemWSCommsServerOnly(unittest.TestCase): socket = 8881 def setUp(self): self.sf = SyncFactory("sync") self.process = Process("proc", self.sf) block = Block() HelloController(self.process, block,'hello') self.sc = WSServerComms("sc", self.process, self.socket) self.process.start() self.sc.start() def tearDown(self): self.sc.stop() self.sc.wait() self.process.stop() @gen.coroutine def send_message(self): conn = yield websocket_connect("ws://localhost:%s/ws" % self.socket) 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()
class TestWSServerComms(unittest.TestCase): def setUp(self): self.p = MagicMock() @patch('malcolm.wscomms.wsservercomms.HTTPServer') @patch('malcolm.wscomms.wsservercomms.IOLoop') def test_init(self, ioloop_mock, server_mock): self.WS = WSServerComms("TestWebSocket", self.p, 1) self.assertEqual("TestWebSocket", self.WS.name) self.assertEqual(self.p, self.WS.process) self.assertEqual(server_mock(), self.WS.server) self.assertEqual(ioloop_mock.current(), self.WS.loop) @patch('malcolm.wscomms.wsservercomms.HTTPServer.listen') @patch('malcolm.wscomms.wsservercomms.IOLoop') def test_listen_called(self, ioloop_mock, listen_mock): self.WS = WSServerComms("TestWebSocket", self.p, 1) self.assertEqual(ioloop_mock.current(), self.WS.loop) @patch('malcolm.wscomms.wsservercomms.HTTPServer.listen') @patch('malcolm.wscomms.wsservercomms.IOLoop') def test_start(self, _, _2): self.p.spawn = MagicMock() self.WS = WSServerComms("TestWebSocket", self.p, 1) self.WS.start() self.assertEqual([call(self.WS.send_loop), call(self.WS.loop.start)], self.p.spawn.call_args_list) @patch('malcolm.wscomms.wsservercomms.HTTPServer') @patch('malcolm.wscomms.wsservercomms.IOLoop') def test_stop(self, ioloop_mock, server_mock): loop_mock = MagicMock() ioloop_mock.current.return_value = loop_mock self.p.spawn = MagicMock() self.WS = WSServerComms("TestWebSocket", self.p, 1) self.WS.start() self.WS.stop() self.assertEqual([call(self.WS.server.stop), call(self.WS.loop.stop)], loop_mock.add_callback.call_args_list) self.p.spawn.return_value.wait.assert_not_called() @patch('malcolm.wscomms.wsservercomms.HTTPServer') @patch('malcolm.wscomms.wsservercomms.IOLoop') def test_wait(self, ioloop_mock, server_mock): spawnable_mocks = [MagicMock(), MagicMock()] timeout = MagicMock() self.p.spawn = MagicMock(side_effect=spawnable_mocks) self.WS = WSServerComms("TestWebSocket", self.p, 1) 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) @patch('malcolm.wscomms.wsservercomms.Serializable') @patch('malcolm.wscomms.wsservercomms.json') @patch('malcolm.wscomms.wsservercomms.HTTPServer.listen') @patch('malcolm.wscomms.wsservercomms.IOLoop') def test_MWSH_on_message(self, _, _1, json_mock, serializable_mock): self.WS = WSServerComms("TestWebSocket", self.p, 1) message_dict = dict(name="TestMessage") json_mock.loads.return_value = message_dict request = MagicMock() request.context = self.WS.server.request_callback.handlers[0][1][0].handler_class serializable_mock.from_dict.return_value = request m = MagicMock() MWSH = MalcolmWebSocketHandler(m, m) self.WS.server.request_callback.handlers[0][1][0].handler_class.on_message( MWSH, "TestMessage") json_mock.loads.assert_called_once_with("TestMessage", object_pairs_hook=OrderedDict) serializable_mock.from_dict.assert_called_once_with(message_dict) self.p.q.put.assert_called_once_with(request) @patch('malcolm.wscomms.wsservercomms.HTTPServer') @patch('malcolm.wscomms.wsservercomms.IOLoop') def test_on_request_with_process_name(self, _, _2): self.WS = WSServerComms("ws", self.p, 1) request = MagicMock(fields=dict(endpoint="anything"), endpoint=[".", "blocks"]) self.WS.on_request(request) self.p.q.put.assert_called_once_with(request) self.assertEqual(request.endpoint, [self.p.name, "blocks"]) @patch('malcolm.wscomms.wsservercomms.json') @patch('malcolm.wscomms.wsservercomms.HTTPServer.listen') @patch('malcolm.wscomms.wsservercomms.IOLoop') def test_send_to_client(self, _, _2, json_mock): self.WS = WSServerComms("TestWebSocket", self.p, 1) response_mock = MagicMock() self.WS.send_to_client(response_mock) json_mock.dumps.assert_called_once_with(response_mock.to_dict()) response_mock.context.write_message.assert_called_once_with( json_mock.dumps())