Exemple #1
0
    def test_multiple_threads(self):
        lock = TimeoutableLock()

        class Foo:
            pass

        counter = Foo()
        counter.assert_count = 0

        def start_and_join(thread_count):
            threads = [
                AcquireHoldRelease(lock, 0.1, 0.3, counter)
                for _ in range(thread_count)
            ]
            for thread in threads:
                thread.start()
            for thread in threads:
                thread.join()

        start_and_join(3)
        self.failIf(counter.assert_count)
        start_and_join(4)
        self.assertEqual(counter.assert_count, 1)
        start_and_join(3)
        self.assertEqual(counter.assert_count, 1)
Exemple #2
0
 def test_blocking_acquire(self):
     lock = TimeoutableLock()
     self.assert_(lock.acquire())
     t = AcquireWithTimeout(lock, None)
     t.start()
     lock.release()
     t.join()
     self.failIf(lock.acquire(False))
Exemple #3
0
    def test_acquire_early(self):
        lock = TimeoutableLock()
        self.assert_(lock.acquire())
        t = AcquireWithTimeout(lock, 10)

        before = time.time()
        t.start()
        lock.release()
        t.join()
        self.assert_(time.time() - before < 1)
        self.failIf(lock.acquire(False))
Exemple #4
0
    def test_basic(self):
        lock = TimeoutableLock()

        self.assert_(lock.acquire())
        self.failIf(lock.acquire(False))

        lock.release()
        self.assert_(lock.acquire())

        lock.release()
        self.assertRaises(RuntimeError, lock.release)
Exemple #5
0
 def test_timeout(self):
     lock = TimeoutableLock()
     self.assert_(lock.acquire())
     before = time.time()
     self.failIf(lock.acquire(timeout=0.25))
     self.assertAlmostEqual(0.25, time.time() - before, 1)