示例#1
0
class TestCounterDemoSystem(unittest.TestCase):
    def setUp(self):
        self.process = Process("proc")
        parts = [call_with_params(CounterPart, name="cpart")]
        self.controller = Controller(self.process, "counting", parts)
        self.process.start()

    def tearDown(self):
        self.process.stop(timeout=1)

    def test_counter_subscribe(self):
        q = Queue()
        sub = Subscribe(id=20,
                        path=["counting", "counter"],
                        delta=False,
                        callback=q.put)
        self.controller.handle_request(sub)
        response = q.get(timeout=1.0)
        self.assertIsInstance(response, Update)
        assert response.id == 20
        assert response.value["typeid"] == "epics:nt/NTScalar:1.0"
        assert response.value["value"] == 0
        post = Post(id=21, path=["counting", "increment"], callback=q.put)
        self.controller.handle_request(post)
        response = q.get(timeout=1)
        self.assertIsInstance(response, Update)
        assert response.id == 20
        assert response.value["value"] == 1
        response = q.get(timeout=1)
        self.assertIsInstance(response, Return)
        assert response.id == 21
        assert response.value == None
        with self.assertRaises(TimeoutError):
            q.get(timeout=0.05)
示例#2
0
class TestHelloDemoSystem(unittest.TestCase):
    def setUp(self):
        self.process = Process("proc")
        parts = [call_with_params(HelloPart, name="hpart")]
        self.controller = Controller(self.process, "hello_block", parts)
        self.process.start()

    def tearDown(self):
        self.process.stop(timeout=1)

    def test_hello_good_input(self):
        q = Queue()
        request = Post(id=44,
                       path=["hello_block", "greet"],
                       parameters=dict(name="thing"),
                       callback=q.put)
        self.controller.handle_request(request)
        response = q.get(timeout=1.0)
        self.assertIsInstance(response, Return)
        assert response.id == 44
        assert response.value["greeting"] == "Hello thing"
示例#3
0
class TestCounterDemoSystem(unittest.TestCase):
    def setUp(self):
        self.process = Process("proc")
        self.controller = Controller("counting")
        self.controller.add_part(CounterPart("cpart"))
        self.process.add_controller(self.controller)
        self.process.start()

    def tearDown(self):
        self.process.stop(timeout=1)

    def test_counter_subscribe(self):
        q = Queue()
        # Subscribe to the value
        sub = Subscribe(id=20, path=["counting", "counter"], delta=False)
        sub.set_callback(q.put)
        self.controller.handle_request(sub)
        # Check initial return
        response = q.get(timeout=1.0)
        self.assertIsInstance(response, Update)
        assert response.id == 20
        assert response.value["typeid"] == "epics:nt/NTScalar:1.0"
        assert response.value["value"] == 0
        # Post increment()
        post = Post(id=21, path=["counting", "increment"])
        post.set_callback(q.put)
        self.controller.handle_request(post)
        # Check the value updates...
        response = q.get(timeout=1)
        self.assertIsInstance(response, Update)
        assert response.id == 20
        assert response.value["value"] == 1
        # ... then we get the return
        response = q.get(timeout=1)
        self.assertIsInstance(response, Return)
        assert response.id == 21
        assert response.value is None
        # Check we can put too
        put = Put(id=22, path=["counting", "counter", "value"], value=31)
        put.set_callback(q.put)
        self.controller.handle_request(put)
        # Check the value updates...
        response = q.get(timeout=1)
        self.assertIsInstance(response, Update)
        assert response.id == 20
        assert response.value["value"] == 31
        # ... then we get the return
        response = q.get(timeout=1)
        self.assertIsInstance(response, Return)
        assert response.id == 22
        assert response.value is None
        # And that there isn't anything else
        with self.assertRaises(TimeoutError):
            q.get(timeout=0.05)
示例#4
0
class TestHelloDemoSystem(unittest.TestCase):
    def setUp(self):
        self.process = Process("proc")
        self.controller = Controller("hello_block")
        self.controller.add_part(HelloPart("hpart"))
        self.process.add_controller(self.controller)
        self.process.start()

    def tearDown(self):
        self.process.stop(timeout=1)

    def test_hello_good_input(self):
        q = Queue()
        request = Post(id=44,
                       path=["hello_block", "greet"],
                       parameters=dict(name="thing"))
        request.set_callback(q.put)
        self.controller.handle_request(request)
        response = q.get(timeout=1.0)
        self.assertIsInstance(response, Return)
        assert response.id == 44
        assert response.value == "Hello thing"

    def test_concurrent(self):
        q = Queue()
        request = Post(id=44,
                       path=["hello_block", "greet"],
                       parameters=dict(name="me", sleep=1))
        request.set_callback(q.put)
        self.controller.handle_request(request)
        request = Post(id=45, path=["hello_block", "error"])
        request.set_callback(q.put)
        self.controller.handle_request(request)
        response = q.get(timeout=1.0)
        self.assertIsInstance(response, Error)
        assert response.id == 45
        response = q.get(timeout=3.0)
        self.assertIsInstance(response, Return)
        assert response.id == 44
        assert response.value == "Hello me"
示例#5
0
class TestController(unittest.TestCase):
    maxDiff = None

    def setUp(self):
        self.process = Process("proc")
        self.part = MyPart("test_part")
        self.o = Controller("mri")
        self.o.add_part(self.part)
        self.process.add_controller(self.o)
        self.process.start()

    def tearDown(self):
        self.process.stop(timeout=1)

    def test_init(self):
        assert self.o.mri == "mri"
        assert self.o.process == self.process

    def test_two_parts_same_attribute_fails(self):
        p2 = MyPart("another_part")
        with self.assertRaises(AssertionError) as cm:
            self.o.add_part(p2)
        assert str(cm.exception) == (
            "Field 'myAttribute' published by MyPart(name='another_part') "
            "would overwrite one made by MyPart(name='test_part')"
        )

    def test_make_view(self):
        b = self.process.block_view("mri")
        method_view = b.method
        attribute_view = b.myAttribute
        dict_view = b.method.meta.returns.elements
        list_view = b.method.meta.returns.required
        assert method_view() == "world"
        assert attribute_view.value == "hello_block"
        assert dict_view["return"].description == "The return value"
        assert list_view[0] == "return"
        assert b.meta.tags == ["version:pymalcolm:%s" % __version__]

    def test_handle_request(self):
        q = Queue()

        request = Get(id=41, path=["mri", "myAttribute"])
        request.set_callback(q.put)
        self.o.handle_request(request)
        response = q.get(timeout=0.1)
        self.assertIsInstance(response, Return)
        assert response.id == 41
        assert response.value["value"] == "hello_block"
        self.part.my_attribute.meta.writeable = False
        request = Put(
            id=42, path=["mri", "myAttribute"], value="hello_block2", get=True
        )
        request.set_callback(q.put)
        self.o.handle_request(request)
        response = q.get(timeout=0.1)
        self.assertIsInstance(response, Error)  # not writeable
        assert response.id == 42

        self.part.my_attribute.meta.writeable = True
        self.o.handle_request(request)
        response = q.get(timeout=0.1)
        self.assertIsInstance(response, Return)
        assert response.id == 42
        assert response.value == "hello_block2"

        request = Post(id=43, path=["mri", "method"])
        request.set_callback(q.put)
        self.o.handle_request(request)
        response = q.get(timeout=0.1)
        self.assertIsInstance(response, Return)
        assert response.id == 43
        assert response.value == "world"

        # cover the controller._handle_post path for parameters
        request = Post(id=43, path=["mri", "method"], parameters={"dummy": 1})
        request.set_callback(q.put)
        self.o.handle_request(request)
        response = q.get(timeout=0.1)
        self.assertIsInstance(response, Error)
        assert response.id == 43
        assert (
            str(response.message)
            == "Given keys ['dummy'], some of which aren't in allowed keys []"
        )

        request = Subscribe(id=44, path=["mri", "myAttribute"], delta=False)
        request.set_callback(q.put)
        self.o.handle_request(request)
        response = q.get(timeout=0.1)
        self.assertIsInstance(response, Update)
        assert response.id == 44
        assert response.value["typeid"] == "epics:nt/NTScalar:1.0"
        assert response.value["value"] == "hello_block2"

        request = Unsubscribe(id=44)
        request.set_callback(q.put)
        self.o.handle_request(request)
        response = q.get(timeout=0.1)
        self.assertIsInstance(response, Return)
        assert response.id == 44
示例#6
0
class TestHelloDemoSystem(unittest.TestCase):
    def setUp(self):
        self.process = Process("proc")
        self.controller = Controller("hello_block")
        self.controller.add_part(HelloPart("hpart"))
        self.process.add_controller(self.controller)
        self.process.start()

    def tearDown(self):
        self.process.stop(timeout=1)

    def test_hello_good_input(self):
        q = Queue()
        request = Post(id=44,
                       path=["hello_block", "greet"],
                       parameters=dict(name="thing"))
        request.set_callback(q.put)
        self.controller.handle_request(request)
        response = q.get(timeout=1.0)
        self.assertIsInstance(response, Return)
        assert response.id == 44
        assert response.value == "Hello thing"

    def test_concurrent(self):
        q = Queue()
        request = Subscribe(id=40, path=["hello_block", "greet"], delta=True)
        request.set_callback(q.put)
        self.controller.handle_request(request)
        # Get the initial subscribe value
        inital = q.get(timeout=0.1)
        self.assertIsInstance(inital, Delta)
        assert inital.changes[0][1]["took"]["value"] == dict(sleep=0, name="")
        assert inital.changes[0][1]["returned"]["value"] == {"return": ""}
        # Do a greet
        request = Post(id=44,
                       path=["hello_block", "greet"],
                       parameters=dict(name="me", sleep=1))
        request.set_callback(q.put)
        self.controller.handle_request(request)
        # Then an error
        request = Post(id=45, path=["hello_block", "error"])
        request.set_callback(q.put)
        self.controller.handle_request(request)
        # We should quickly get the error response first
        response = q.get(timeout=1.0)
        self.assertIsInstance(response, Error)
        assert response.id == 45
        # Then the long running greet delta
        response = q.get(timeout=3.0)
        self.assertIsInstance(response, Delta)
        assert len(response.changes) == 2
        assert response.changes[0][0] == ["took"]
        took = response.changes[0][1]
        assert took.value == dict(sleep=1, name="me")
        assert took.present == ["name", "sleep"]
        assert took.alarm == Alarm.ok
        assert response.changes[1][0] == ["returned"]
        returned = response.changes[1][1]
        assert returned.value == {"return": "Hello me"}
        assert returned.present == ["return"]
        assert returned.alarm == Alarm.ok
        # Check it took about 1s to run
        assert abs(1 - (returned.timeStamp.to_time() -
                        took.timeStamp.to_time())) < 0.4
        # And it's response
        response = q.get(timeout=1.0)
        self.assertIsInstance(response, Return)
        assert response.id == 44
        assert response.value == "Hello me"