Ejemplo n.º 1
0
    def test_stop_waiting_removes_non_called_waiter(self, mode):
        req = Requester()
        req_waiter1 = Requester()
        req_waiter2 = Requester()

        lock = BaseLock('test', maxCount=1)
        access = mock.Mock(spec=LockAccess)
        access.mode = mode

        lock.claim(req, access)
        d1 = lock.waitUntilMaybeAvailable(req_waiter1, access)
        d2 = lock.waitUntilMaybeAvailable(req_waiter2, access)
        lock.release(req, access)
        yield flushEventualQueue()

        self.assertFalse(lock.isAvailable(req, access))
        self.assertTrue(lock.isAvailable(req_waiter1, access))
        self.assertFalse(lock.isAvailable(req_waiter2, access))
        self.assertTrue(d1.called)

        lock.stopWaitingUntilAvailable(req_waiter2, access, d2)
        self.assertFalse(lock.isAvailable(req, access))
        self.assertTrue(lock.isAvailable(req_waiter1, access))
        self.assertFalse(lock.isAvailable(req_waiter2, access))

        lock.claim(req_waiter1, access)
        lock.release(req_waiter1, access)
        self.assertTrue(lock.isAvailable(req, access))
        self.assertTrue(lock.isAvailable(req_waiter1, access))
        self.assertTrue(lock.isAvailable(req_waiter2, access))
Ejemplo n.º 2
0
    def test_stop_waiting_fires_deferred_if_not_woken(self, mode):
        req = Requester()
        req_waiter = Requester()

        lock = BaseLock('test', maxCount=1)
        access = mock.Mock(spec=LockAccess)
        access.mode = mode

        lock.claim(req, access)
        d = lock.waitUntilMaybeAvailable(req_waiter, access)
        lock.stopWaitingUntilAvailable(req_waiter, access, d)
        self.assertTrue(d.called)

        lock.release(req, access)
Ejemplo n.º 3
0
    def test_stop_waiting_ensures_deferred_was_previous_result_of_wait(self, mode):
        req = Requester()
        req_waiter = Requester()

        lock = BaseLock('test', maxCount=1)
        access = mock.Mock(spec=LockAccess)
        access.mode = mode

        lock.claim(req, access)

        lock.waitUntilMaybeAvailable(req_waiter, access)
        with self.assertRaises(AssertionError):
            wrong_d = defer.Deferred()
            lock.stopWaitingUntilAvailable(req_waiter, access, wrong_d)

        lock.release(req, access)
Ejemplo n.º 4
0
    def test_stop_waiting_does_not_fire_deferred_if_already_woken(self, mode):
        req = Requester()
        req_waiter = Requester()

        lock = BaseLock('test', maxCount=1)
        access = mock.Mock(spec=LockAccess)
        access.mode = mode

        lock.claim(req, access)
        d = lock.waitUntilMaybeAvailable(req_waiter, access)
        lock.release(req, access)
        yield flushEventualQueue()
        self.assertTrue(d.called)

        # note that if the function calls the deferred again, an exception would be thrown from
        # inside Twisted.
        lock.stopWaitingUntilAvailable(req_waiter, access, d)
Ejemplo n.º 5
0
    def test_stop_waiting_does_not_raise_after_release(self, mode):
        req = Requester()
        req_waiter = Requester()

        lock = BaseLock('test', maxCount=1)
        access = mock.Mock(spec=LockAccess)
        access.mode = mode

        lock.claim(req, access)
        d = lock.waitUntilMaybeAvailable(req_waiter, access)
        lock.release(req, access)
        self.assertFalse(lock.isAvailable(req, access))
        self.assertTrue(lock.isAvailable(req_waiter, access))

        lock.stopWaitingUntilAvailable(req_waiter, access, d)

        lock.claim(req_waiter, access)
        lock.release(req_waiter, access)
Ejemplo n.º 6
0
    def test_stop_waiting_raises_after_release(self, mode):
        req = Requester()
        req_waiter = Requester()

        lock = BaseLock('test', maxCount=1)
        access = mock.Mock(spec=LockAccess)
        access.mode = mode

        lock.claim(req, access)
        d = lock.waitUntilMaybeAvailable(req_waiter, access)
        lock.release(req, access)
        self.assertFalse(lock.isAvailable(req, access))
        self.assertTrue(lock.isAvailable(req_waiter, access))

        with self.assertRaises(AssertionError):
            lock.stopWaitingUntilAvailable(req_waiter, access, d)

        lock.claim(req_waiter, access)
        lock.release(req_waiter, access)
Ejemplo n.º 7
0
    def test_stop_waiting_wakes_up_next_deferred_if_already_woken(self, mode):
        req = Requester()
        req_waiter1 = Requester()
        req_waiter2 = Requester()

        lock = BaseLock('test', maxCount=1)
        access = mock.Mock(spec=LockAccess)
        access.mode = mode

        lock.claim(req, access)
        d1 = lock.waitUntilMaybeAvailable(req_waiter1, access)
        d2 = lock.waitUntilMaybeAvailable(req_waiter2, access)
        lock.release(req, access)
        yield flushEventualQueue()

        self.assertTrue(d1.called)
        self.assertFalse(d2.called)

        lock.stopWaitingUntilAvailable(req_waiter1, access, d1)

        yield flushEventualQueue()
        self.assertTrue(d2.called)