def send_get_request(self): self.log_debug("send_get_request called with request: %s", self._request) try: self._server.register_get(self._id, self) endpoints = [self._block] + self.dict_to_path( self._request.toDict()) msg = Get(response_queue=self._server.q, endpoint=endpoints) msg.set_id(self._id) with self._lock: self._server.send_to_process(msg) self.wait_for_reply() self.log_debug( "send_get_request received the following response: %s", self._response) # Create the reply structure response_dict = self._response["value"] for ep in reversed(endpoints[1:]): response_dict = {ep: response_dict} self.log_debug("response_dict: %s", response_dict) self._pv_structure = self._server.dict_to_pv_object(response_dict) except Exception: self.log_exception("Unable to complete send_get_request: %s", self._request) self._server.remove_get(self._id)
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"]}')
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"]}' )
def test_MWSH_on_message(self, ioloop_mock, server_mock): MWSH = MalcWebSocketHandler(MagicMock(), MagicMock()) MWSH.servercomms = MagicMock() request = Get(None, None, ["block", "attr"]) request.set_id(54) message = """{ "typeid": "malcolm:core/Get:1.0", "id": 54, "endpoint": ["block", "attr"] }""" MWSH.on_message(message) self.assertEquals(MWSH.servercomms.on_request.call_count, 1) actual = MWSH.servercomms.on_request.call_args[0][0] self.assertEquals(actual.to_dict(), request.to_dict())
class TestGet(unittest.TestCase): def setUp(self): self.context = MagicMock() self.response_queue = MagicMock() self.endpoint = ["BL18I:XSPRESS3", "state", "value"] self.get = Get(self.context, self.response_queue, self.endpoint) def test_init(self): self.assertEqual(self.context, self.get.context) self.assertEqual(self.response_queue, self.get.response_queue) self.assertEqual(self.endpoint, self.get.endpoint) self.assertEqual("malcolm:core/Get:1.0", self.get.typeid) def test_setters(self): self.get.set_endpoint(["BL18I:XSPRESS3", "state", "value2"]) self.assertEquals(["BL18I:XSPRESS3", "state", "value2"], self.get.endpoint)
class TestGet(unittest.TestCase): def setUp(self): self.callback = MagicMock() self.path = ["BL18I:XSPRESS3", "state", "value"] self.o = Get(32, self.path) self.o.set_callback(self.callback) def test_init(self): assert self.o.typeid == "malcolm:core/Get:1.0" assert self.o.id == 32 assert self.o.callback == self.callback assert self.path == self.o.path def test_setters(self): self.o.path = ["BL18I:XSPRESS3"] assert get_doc_json("get_xspress3") == self.o.to_dict() def test_doc_state(self): assert get_doc_json("get_xspress3_state_value") == self.o.to_dict()
def send_get_request(self): self.log_debug("send_get_request called with request: %s", self._request) try: self._server.register_get(self._id, self) endpoints = [self._block] + self.dict_to_path(self._request.toDict()) msg = Get(response_queue=self._server.q, endpoint=endpoints) msg.set_id(self._id) with self._lock: self._server.send_to_process(msg) self.wait_for_reply() self.log_debug("send_get_request received the following response: %s", self._response) # Create the reply structure response_dict = self._response["value"] for ep in reversed(endpoints[1:]): response_dict = {ep: response_dict} self.log_debug("response_dict: %s", response_dict) self._pv_structure = self._server.dict_to_pv_object(response_dict) except Exception: self.log_exception("Unable to complete send_get_request: %s", self._request) self._server.remove_get(self._id)
def test_get(self): p = Process("proc", MagicMock()) block = MagicMock() block.name = "myblock" block.to_dict = MagicMock( return_value={"path_1": {"path_2": {"attr": "value"}}}) request = Get(MagicMock(), MagicMock(), ["myblock", "path_1", "path_2"]) p._handle_block_add(BlockAdd(block)) p.q.get = MagicMock(side_effect=[request, PROCESS_STOP]) p.recv_loop() response = request.response_queue.put.call_args[0][0] self.assertIsInstance(response, Return) self.assertEquals({"attr": "value"}, response.value)
def test_send_get_to_server(self): self.PVA = PvaClientComms(self.p) self.PVA.send_to_caller = MagicMock() request = Get(endpoint=["ep1", "ep2"]) self.PVA.send_to_server(request) pvaccess.Channel.assert_called_once() self.ch.get.assert_called_once() self.PVA.send_to_caller.assert_called_once() self.PVA.send_to_caller.reset_mock() self.ret_val.toDict = MagicMock(return_value={'typeid': 'test1'}) self.PVA.send_to_server(request) self.assertIsInstance(self.PVA.send_to_caller.call_args[0][0], Return) self.PVA.send_to_caller.reset_mock() self.ret_val.toDict = MagicMock( return_value={'typeid': 'malcolm:core/Error:1.0'}) self.PVA.send_to_server(request) self.assertIsInstance(self.PVA.send_to_caller.call_args[0][0], Error)
def setUp(self): self.context = MagicMock() self.response_queue = MagicMock() self.endpoint = ["BL18I:XSPRESS3", "state", "value"] self.get = Get(self.context, self.response_queue, self.endpoint)
def setUp(self): self.callback = MagicMock() self.path = ["BL18I:XSPRESS3", "state", "value"] self.o = Get(32, self.path) self.o.set_callback(self.callback)
def test_handle_request(self): q = Queue() request = Get(id=41, path=["mri", "myAttribute"], callback=q.put) self.o.handle_request(request) response = q.get(timeout=.1) self.assertIsInstance(response, Return) assert response.id == 41 assert response.value["value"] == "hello_block" # It's part2 that will get the attribute as it was defined second self.part2.myAttribute.meta.writeable = False request = Put(id=42, path=["mri", "myAttribute"], value='hello_block', callback=q.put) self.o.handle_request(request) response = q.get(timeout=.1) self.assertIsInstance(response, Error) # not writeable assert response.id == 42 self.part2.myAttribute.meta.writeable = True self.o.handle_request(request) response = q.get(timeout=.1) self.assertIsInstance(response, Return) assert response.id == 42 assert response.value == "hello_block" request = Post(id=43, path=["mri", "my_method"], callback=q.put) self.o.handle_request(request) response = q.get(timeout=.1) self.assertIsInstance(response, Return) assert response.id == 43 assert response.value['ret'] == "world" # cover the controller._handle_post path for parameters request = Post(id=43, path=["mri", "my_method"], parameters={'dummy': 1}, callback=q.put) self.o.handle_request(request) response = q.get(timeout=.1) self.assertIsInstance(response, Return) assert response.id == 43 assert response.value['ret'] == "world" request = Subscribe(id=44, path=["mri", "myAttribute"], delta=False, callback=q.put) self.o.handle_request(request) response = q.get(timeout=.1) self.assertIsInstance(response, Update) assert response.id == 44 assert response.value["typeid"] == "epics:nt/NTScalar:1.0" assert response.value["value"] == "hello_block" request = Unsubscribe(id=44, callback=q.put) self.o.handle_request(request) response = q.get(timeout=.1) self.assertIsInstance(response, Return) assert response.id == 44