def test_ctor_loop(self): loop = unittest.mock.Mock() cond = locks.Condition(loop=loop) self.assertIs(cond._loop, loop) cond = locks.Condition(loop=self.loop) self.assertIs(cond._loop, self.loop)
def test_ctor_noloop(self): try: events.set_event_loop(self.loop) cond = locks.Condition() self.assertIs(cond._loop, self.loop) finally: events.set_event_loop(None)
def test_wait_for_unacquired(self): cond = locks.Condition(loop=self.loop) # predicate can return true immediately res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3])) self.assertEqual([1, 2, 3], res) self.assertRaises(RuntimeError, self.loop.run_until_complete, cond.wait_for(lambda: False))
def test_wait_cancel(self): cond = locks.Condition(loop=self.loop) self.loop.run_until_complete(cond.acquire()) wait = tasks.Task(cond.wait(), loop=self.loop) self.loop.call_soon(wait.cancel) self.assertRaises(futures.CancelledError, self.loop.run_until_complete, wait) self.assertFalse(cond._condition_waiters) self.assertTrue(cond.locked())
def test_notify(self): cond = locks.Condition(loop=self.loop) result = [] @tasks.coroutine def c1(result): yield from cond.acquire() if (yield from cond.wait()): result.append(1) cond.release() return True @tasks.coroutine def c2(result): yield from cond.acquire() if (yield from cond.wait()): result.append(2) cond.release() return True @tasks.coroutine def c3(result): yield from cond.acquire() if (yield from cond.wait()): result.append(3) cond.release() return True t1 = tasks.Task(c1(result), loop=self.loop) t2 = tasks.Task(c2(result), loop=self.loop) t3 = tasks.Task(c3(result), loop=self.loop) test_utils.run_briefly(self.loop) self.assertEqual([], result) self.loop.run_until_complete(cond.acquire()) cond.notify(1) cond.release() test_utils.run_briefly(self.loop) self.assertEqual([1], result) self.loop.run_until_complete(cond.acquire()) cond.notify(1) cond.notify(2048) cond.release() test_utils.run_briefly(self.loop) self.assertEqual([1, 2, 3], result) self.assertTrue(t1.done()) self.assertTrue(t1.result()) self.assertTrue(t2.done()) self.assertTrue(t2.result()) self.assertTrue(t3.done()) self.assertTrue(t3.result())
def test_wait_for(self): cond = locks.Condition(loop=self.loop) presult = False def predicate(): return presult result = [] @tasks.coroutine def c1(result): yield from cond.acquire() if (yield from cond.wait_for(predicate)): result.append(1) cond.release() return True t = tasks.Task(c1(result), loop=self.loop) test_utils.run_briefly(self.loop) self.assertEqual([], result) self.loop.run_until_complete(cond.acquire()) cond.notify() cond.release() test_utils.run_briefly(self.loop) self.assertEqual([], result) presult = True self.loop.run_until_complete(cond.acquire()) cond.notify() cond.release() test_utils.run_briefly(self.loop) self.assertEqual([1], result) self.assertTrue(t.done()) self.assertTrue(t.result())
def test_notify_all_unacquired(self): cond = locks.Condition(loop=self.loop) self.assertRaises(RuntimeError, cond.notify_all)
def test_wait_unacquired(self): cond = locks.Condition(loop=self.loop) self.assertRaises(RuntimeError, self.loop.run_until_complete, cond.wait())