def setUp(self):
     super(TestMultiLock, self).setUp()
     self.lock_one = defer.DeferredLock()
     self.lock_two = defer.DeferredLock()
     self.multi_lock = MultiLock(self.lock_one, self.lock_two)
     self.count = 0
 def setUp(self):
     super(TestMultiLock, self).setUp()
     self.lock_one = defer.DeferredLock()
     self.lock_two = defer.DeferredLock()
     self.multi_lock = MultiLock(self.lock_one, self.lock_two)
     self.count = 0
class TestMultiLock(TestCase):
    def setUp(self):
        super(TestMultiLock, self).setUp()
        self.lock_one = defer.DeferredLock()
        self.lock_two = defer.DeferredLock()
        self.multi_lock = MultiLock(self.lock_one, self.lock_two)
        self.count = 0

    def callback(self):
        self.count += 1

    def test_run_does_not_wait_when_there_is_no_need_to(self):
        """Multilock.run will run any given task if it's not locked and
        there's no task currently running.
        """
        self.multi_lock.run(self.callback)
        self.assertEquals(self.count, 1, "self.callback should have run.")

        self.multi_lock.run(self.callback)
        self.assertEquals(self.count, 2,
                          "self.callback should have run twice.")

    def test_run_waits_for_first_lock(self):
        """MultiLock.run acquires the first lock before running a function."""
        # Keep lock_one busy.
        deferred = defer.Deferred()
        self.lock_one.run(lambda: deferred)

        # Run self.callback when self.multi_lock is acquired.
        self.multi_lock.run(self.callback)
        self.assertEquals(self.count, 0,
                          "self.callback should not have run yet.")

        # Release lock_one.
        deferred.callback(None)

        # multi_lock will now have been able to acquire both semaphores, and
        # so it will have run its task.
        self.assertEquals(self.count, 1, "self.callback should have run.")

    def test_run_waits_for_second_lock(self):
        """MultiLock.run acquires the second lock before running functions."""
        # Keep lock_two busy.
        deferred = defer.Deferred()
        self.lock_two.run(lambda: deferred)

        # Run self.callback when self.multi_lock is acquired.
        self.multi_lock.run(self.callback)
        self.assertEquals(self.count, 0,
                          "self.callback should not have run yet.")

        # Release lock_two.
        deferred.callback(None)

        # multi_lock will now have been able to acquire both semaphores, and
        # so it will have run its task.
        self.assertEquals(self.count, 1, "self.callback should have run.")

    def test_run_waits_for_current_task(self):
        """MultiLock.run waits the end of the current task before running the
        next.
        """
        # Keep multi_lock busy.
        deferred = defer.Deferred()
        self.multi_lock.run(lambda: deferred)

        # Run self.callback when self.multi_lock is acquired.
        self.multi_lock.run(self.callback)
        self.assertEquals(self.count, 0,
                          "self.callback should not have run yet.")

        # Release lock_one.
        deferred.callback(None)

        # multi_lock will now have been able to acquire both semaphores, and
        # so it will have run its task.
        self.assertEquals(self.count, 1, "self.callback should have run.")
class TestMultiLock(TestCase):

    def setUp(self):
        super(TestMultiLock, self).setUp()
        self.lock_one = defer.DeferredLock()
        self.lock_two = defer.DeferredLock()
        self.multi_lock = MultiLock(self.lock_one, self.lock_two)
        self.count = 0

    def callback(self):
        self.count += 1

    def test_run_does_not_wait_when_there_is_no_need_to(self):
        """Multilock.run will run any given task if it's not locked and
        there's no task currently running.
        """
        self.multi_lock.run(self.callback)
        self.assertEquals(self.count, 1, "self.callback should have run.")

        self.multi_lock.run(self.callback)
        self.assertEquals(
            self.count, 2, "self.callback should have run twice.")

    def test_run_waits_for_first_lock(self):
        """MultiLock.run acquires the first lock before running a function."""
        # Keep lock_one busy.
        deferred = defer.Deferred()
        self.lock_one.run(lambda: deferred)

        # Run self.callback when self.multi_lock is acquired.
        self.multi_lock.run(self.callback)
        self.assertEquals(
            self.count, 0, "self.callback should not have run yet.")

        # Release lock_one.
        deferred.callback(None)

        # multi_lock will now have been able to acquire both semaphores, and
        # so it will have run its task.
        self.assertEquals(self.count, 1, "self.callback should have run.")

    def test_run_waits_for_second_lock(self):
        """MultiLock.run acquires the second lock before running functions."""
        # Keep lock_two busy.
        deferred = defer.Deferred()
        self.lock_two.run(lambda: deferred)

        # Run self.callback when self.multi_lock is acquired.
        self.multi_lock.run(self.callback)
        self.assertEquals(
            self.count, 0, "self.callback should not have run yet.")

        # Release lock_two.
        deferred.callback(None)

        # multi_lock will now have been able to acquire both semaphores, and
        # so it will have run its task.
        self.assertEquals(self.count, 1, "self.callback should have run.")

    def test_run_waits_for_current_task(self):
        """MultiLock.run waits the end of the current task before running the
        next.
        """
        # Keep multi_lock busy.
        deferred = defer.Deferred()
        self.multi_lock.run(lambda: deferred)

        # Run self.callback when self.multi_lock is acquired.
        self.multi_lock.run(self.callback)
        self.assertEquals(
            self.count, 0, "self.callback should not have run yet.")

        # Release lock_one.
        deferred.callback(None)

        # multi_lock will now have been able to acquire both semaphores, and
        # so it will have run its task.
        self.assertEquals(self.count, 1, "self.callback should have run.")