Exemplo n.º 1
0
class DirectoryServiceTest(unittest.TestCase):

    def setUp(self):
        self.ds = DirectoryService(["tst://socket"], "The Process")
        self.ds.run(block=False)
        self.s = self.ds._server_socks["tst://socket"]

    def test_gc(self):
        msgs = []

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

    def test_update_instance_attributes(self):
        d1 = self.ds.createMockDevice2("MD1")
        self.assertEqual(d1.child, None)
        self.assertEqual(d1.attributes["child"].typ.labels, ("MD1",))
        l = MagicMock()
        d1.add_listener(l, "attributes.child")
        d2 = self.ds.createMockDevice2("MD2")
        self.assertEqual(d2.child, None)
        self.assertEqual(d1.attributes["child"].typ.labels, ("MD1", "MD2"))
        self.assertEqual(d2.attributes["child"].typ.labels, ("MD1", "MD2"))
        l.assert_called_once_with(
            d1.attributes["child"], dict(type=d1.attributes["child"].typ))
        self.assertRaises(
            AssertionError, d1.attributes["child"].update, "MD2ss")
        d1.child = "MD2"
        self.assertEqual(d1.child, d2)
        self.assertEqual(d1.attributes["child"].to_dict()["value"], "MD2")
Exemplo n.º 2
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")
Exemplo n.º 3
0
 def setUp(self):
     # Spawn ds under cothread
     self.ds = DirectoryService(["zmq://ipc:///tmp/sock.ipc"])
     self.ds.run(block=False)
     self.lp = Process(
         [], "Local Process", ds_string="zmq://ipc:///tmp/sock.ipc")
     self.lp.run(block=False)
     self.lp.ds.createCounter(name="The Counter")
     self.c = self.lp.get_device("The Counter")
Exemplo n.º 4
0
class ZmqSystemTest(unittest.TestCase):
    # class ZmqSystemTest(object):

    def setUp(self):
        # Spawn ds under cothread
        self.ds = DirectoryService(["zmq://ipc:///tmp/sock.ipc"])
        self.ds.run(block=False)
        self.lp = Process(
            [], "Local Process", ds_string="zmq://ipc:///tmp/sock.ipc")
        self.lp.run(block=False)
        self.lp.ds.createCounter(name="The Counter")
        self.c = self.lp.get_device("The Counter")

    def test_simple_function(self):
        import cothread
        cothread.Sleep(0.2)
        start = self.c.getCount()
        self.assertEqual(self.c.hello(), "world")
        # Hello world takes about 10 ticks
        self.assertAlmostEqual(self.c.getCount(), start + 10, delta=3)
        # Do a long running call
        s = cothread.Spawn(self.c.longHello)
        # Check it returns immediately
        self.assertAlmostEqual(self.c.getCount(), start + 10, delta=3)
        self.assertEqual(self.c.hello(), "world")
        # Hello world takes 10 ticks
        self.assertAlmostEqual(self.c.getCount(), start + 20, delta=3)
        self.assertEqual(s.Wait(), "long world")
        # Long hello takes about 50 ticks from send
        self.assertAlmostEqual(self.c.getCount(), start + 60, delta=8)
        s.Wait()

    def tearDown(self):
        self.c = None
        self.lp.ds.exit()
        self.lp.exit()
        self.lp = None
        self.ds.loop_wait()
        self.ds = None
Exemplo n.º 5
0
 def setUp(self):
     self.ds = DirectoryService(["tst://socket"], "The Process")
     self.ds.run(block=False)
     self.s = self.ds._server_socks["tst://socket"]
Exemplo n.º 6
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):
        positions = [
            ("y", VDouble, np.repeat(np.arange(6, 9), 5) * 0.1, "mm"),
            ("x", VDouble, np.tile(np.arange(5), 3) * 0.1, "mm"),
        ]
        self.zebraClient.do_call("configure", pcBitCap=1, pcTsPre="ms", positions=positions)
        self.assertDocExample("call_zebra_configure", self.cs.sendq.popleft()[0])

    @patch("malcolm.core.deviceclient.ValueQueue")
    def test_get_DirectoryService_Device_instances(self, mock_vq):
        self.dsClient.do_get("attributes.Device_instances.value")
        self.assertDocExample("get_DirectoryService_Device_instances", self.cs.sendq.popleft()[0])

    @patch("malcolm.core.deviceclient.ValueQueue")
    def test_get_zebra_status(self, mock_vq):
        self.zebraClient.do_get("stateMachine")
        self.assertDocExample("get_zebra_status", self.cs.sendq.popleft()[0])

    @patch("malcolm.core.deviceclient.ValueQueue")
    def test_get_zebra(self, mock_vq):
        self.zebraClient.do_get()
        self.assertDocExample("get_zebra", self.cs.sendq.popleft()[0])

    def test_subscribe_zebra_status(self):
        el = self.zebraClient.do_subscribe(lambda: None, "stateMachine")
        self.assertDocExample("subscribe_zebra_status", self.cs.sendq.popleft()[0])
        el.loop_run()
        el.loop_stop()
        self.assertDocExample("unsubscribe_zebra_status", self.cs.sendq.popleft()[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])
        self.assertDocExample("value_zebra_status", self.ss.sendq.popleft()[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")
        self.assertDocExample("return_zebra_status", self.ss.sendq.popleft()[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.zebra.attributes["connected"].update(
            0, Alarm(AlarmSeverity.invalidAlarm, AlarmStatus.UDF, "Disconnected"), timeStamp=14419091000.2
        )
        self.ds.do_get(send, "zebra1")
        self.assertDocExample("return_zebra", self.ss.sendq.popleft()[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.instancesDevice.value")
        self.assertDocExample("return_DirectoryService_Device_instances", self.ss.sendq.popleft()[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)
        self.assertDocExample("error_foo", self.ss.sendq.popleft()[1])