コード例 #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")
コード例 #2
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