Esempio n. 1
0
class ProcessTest(unittest.TestCase):

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

    def test_gc(self):
        msgs = []

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

    def test_create_device(self):
        expected = ['Device', 'RunnableDevice', 'PausableDevice',
                    'Process', 'DirectoryService', 'MockDevice']
        for d in expected:
            self.assertIn(d, self.p.deviceTypes)
        d = self.p.createMockDevice("MD")
        self.assertIsInstance(d, MockDevice)
        self.assertEqual(d.name, "MD")

    def test_call_on_device(self):
        d = self.p.createMockDevice("MD")
        self.s.inq.Signal((SType.Call, dict(endpoint="MD", method="run")))
        self.assertEqual(d.runcalled, False)
        # Yield to let socket recv
        cothread.Yield()
        # Yield to let process retrieve from inq
        cothread.Yield()
        # Yield to let spawned do_func run
        cothread.Yield()
        self.assertEqual(d.runcalled, True)
        self.assertIsNot(self.s.send, None)
        self.s.send.assert_called_once_with(SType.Return, 32)

    def test_put_on_device(self):
        d = self.p.createMockDevice("MD")
        d.b = 40
        self.s.inq.Signal((SType.Put, dict(endpoint="MD.attributes.a", value=True)))
        self.assertEqual(d.runcalled, False)
        # Yield to let socket recv
        cothread.Yield()
        # Yield to let process retrieve from inq
        cothread.Yield()
        # Yield to let spawned do_func run
        cothread.Yield()
        self.assertEqual(d.runcalled, True)
        self.assertEqual(d.runargs, (True, 40))
        self.assertIsNot(self.s.send, None)
        self.s.send.assert_called_once_with(SType.Return, 32)

    def test_get_on_device(self):
        d = self.p.createMockDevice("MD")
        self.s.inq.Signal((SType.Get, dict(endpoint="MD.methods")))
        # Yield to let socket recv
        cothread.Yield()
        # Yield to let process retrieve from inq
        cothread.Yield()
        self.assertIsNot(self.s.send, None)
        self.s.send.assert_called_once_with(SType.Return, d.methods)

    def test_subscribe_on_device(self):
        d = self.p.createMockDevice("MD")
        self.s.inq.Signal((SType.Subscribe, dict(endpoint="MD.methods.run")))
        # Yield to let socket recv
        cothread.Yield()
        # Yield to let process retrieve from inq
        cothread.Yield()
        d.notify_listeners({"methods.run.descriptor": "new desc"})
        # Yield to let subscription send back out
        cothread.Yield()
        self.assertIsNot(self.s.send, None)
        self.s.send.assert_called_once_with(
            SType.Value, d.methods["run"], changes=dict(descriptor="new desc"))
        self.s.send.reset_mock()
        self.s.inq.Signal((SType.Unsubscribe, {}))
        # Yield to let socket recv
        cothread.Yield()
        # Yield to let process retrieve from inq
        cothread.Yield()
        self.s.send.assert_called_once_with(
            SType.Return)
        # Yield to let subscription remove itself from parent
        cothread.Yield()