Ejemplo n.º 1
0
    def test_mutex(self):
        def threadmain(mutex):
            yield cpu(1)
            yield lock(mutex)
            yield io(10)
            yield unlock(mutex)

        def main():
            mutex = Mutex()
            yield spawn(coromake(threadmain, mutex))
            yield spawn(coromake(threadmain, mutex))

        main = coromake(main)
        self.scheduler.run_program(main, 2, self.mon)
        self.assertListEqual(self.mon.history, [
            (0, "<CPU 0>", "<Coro 0>", "('spawn', 1, <Coro 1>)"),
            (1, "<CPU 0>", "<Coro 0>", "('spawn', 1, <Coro 2>)"),
            (1, "<CPU 1>", "<Coro 1>", "('cpu', 1)"),
            (2, "<CPU 0>", "<Coro 0>", "('terminate', 1, <Coro 0>)"),
            (2, "<CPU 1>", "<Coro 1>", "('lock', 1, <Mutex 0>)"),
            (3, "<CPU 0>", "<Coro 1>", "('io', 10)"),
            (3, "<CPU 1>", "<Coro 2>", "('cpu', 1)"),
            (4, "<CPU 1>", "<Coro 2>", "('lock', 1, <Mutex 0>)"),
            (13, "<CPU 0>", "<Coro 1>", "('unlock', 1, <Mutex 0>)"),
            (14, "<CPU 0>", "<Coro 1>", "('terminate', 1, <Coro 1>)"),
            (14, "<CPU 1>", "<Coro 2>", "('lock', 1, <Mutex 0>)"),
            (15, "<CPU 0>", "<Coro 2>", "('io', 10)"),
            (25, "<CPU 0>", "<Coro 2>", "('unlock', 1, <Mutex 0>)"),
            (26, "<CPU 0>", "<Coro 2>", "('terminate', 1, <Coro 2>)"),
        ])
Ejemplo n.º 2
0
 def test_peek(self):
     def func():
         yield cpu(1)
     coro = coromake(func)
     self.assertEqual(('cpu', 1), coro.peek_instruction())
     self.assertEqual(('cpu', 1), coro.pop_instruction())
     self.assertEqual(('terminate', 1, coro), coro.peek_instruction())
     self.assertEqual(('terminate', 1, coro), coro.pop_instruction())
Ejemplo n.º 3
0
 def test_pop(self):
     def func():
         yield cpu(1)
         yield io(3)
     coro = coromake(func)
     self.assertEqual(('cpu', 1), coro.pop_instruction())
     self.assertEqual(('io', 3), coro.pop_instruction())
     self.assertEqual(('terminate', 1, coro), coro.pop_instruction())
Ejemplo n.º 4
0
 def test_suspend(self):
     def func():
         yield(cpu(10))
         yield(io(1))
     coro = coromake(func)
     instruction = coro.pop_instruction()
     coro.suspend(instruction)
     self.assertTrue(coro.suspended())
     with self.assertRaises(RuntimeError):
         coro.pop_instruction()
     coro.resume()
     self.assertEqual(('cpu', 10), coro.peek_instruction())
     self.assertEqual(('cpu', 10), coro.pop_instruction())
Ejemplo n.º 5
0
    def test_hello_world(self):
        def main():
            yield cpu(1)
            yield io(1)
            yield cpu(1)

        main = coromake(main)
        self.scheduler.run_program(main, 1, self.mon)
        self.assertListEqual(self.mon.history, [
            (0, "<CPU 0>", "<Coro 0>", "('cpu', 1)"),
            (1, "<CPU 0>", "<Coro 0>", "('io', 1)"),
            (2, "<CPU 0>", "<Coro 0>", "('cpu', 1)"),
            (3, "<CPU 0>", "<Coro 0>", "('terminate', 1, <Coro 0>)"),
        ])
Ejemplo n.º 6
0
    def test_two_threads(self):
        def threadmain():
            yield cpu(1)

        def main():
            yield spawn(coromake(threadmain))
            yield spawn(coromake(threadmain))

        main = coromake(main)
        self.scheduler.run_program(main, 2, self.mon)
        self.assertListEqual(self.mon.history, [
            (0, "<CPU 0>", "<Coro 0>", "('spawn', 1, <Coro 1>)"),
            (1, "<CPU 0>", "<Coro 0>", "('spawn', 1, <Coro 2>)"),
            (1, "<CPU 1>", "<Coro 1>", "('cpu', 1)"),
            (2, "<CPU 0>", "<Coro 0>", "('terminate', 1, <Coro 0>)"),
            (2, "<CPU 1>", "<Coro 1>", "('terminate', 1, <Coro 1>)"),
            (3, "<CPU 0>", "<Coro 2>", "('cpu', 1)"),
            (4, "<CPU 0>", "<Coro 2>", "('terminate', 1, <Coro 2>)"),
        ])
Ejemplo n.º 7
0
 def test_empty_coro(self):
     def func(): yield
     coro = coromake(func)
     self.assertEqual(('terminate', 1, coro), coro.pop_instruction())
Ejemplo n.º 8
0
 def main():
     mutex = Mutex()
     yield spawn(coromake(threadmain, mutex))
     yield spawn(coromake(threadmain, mutex))
Ejemplo n.º 9
0
 def main():
     yield spawn(coromake(threadmain))
     yield spawn(coromake(threadmain))