示例#1
0
    def test_ctor_loop(self):
        loop = unittest.mock.Mock()
        ev = locks.Event(loop=loop)
        self.assertIs(ev._loop, loop)

        ev = locks.Event(loop=self.loop)
        self.assertIs(ev._loop, self.loop)
示例#2
0
    def test_clear_with_waiters(self):
        ev = locks.Event(loop=self.loop)
        result = []

        @tasks.coroutine
        def c1(result):
            if (yield from ev.wait()):
                result.append(1)
            return True

        t = tasks.Task(c1(result), loop=self.loop)
        test_utils.run_briefly(self.loop)
        self.assertEqual([], result)

        ev.set()
        ev.clear()
        self.assertFalse(ev.is_set())

        ev.set()
        ev.set()
        self.assertEqual(1, len(ev._waiters))

        test_utils.run_briefly(self.loop)
        self.assertEqual([1], result)
        self.assertEqual(0, len(ev._waiters))

        self.assertTrue(t.done())
        self.assertTrue(t.result())
示例#3
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         ev = locks.Event()
         self.assertIs(ev._loop, self.loop)
     finally:
         events.set_event_loop(None)
示例#4
0
    def test_blocking_put_wait(self):
        def gen():
            when = yield
            self.assertAlmostEqual(0.01, when)
            yield 0.01

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        q = queues.Queue(maxsize=1, loop=loop)
        started = locks.Event(loop=loop)
        finished = False

        @tasks.coroutine
        def queue_put():
            nonlocal finished
            started.set()
            yield from q.put(1)
            yield from q.put(2)
            finished = True

        @tasks.coroutine
        def queue_get():
            loop.call_later(0.01, q.get_nowait)
            queue_put_task = tasks.Task(queue_put(), loop=loop)
            yield from started.wait()
            self.assertFalse(finished)
            yield from queue_put_task
            self.assertTrue(finished)

        loop.run_until_complete(queue_get())
        self.assertAlmostEqual(0.01, loop.time())
示例#5
0
    def test_wait_cancel(self):
        ev = locks.Event(loop=self.loop)

        wait = tasks.Task(ev.wait(), loop=self.loop)
        self.loop.call_soon(wait.cancel)
        self.assertRaises(futures.CancelledError, self.loop.run_until_complete,
                          wait)
        self.assertFalse(ev._waiters)
示例#6
0
    def test_clear(self):
        ev = locks.Event(loop=self.loop)
        self.assertFalse(ev.is_set())

        ev.set()
        self.assertTrue(ev.is_set())

        ev.clear()
        self.assertFalse(ev.is_set())
示例#7
0
    def test_wait(self):
        ev = locks.Event(loop=self.loop)
        self.assertFalse(ev.is_set())

        result = []

        @tasks.coroutine
        def c1(result):
            if (yield from ev.wait()):
                result.append(1)

        @tasks.coroutine
        def c2(result):
            if (yield from ev.wait()):
                result.append(2)

        @tasks.coroutine
        def c3(result):
            if (yield from ev.wait()):
                result.append(3)

        t1 = tasks.Task(c1(result), loop=self.loop)
        t2 = tasks.Task(c2(result), loop=self.loop)

        test_utils.run_briefly(self.loop)
        self.assertEqual([], result)

        t3 = tasks.Task(c3(result), loop=self.loop)

        ev.set()
        test_utils.run_briefly(self.loop)
        self.assertEqual([3, 1, 2], result)

        self.assertTrue(t1.done())
        self.assertIsNone(t1.result())
        self.assertTrue(t2.done())
        self.assertIsNone(t2.result())
        self.assertTrue(t3.done())
        self.assertIsNone(t3.result())
示例#8
0
    def test_wait_on_set(self):
        ev = locks.Event(loop=self.loop)
        ev.set()

        res = self.loop.run_until_complete(ev.wait())
        self.assertTrue(res)
示例#9
0
    def test_repr(self):
        ev = locks.Event(loop=self.loop)
        self.assertTrue(repr(ev).endswith('[unset]>'))

        ev.set()
        self.assertTrue(repr(ev).endswith('[set]>'))