Esempio n. 1
0
 def setUp(self):
     self.callback = MagicMock()
     self.path = ["BL18I:XSPRESS3", "configure"]
     self.parameters = OrderedDict()
     self.parameters["filePath"] = "/path/to/file.h5"
     self.parameters["exposure"] = 0.1
     self.o = Post(2, self.path, self.parameters, self.callback)
Esempio n. 2
0
    def put_async(self, attr_or_items, value=None):
        """"puts a value or values into an attribute or attributes and returns
            immediately

            Args:
                attr_or_items (Attribute or Dict): The attribute or dictionary
                    of {attributes: values} to set
                value (object): For single attr, the value set

            Returns:
                 a list of futures to monitor when each put completes"""
        if value:
            attr_or_items = {attr_or_items: value}
        result_f = []

        for attr, value in attr_or_items.items():
            endpoint = [attr.parent.name, attr.name, "value"]
            request = Post(None, self.q, endpoint, value)
            f = Future(self)
            new_id = self._save_future(f)
            request.set_id(new_id)
            self.process.q.put(request)
            result_f.append(f)

        return result_f
Esempio n. 3
0
    def setUp(self):
        self.context = MagicMock()
        self.response_queue = MagicMock()
        self.endpoint = ["BL18I:XSPRESS3", "state", "value"]
        self.parameters = dict(arg1=5, arg2=True)

        self.post = Post(self.context, self.response_queue, self.endpoint, self.parameters)
Esempio n. 4
0
    def execute(self, args):
        self.log_debug("Execute %s method called on [%s] with: %s", self._method, self._block, args)
        self.log_debug("Structure: %s", args.getStructureDict())
        # Acquire the lock
        with self._lock:
            # We now need to create the Post message and execute it
            endpoint = [self._block, self._method]
            request = Post(None, self._server.q, endpoint, self.parse_variants(args.toDict(True)))
            request.set_id(self._id)
            self._server.process.q.put(request)

            # Now wait for the Post reply
            self.log_debug("Waiting for reply")
            self.wait_for_reply()
            self.log_debug("Reply received")
            response_dict = OrderedDict()
            if isinstance(self._response, Return):
                response_dict = self._response["value"]
                self.log_debug("Response value : %s", self._response["value"])
            elif isinstance(self._response, Error):
                response_dict = self._response.to_dict()
                response_dict.pop("id")

            if not response_dict:
                pv_object = pvaccess.PvObject(OrderedDict({}), 'malcolm:core/Map:1.0')
            else:
                #pv_object = self._server.dict_to_structure(response_dict)
                #self.log_debug("Pv Object structure created")
                #self.log_debug("%s", self._server.strip_type_id(response_dict))
                #pv_object.set(self._server.strip_type_id(response_dict))
                pv_object = self._server.dict_to_pv_object(response_dict)
            self.log_debug("Pv Object value set: %s", pv_object)
            # Add this RPC to the purge list
            #self._server.register_dead_rpc(self._id)
            return pv_object
Esempio n. 5
0
    def post_async(self, method, params):
        """Asynchronously calls a function on a child block

            Returns a list of one future which will proved the return value
            on completion"""
        endpoint = [method.parent.name, method.name]
        request = Post(None, self.q, endpoint, params)
        f = Future(self)
        new_id = self._save_future(f)
        request.set_id(new_id)
        self.process.q.put(request)

        return [f]
Esempio n. 6
0
    def test_invalid_request_fails(self):
        endpoint = ["a", "b", "c", "d"]
        request = Post(MagicMock(), MagicMock(), endpoint)
        self.assertRaises(ValueError, self.block.handle_request, request)

        request = Put(MagicMock(), MagicMock(), endpoint)
        self.assertRaises(ValueError, self.block.handle_request, request)
Esempio n. 7
0
    def setUp(self):
        self.context = MagicMock()
        self.response_queue = MagicMock()
        self.endpoint = ["BL18I:XSPRESS3", "state", "value"]
        self.parameters = dict(arg1=5, arg2=True)

        self.post = Post(self.context, self.response_queue, self.endpoint, self.parameters)
Esempio n. 8
0
    def test_counter_subscribe(self):
        sync_factory = SyncFactory("sched")
        process = Process("proc", sync_factory)
        b = Counter(process, dict(mri="counting"))[0]
        process.start()
        # wait until block is Ready
        task = Task("counter_ready_task", process)
        task.when_matches(b["state"], "Ready", timeout=1)
        q = sync_factory.create_queue()

        sub = Subscribe(response_queue=q, context="ClientConnection",
                        endpoint=["counting", "counter"],
                        delta=False)
        process.q.put(sub)
        resp = q.get(timeout=1)
        self.assertIsInstance(resp, Update)
        attr = NTScalar.from_dict(resp.value)
        self.assertEqual(0, attr.value)

        post = Post(response_queue=q, context="ClientConnection",
                    endpoint=["counting", "increment"])
        process.q.put(post)

        resp = q.get(timeout=1)
        self.assertIsInstance(resp, Update)
        self.assertEqual(resp.value["value"], 1)
        resp = q.get(timeout=1)
        self.assertIsInstance(resp, Return)

        process.stop()
Esempio n. 9
0
    def post_async(self, method, params=None):
        """Asynchronously calls a function on a child block

            Returns a list of one future which will proved the return value
            on completion"""
        assert isinstance(method, MethodMeta), \
            "Expected MethodMeta, got %r" % (method,)

        endpoint = method.path_relative_to(self.process)

        request = Post(None, self.q, endpoint, params)
        f = Future(self)
        new_id = self._save_future(f)
        request.set_id(new_id)
        self.process.q.put(request)

        return [f]
Esempio n. 10
0
 def test_post(self):
     self.controller.validate_result.return_value = 22
     self.o._q.put(Return(1, dict(a=2)))
     result = self.o.post(["block", "method"], dict(b=32))
     self.controller.handle_request.assert_called_once_with(
         Post(1, ["block", "method"], dict(b=32)))
     self.controller.validate_result.assert_called_once_with(
         "method", dict(a=2))
     assert result == 22
Esempio n. 11
0
    def test_given_request_then_pass_to_correct_method(self):
        endpoint = ["TestBlock", "get_things"]
        request = Post(MagicMock(), MagicMock(), endpoint)

        self.block.handle_request(request)

        self.method.get_response.assert_called_once_with(request)
        response = self.method.get_response.return_value
        self.block.parent.block_respond.assert_called_once_with(
            response, request.response_queue)
Esempio n. 12
0
class TestPost(unittest.TestCase):
    def setUp(self):
        self.callback = MagicMock()
        self.path = ["BL18I:XSPRESS3", "configure"]
        self.parameters = OrderedDict()
        self.parameters["filePath"] = "/path/to/file.h5"
        self.parameters["exposure"] = 0.1
        self.o = Post(2, self.path, self.parameters)
        self.o.set_callback(self.callback)

    def test_init(self):
        assert self.o.typeid == "malcolm:core/Post:1.0"
        assert self.o.id == 2
        assert self.o.callback == self.callback
        assert self.path == self.o.path
        assert self.parameters == self.o.parameters

    def test_doc(self):
        assert get_doc_json("post_xspress3_configure") == self.o.to_dict()
Esempio n. 13
0
 def test_hello_with_process(self):
     sync_factory = SyncFactory("sched")
     process = Process("proc", sync_factory)
     b = Hello(process, dict(mri="hello"))[0]
     process.start()
     # wait until block is Ready
     task = Task("hello_ready_task", process)
     task.when_matches(b["state"], "Ready", timeout=1)
     q = sync_factory.create_queue()
     req = Post(response_queue=q, context="ClientConnection",
                endpoint=["hello", "greet"],
                parameters=dict(name="thing"))
     req.set_id(44)
     process.q.put(req)
     resp = q.get(timeout=1)
     self.assertEqual(resp.id, 44)
     self.assertEqual(resp.context, "ClientConnection")
     self.assertEqual(resp.typeid, "malcolm:core/Return:1.0")
     self.assertEqual(resp.value["greeting"], "Hello thing")
     process.stop()
Esempio n. 14
0
 def test_starting_process(self):
     s = SyncFactory("sched")
     p = Process("proc", s)
     b = MagicMock()
     p._handle_block_add(BlockAdd(b, "myblock", None))
     self.assertEqual(p._blocks, dict(myblock=b, proc=ANY))
     p.start()
     request = Post(MagicMock(), MagicMock(), ["myblock", "foo"])
     p.q.put(request)
     # wait for spawns to have done their job
     p.stop()
     b.handle_request.assert_called_once_with(request)
Esempio n. 15
0
    def execute(self, args):
        self.log_debug("Execute %s method called on [%s] with: %s", self._method, self._block, args)
        self.log_debug("Structure: %s", args.getStructureDict())
        # Acquire the lock
        with self._lock:
            try:
                # We now need to create the Post message and execute it
                endpoint = [self._block, self._method]
                request = Post(None, self._server.q, endpoint, self.parse_variants(args.toDict(True)))
                request.set_id(self._id)
                self._server.process.q.put(request)

                # Now wait for the Post reply
                self.log_debug("Waiting for reply")
                self.wait_for_reply(timeout=None)
                self.log_debug("Reply received %s %s", type(self._response), self._response)
                response_dict = OrderedDict()
                if isinstance(self._response, Return):
                    response_dict = self._response["value"]
                    self.log_debug("Response value : %s", response_dict)
                elif isinstance(self._response, Error):
                    response_dict = self._response.to_dict()
                    response_dict.pop("id")

                if not response_dict:
                    pv_object = pvaccess.PvObject(OrderedDict(), 'malcolm:core/Map:1.0')
                else:
                    #pv_object = self._server.dict_to_structure(response_dict)
                    #self.log_debug("Pv Object structure created")
                    #self.log_debug("%s", self._server.strip_type_id(response_dict))
                    #pv_object.set(self._server.strip_type_id(response_dict))
                    pv_object = self._server.dict_to_pv_object(response_dict)
                self.log_debug("Pv Object value set: %s", pv_object)
                # Add this RPC to the purge list
                #self._server.register_dead_rpc(self._id)
                return pv_object
            except Exception:
                self.log_exception("Request %s failed", self._request)
Esempio n. 16
0
class TestPost(unittest.TestCase):

    def setUp(self):
        self.context = MagicMock()
        self.response_queue = MagicMock()
        self.endpoint = ["BL18I:XSPRESS3", "state", "value"]
        self.parameters = dict(arg1=5, arg2=True)

        self.post = Post(self.context, self.response_queue, self.endpoint, self.parameters)

    def test_init(self):
        self.assertEqual(self.context, self.post.context)
        self.assertEqual(self.response_queue, self.post.response_queue)
        self.assertEqual(self.endpoint, self.post.endpoint)
        self.assertEqual(self.parameters, self.post.parameters)
        self.assertEqual("malcolm:core/Post:1.0", self.post.typeid)

    def test_setters(self):
        self.post.set_endpoint(["BL18I:XSPRESS3", "state", "value2"])
        self.assertEquals(["BL18I:XSPRESS3", "state", "value2"], self.post.endpoint)

        self.post.set_parameters(dict(arg1=2, arg2=False))
        self.assertEquals(dict(arg1=2, arg2=False), self.post.parameters)
Esempio n. 17
0
class TestPost(unittest.TestCase):

    def setUp(self):
        self.context = MagicMock()
        self.response_queue = MagicMock()
        self.endpoint = ["BL18I:XSPRESS3", "state", "value"]
        self.parameters = dict(arg1=5, arg2=True)

        self.post = Post(self.context, self.response_queue, self.endpoint, self.parameters)

    def test_init(self):
        self.assertEqual(self.context, self.post.context)
        self.assertEqual(self.response_queue, self.post.response_queue)
        self.assertEqual(self.endpoint, self.post.endpoint)
        self.assertEqual(self.parameters, self.post.parameters)
        self.assertEqual("malcolm:core/Post:1.0", self.post.typeid)

    def test_setters(self):
        self.post.set_endpoint(["BL18I:XSPRESS3", "state", "value2"])
        self.assertEquals(["BL18I:XSPRESS3", "state", "value2"], self.post.endpoint)

        self.post.set_parameters(dict(arg1=2, arg2=False))
        self.assertEquals(dict(arg1=2, arg2=False), self.post.parameters)
Esempio n. 18
0
    def post_async(self, method, params=None):
        """Asynchronously calls a function on a child block

        Returns a list of one future which will proved the return value
        on completion
        """
        assert isinstance(method, MethodMeta), \
            "Expected MethodMeta, got %r" % (method,)

        endpoint = method.process_path

        request = Post(None, self.q, endpoint, params)
        future = self._dispatch_request(request)
        self._methods[request.id] = method
        return [future]
 def test_send_post_to_server(self):
     self.PVA = PvaClientComms(self.p)
     self.PVA.send_to_caller = MagicMock()
     request = Post(endpoint=["ep1", "method1"], parameters={'arg1': 1})
     self.PVA.send_to_server(request)
     pvaccess.RpcClient.assert_called_once()
     self.rpc.invoke.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)
Esempio n. 20
0
 def test_post(self):
     self.o._q.put(Return(1, dict(a=2)))
     result = self.o.post(["block", "method"], dict(b=32))
     self.assert_handle_request_called_with(
         Post(1, ["block", "method"], dict(b=32)))
     assert result == dict(a=2)
 def do_initial_reset(self):
     request = Post(None, self.process.create_queue(),
                    [self.block_name, "reset"])
     self.process.q.put(request)
Esempio n. 22
0
    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