Example #1
0
def test_parallel_threads():
    # Check that ReentrancyLock serializes work in parallel threads.
    #
    # The test is not fully deterministic, and may succeed falsely if
    # the timings go wrong.

    lock = ReentrancyLock("failure")

    failflag = [False]
    exceptions_raised = []

    def worker(k):
        try:
            with lock:
                assert_(not failflag[0])
                failflag[0] = True
                time.sleep(0.1 * k)
                assert_(failflag[0])
                failflag[0] = False
        except Exception:
            exceptions_raised.append(traceback.format_exc(2))

    threads = [
        threads_new.Thread(target=lambda k=k: worker(k)) for k in range(3)
    ]
    for t in threads:
        t.start()
    for t in threads:
        t.join()

    exceptions_raised = "\n".join(exceptions_raised)
    assert_(not exceptions_raised, exceptions_raised)
Example #2
0
def test_parallel_threads():
    results = []

    lock = ReentrancyLock("failure")

    def worker(k):
        with lock:
            time.sleep(0.01 * (3 - k))
            results.append(k)

    threads = [
        threading.Thread(target=lambda k=k: worker(k)) for k in range(3)
    ]
    for t in threads:
        t.start()
    for t in threads:
        t.join()

    assert_equal(results, sorted(results))