예제 #1
0
    def test_ctor_loop(self):
        loop = unittest.mock.Mock()
        sem = locks.Semaphore(loop=loop)
        self.assertIs(sem._loop, loop)

        sem = locks.Semaphore(loop=self.loop)
        self.assertIs(sem._loop, self.loop)
예제 #2
0
    def test_release_no_waiters(self):
        sem = locks.Semaphore(loop=self.loop)
        self.loop.run_until_complete(sem.acquire())
        self.assertTrue(sem.locked())

        sem.release()
        self.assertFalse(sem.locked())
예제 #3
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         sem = locks.Semaphore()
         self.assertIs(sem._loop, self.loop)
     finally:
         events.set_event_loop(None)
예제 #4
0
    def test_acquire_cancel(self):
        sem = locks.Semaphore(loop=self.loop)
        self.loop.run_until_complete(sem.acquire())

        acquire = tasks.Task(sem.acquire(), loop=self.loop)
        self.loop.call_soon(acquire.cancel)
        self.assertRaises(futures.CancelledError, self.loop.run_until_complete,
                          acquire)
        self.assertFalse(sem._waiters)
예제 #5
0
    def test_context_manager(self):
        sem = locks.Semaphore(2, loop=self.loop)

        @tasks.coroutine
        def acquire_lock():
            return (yield from sem)

        with self.loop.run_until_complete(acquire_lock()):
            self.assertFalse(sem.locked())
            self.assertEqual(1, sem._value)

            with self.loop.run_until_complete(acquire_lock()):
                self.assertTrue(sem.locked())

        self.assertEqual(2, sem._value)
예제 #6
0
    def test_semaphore(self):
        sem = locks.Semaphore(loop=self.loop)
        self.assertEqual(1, sem._value)

        @tasks.coroutine
        def acquire_lock():
            return (yield from sem)

        res = self.loop.run_until_complete(acquire_lock())

        self.assertTrue(res)
        self.assertTrue(sem.locked())
        self.assertEqual(0, sem._value)

        sem.release()
        self.assertFalse(sem.locked())
        self.assertEqual(1, sem._value)
예제 #7
0
    def test_release_not_acquired(self):
        sem = locks.Semaphore(bound=True, loop=self.loop)

        self.assertRaises(ValueError, sem.release)
예제 #8
0
    def test_acquire(self):
        sem = locks.Semaphore(3, loop=self.loop)
        result = []

        self.assertTrue(self.loop.run_until_complete(sem.acquire()))
        self.assertTrue(self.loop.run_until_complete(sem.acquire()))
        self.assertFalse(sem.locked())

        @tasks.coroutine
        def c1(result):
            yield from sem.acquire()
            result.append(1)
            return True

        @tasks.coroutine
        def c2(result):
            yield from sem.acquire()
            result.append(2)
            return True

        @tasks.coroutine
        def c3(result):
            yield from sem.acquire()
            result.append(3)
            return True

        @tasks.coroutine
        def c4(result):
            yield from sem.acquire()
            result.append(4)
            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([1], result)
        self.assertTrue(sem.locked())
        self.assertEqual(2, len(sem._waiters))
        self.assertEqual(0, sem._value)

        t4 = tasks.Task(c4(result), loop=self.loop)

        sem.release()
        sem.release()
        self.assertEqual(2, sem._value)

        test_utils.run_briefly(self.loop)
        self.assertEqual(0, sem._value)
        self.assertEqual([1, 2, 3], result)
        self.assertTrue(sem.locked())
        self.assertEqual(1, len(sem._waiters))
        self.assertEqual(0, sem._value)

        self.assertTrue(t1.done())
        self.assertTrue(t1.result())
        self.assertTrue(t2.done())
        self.assertTrue(t2.result())
        self.assertTrue(t3.done())
        self.assertTrue(t3.result())
        self.assertFalse(t4.done())

        # cleanup locked semaphore
        sem.release()
예제 #9
0
    def test_repr(self):
        sem = locks.Semaphore(loop=self.loop)
        self.assertTrue(repr(sem).endswith('[unlocked,value:1]>'))

        self.loop.run_until_complete(sem.acquire())
        self.assertTrue(repr(sem).endswith('[locked]>'))