Пример #1
0
 def setUp(self):
     self.cs = DummyClientSocket("cs", "csaddr")
     self.cs.open(self.cs.address)
     self.ss = DummyServerSocket("ss", "ssaddr", None)
     self.ss.open(self.ss.address)
     self.zebraClient = DeviceClient("zebra1", self.cs)
     self.dsClient = DeviceClient("DirectoryService", self.cs)
     self.ds = DirectoryService([])
     self.zebra = self.ds.create_device(DummyZebra, "zebra1")
Пример #2
0
 def setUp(self):
     self.sock = MagicMock()
     self.d = DeviceClient("D", self.sock, timeout=1)
Пример #3
0
class ZmqDocsTest(unittest.TestCase):

    def setUp(self):
        self.cs = DummyClientSocket("cs", "csaddr")
        self.cs.open(self.cs.address)
        self.ss = DummyServerSocket("ss", "ssaddr", None)
        self.ss.open(self.ss.address)
        self.zebraClient = DeviceClient("zebra1", self.cs)
        self.dsClient = DeviceClient("DirectoryService", self.cs)
        self.ds = DirectoryService([])
        self.zebra = self.ds.create_device(DummyZebra, "zebra1")

    def assertDocExample(self, fname, actual):
        expected = open(os.path.join(os.path.dirname(
            __file__), "..", "..", "docs", "comms", "zmqExamples", fname)).read()
        prettyactual = json.dumps(
            json.loads(actual, object_pairs_hook=OrderedDict), indent=2)
        header = ".. code-block:: javascript\n\n"
        docactual = header + \
            "\n".join("    " + x for x in prettyactual.splitlines()) + "\n"
        if expected != docactual:
            print
            print docactual
            message = ''.join(difflib.unified_diff(
                expected.splitlines(True), docactual.splitlines(True)))
            self.fail("Output doesn't match docs: %s\n" % message)

    @patch("malcolm.core.deviceClient.ValueQueue")
    def test_call_zebra_configure(self, mock_vq):
        self.zebraClient.do_call("configure", PC_BIT_CAP=1, PC_TSPRE="ms")
        call_args = self.cs.sock.send_multipart.call_args
        self.assertDocExample("call_zebra_configure", call_args[0][0][0])

    @patch("malcolm.core.deviceClient.ValueQueue")
    def test_get_DirectoryService_Device_instances(self, mock_vq):
        self.dsClient.do_get("attributes.Device_instances.value")
        call_args = self.cs.sock.send_multipart.call_args
        self.assertDocExample(
            "get_DirectoryService_Device_instances", call_args[0][0][0])

    @patch("malcolm.core.deviceClient.ValueQueue")
    def test_get_zebra_status(self, mock_vq):
        self.zebraClient.do_get("stateMachine")
        call_args = self.cs.sock.send_multipart.call_args
        self.assertDocExample("get_zebra_status", call_args[0][0][0])

    @patch("malcolm.core.deviceClient.ValueQueue")
    def test_get_zebra(self, mock_vq):
        self.zebraClient.do_get()
        call_args = self.cs.sock.send_multipart.call_args
        self.assertDocExample("get_zebra", call_args[0][0][0])

    def test_subscribe_zebra_status(self):
        el = self.zebraClient.do_subscribe(lambda: None, "stateMachine")
        call_args = self.cs.sock.send_multipart.call_args
        self.assertDocExample("subscribe_zebra_status", call_args[0][0][0])
        el.inq = MagicMock()
        el.loop_stop()
        el._loop_state = LState.Stopped
        call_args = self.cs.sock.send_multipart.call_args
        self.assertDocExample("unsubscribe_zebra_status", call_args[0][0][0])

    def test_value_zebra_status(self):
        send = self.ss.make_send_function(dict(zmq_id=1, id=0))
        sub = self.ds.do_subscribe(send, "zebra1.stateMachine")
        sub.inq = MagicMock()
        self.zebra.stateMachine.update(state=DState.Configuring, message="Configuring...", timeStamp=14419090000.2)
        send(*sub.inq.Signal.call_args[0][0][1])
        call_args = self.ss.sock.send_multipart.call_args
        self.assertDocExample("value_zebra_status", call_args[0][0][1])

    def test_return_zebra_status(self):
        send = self.ss.make_send_function(dict(zmq_id=1, id=0))
        self.zebra.stateMachine.update(state=DState.Configuring, message="Configuring...", timeStamp=14419090000.2)        
        self.ds.do_get(send, "zebra1.stateMachine")
        call_args = self.ss.sock.send_multipart.call_args
        self.assertDocExample("return_zebra_status", call_args[0][0][1])        

    def test_return_zebra(self):
        send = self.ss.make_send_function(dict(zmq_id=1, id=0))
        self.zebra.stateMachine.update(state=DState.Configuring, message="Configuring...", timeStamp=14419090000.2)        
        self.ds.do_get(send, "zebra1")
        call_args = self.ss.sock.send_multipart.call_args
        self.assertDocExample("return_zebra", call_args[0][0][1])

    def test_return_DirectoryService_Device_instances(self):
        send = self.ss.make_send_function(dict(zmq_id=1, id=0))
        self.ds.do_get(send, "DirectoryService.attributes.Device_instances.value")
        call_args = self.ss.sock.send_multipart.call_args
        self.assertDocExample("return_DirectoryService_Device_instances", call_args[0][0][1])

    # Mock out EventLoop so we don't log error
    @patch("malcolm.core.process.EventLoop")
    def test_error_foo(self, el):
        send = self.ss.make_send_function(dict(zmq_id=1, id=0))
        try:
            self.ds.do_get(send, "foo.stateMachine")
        except Exception, e:
            self.ds.do_error(e, send)
        call_args = self.ss.sock.send_multipart.call_args
        self.assertDocExample("error_foo", call_args[0][0][1])
Пример #4
0
class DeviceTest(unittest.TestCase):

    def setUp(self):
        self.sock = MagicMock()
        self.d = DeviceClient("D", self.sock, timeout=1)

    def test_setup(self):
        self.assertEqual(self.sock.mock_calls, [])

    def test_run_calls_right_things(self):
        def side_effect(response, typ, kwargs):
            self.post = response
            response(SType.Return, value={})
        self.sock.request.side_effect = side_effect
        self.d.loop_run()
        self.assertEqual(len(self.sock.mock_calls), 1)
        self.sock.request.assert_called_once_with(
            self.post, SType.Get, dict(endpoint="D"))

    def test_attribute_monitors(self):
        def side_effect(post, typ, kwargs):
            if kwargs["endpoint"] == "D":
                self.post = post
                bit = dict(type="int", descriptor="Desc", value=3,
                           alarm=Alarm.ok().to_dict(), timeStamp=43.2)
                post(SType.Return, value=dict(attributes=dict(bit=bit)))
            elif kwargs["endpoint"] == "D.attributes.bit":
                self.spost = post
        self.sock.request.side_effect = side_effect
        self.d.loop_run()
        self.assertEqual(len(self.sock.mock_calls), 2)
        self.assertEqual(self.sock.request.call_args_list[0], call(
            self.post, SType.Get, dict(endpoint="D")))
        self.assertEqual(self.sock.request.call_args_list[1], call(
            self.spost, SType.Subscribe, dict(endpoint="D.attributes.bit")))
        self.assertEqual(self.d.bit, 3)
        self.assertEqual(self.d.attributes["bit"].typ, int)
        self.assertEqual(self.d.attributes["bit"].descriptor, "Desc")
        self.assertEqual(self.d.attributes["bit"].value, 3)
        self.assertEqual(self.d.attributes["bit"].alarm, Alarm.ok())
        self.assertEqual(self.d.attributes["bit"].timeStamp, 43.2)

    def test_methods(self):
        def side_effect(response, typ, kwargs):
            self.post = response
            if typ == SType.Get and kwargs["endpoint"] == "D":
                run = dict(descriptor="Run")
                response(SType.Return, value=dict(methods=dict(run=run)))
            elif kwargs["method"] == "run":
                response(SType.Return, value=99)
            else:
                print kwargs
        self.sock.request.side_effect = side_effect
        self.d.loop_run()
        self.assertEqual(len(self.sock.mock_calls), 1)
        self.sock.request.assert_called_once_with(
            self.post, SType.Get, dict(endpoint="D"))
        self.assertIn("run", dir(self.d))
        self.sock.request.reset_mock()
        self.assertEqual(self.d.run(), 99)
        self.sock.request.assert_called_once_with(
            self.post, SType.Call, dict(endpoint="D", method="run", args={}))

    def test_sm(self):
        def side_effect(post, typ, kwargs):
            if kwargs["endpoint"] == "D":
                self.post = post
                sm = dict(
                    state=DState.Running.to_dict(), message="foo", timeStamp=43.2)
                post(SType.Return, value=dict(stateMachine=sm))
            elif kwargs["endpoint"] == "D.stateMachine":
                self.spost = post
        self.sock.request.side_effect = side_effect
        self.d.loop_run()
        self.assertEqual(len(self.sock.mock_calls), 2)
        self.assertEqual(self.sock.request.call_args_list[0], call(
            self.post, SType.Get, dict(endpoint="D")))
        self.assertEqual(self.sock.request.call_args_list[1], call(
            self.spost, SType.Subscribe, dict(endpoint="D.stateMachine")))
        self.assertEqual(self.d.stateMachine.state, DState.Running)
        self.assertEqual(self.d.stateMachine.message, "foo")
        self.assertEqual(self.d.stateMachine.timeStamp, 43.2)

    def test_gc(self):
        def side_effect(post, typ, kwargs):
            if kwargs["endpoint"] == "D":
                sm = dict(
                    state=DState.Running.to_dict(), message="foo", timeStamp=43.2)
                run = dict(descriptor="Run")
                bit = dict(type="int", descriptor="Desc", value=3,
                           alarm=Alarm.ok().to_dict(), timeStamp=43.2)
                post(SType.Return, value=dict(
                    attributes=dict(bit=bit), stateMachine=sm, methods=dict(run=run)))
        self.sock.request.side_effect = side_effect
        self.d.loop_run()
        self.assertEqual(len(self.sock.mock_calls), 3)
        msgs = []

        def log_debug(msg):
            msgs.append(msg)
        self.d.log_debug = log_debug
        self.d = None
        self.assertEqual(msgs, ['Garbage collecting loop', 'Stopping loop', 'Waiting for loop to finish', 
                                'Confirming loop stopped', 'Loop garbage collected'])