Beispiel #1
0
    def test_ctor_loop(self):
        loop = mock.Mock()
        cond = asyncio.Condition(loop=loop)
        self.assertIs(cond._loop, loop)

        cond = asyncio.Condition(loop=self.loop)
        self.assertIs(cond._loop, self.loop)
Beispiel #2
0
    def test_ambiguous_loops(self):
        loop = self.new_test_loop()
        self.addCleanup(loop.close)

        lock = asyncio.Lock(loop=self.loop)
        with self.assertRaises(ValueError):
            asyncio.Condition(lock, loop=loop)
Beispiel #3
0
    def test_wait_for_unacquired(self):
        cond = asyncio.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))
Beispiel #4
0
    def test_notify(self):
        cond = asyncio.Condition(loop=self.loop)
        result = []

        @asyncio.coroutine
        def c1(result):
            yield From(cond.acquire())
            if (yield From(cond.wait())):
                result.append(1)
                cond.release()
            raise Return(True)

        @asyncio.coroutine
        def c2(result):
            yield From(cond.acquire())
            if (yield From(cond.wait())):
                result.append(2)
                cond.release()
            raise Return(True)

        @asyncio.coroutine
        def c3(result):
            yield From(cond.acquire())
            if (yield From(cond.wait())):
                result.append(3)
                cond.release()
            raise Return(True)

        t1 = asyncio.Task(c1(result), loop=self.loop)
        t2 = asyncio.Task(c2(result), loop=self.loop)
        t3 = asyncio.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()
        # each coroutine requires 2 runs of the event loop
        test_utils.run_briefly(self.loop, 2)
        self.assertEqual([1], result)

        self.loop.run_until_complete(cond.acquire())
        cond.notify(1)
        cond.notify(2048)
        cond.release()
        # each coroutine requires 2 runs of the event loop
        test_utils.run_briefly(self.loop, 4)
        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())
Beispiel #5
0
    def test_wait_cancel(self):
        cond = asyncio.Condition(loop=self.loop)
        self.loop.run_until_complete(cond.acquire())

        wait = asyncio.Task(cond.wait(), loop=self.loop)
        self.loop.call_soon(wait.cancel)
        self.assertRaises(asyncio.CancelledError, self.loop.run_until_complete,
                          wait)
        self.assertFalse(cond._waiters)
        self.assertTrue(cond.locked())
Beispiel #6
0
    def test_context_manager(self):
        cond = asyncio.Condition(loop=self.loop)

        @asyncio.coroutine
        def acquire_cond():
            raise Return((yield From(cond)))

        with self.loop.run_until_complete(acquire_cond()):
            self.assertTrue(cond.locked())

        self.assertFalse(cond.locked())
Beispiel #7
0
    def __init__(self, connection_factory, max_connections=6):
        assert max_connections > 0, \
            'num must be positive. got {}'.format(max_connections)

        self._connection_factory = connection_factory
        self.max_connections = max_connections
        self.ready = set()
        self.busy = set()
        self._lock = trollius.Lock()
        self._condition = trollius.Condition(lock=self._lock)
        self._closed = False
Beispiel #8
0
    def test_context_manager_no_yield(self):
        cond = asyncio.Condition(loop=self.loop)

        try:
            with cond:
                self.fail('RuntimeError is not raised in with expression')
        except RuntimeError as err:
            self.assertEqual(
                str(err),
                '"yield From" should be used as context manager expression')

        self.assertFalse(cond.locked())
Beispiel #9
0
    def test_repr(self):
        cond = asyncio.Condition(loop=self.loop)
        self.assertTrue('unlocked' in repr(cond))
        self.assertTrue(RGX_REPR.match(repr(cond)))

        self.loop.run_until_complete(cond.acquire())
        self.assertTrue('locked' in repr(cond))

        cond._waiters.append(mock.Mock())
        self.assertTrue('waiters:1' in repr(cond))
        self.assertTrue(RGX_REPR.match(repr(cond)))

        cond._waiters.append(mock.Mock())
        self.assertTrue('waiters:2' in repr(cond))
        self.assertTrue(RGX_REPR.match(repr(cond)))
Beispiel #10
0
    def test_wait_for(self):
        cond = asyncio.Condition(loop=self.loop)
        presult = False

        def predicate():
            return presult

        result = []

        @asyncio.coroutine
        def c1(result):
            yield From(cond.acquire())
            if (yield From(cond.wait_for(predicate))):
                result.append(1)
                cond.release()
            raise Return(True)

        t = asyncio.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())
Beispiel #11
0
    def test_explicit_lock(self):
        lock = asyncio.Lock(loop=self.loop)
        cond = asyncio.Condition(lock, loop=self.loop)

        self.assertIs(cond._lock, lock)
        self.assertIs(cond._loop, lock._loop)
Beispiel #12
0
 def test_notify_all_unacquired(self):
     cond = asyncio.Condition(loop=self.loop)
     self.assertRaises(RuntimeError, cond.notify_all)
Beispiel #13
0
 def test_wait_unacquired(self):
     cond = asyncio.Condition(loop=self.loop)
     self.assertRaises(RuntimeError, self.loop.run_until_complete,
                       cond.wait())
Beispiel #14
0
 def test_ctor_noloop(self):
     asyncio.set_event_loop(self.loop)
     cond = asyncio.Condition()
     self.assertIs(cond._loop, self.loop)