예제 #1
0
def test_with_events(num_threads):
    event = threading.Event()

    hits = []
    hits_before, hits_after = 0, 0

    clock = ThreadedClock(0)

    def hit_me():
        clock.sleep(0.1)
        hits.append(True)

    threads = []
    for _ in range(num_threads):
        th = threading.Thread(target=hit_me)
        th.daemon = True
        th.start()
        threads.append(th)

    clock.converge(threads=threads)
    for th in threads:
        clock.assert_waiting(th, 0.1)

    clock.tick(0.05)
    clock.converge(threads=threads)
    hits_before += len(hits)

    with pytest.raises(AssertionError):
        clock.assert_waiting(threads[0], 234)

    clock.tick(0.05)
    clock.converge(threads=threads)
    hits_after += len(hits)

    for th in threads:
        clock.assert_not_waiting(th)
        with pytest.raises(AssertionError):
            clock.assert_waiting(th, 0.1)

    assert hits_before == 0
    assert hits_after == num_threads
예제 #2
0
def test_with_events(num_threads):
    event = threading.Event()

    hits = []
    hits_before, hits_after = 0, 0

    clock = ThreadedClock(0)

    def hit_me():
        clock.sleep(0.1)
        hits.append(True)

    threads = []
    for _ in range(num_threads):
        th = threading.Thread(target=hit_me)
        th.daemon = True
        th.start()
        threads.append(th)

    clock.converge(threads=threads)
    for th in threads:
        clock.assert_waiting(th, 0.1)

    clock.tick(0.05)
    clock.converge(threads=threads)
    hits_before += len(hits)

    with pytest.raises(AssertionError):
        clock.assert_waiting(threads[0], 234)

    clock.tick(0.05)
    clock.converge(threads=threads)
    hits_after += len(hits)

    for th in threads:
        clock.assert_not_waiting(th)
        with pytest.raises(AssertionError):
            clock.assert_waiting(th, 0.1)

    assert hits_before == 0
    assert hits_after == num_threads
예제 #3
0
def test_not_converged():
    clock1 = ThreadedClock(0)
    clock2 = ThreadedClock(0)

    def run():
        clock1.sleep(1)
        clock2.sleep(1)

    th = threading.Thread(target=run)
    th.daemon = True
    th.start()

    assert clock1.converge(threads=[th])
    clock1.assert_waiting(th, 1)
    assert clock2.converge(threads=[th], timeout=0.1) is False
    clock2.assert_not_waiting(th)

    clock1.tick(1)
    clock2.tick(2)
    clock1.converge(threads=[th])
    clock2.converge(threads=[th])
    clock1.assert_not_waiting(th)
    clock2.assert_not_waiting(th)
예제 #4
0
def test_not_converged():
    clock1 = ThreadedClock(0)
    clock2 = ThreadedClock(0)

    def run():
        clock1.sleep(1)
        clock2.sleep(1)

    th = threading.Thread(target=run)
    th.daemon = True
    th.start()

    assert clock1.converge(threads=[th])
    clock1.assert_waiting(th, 1)
    assert clock2.converge(threads=[th], timeout=0.1) is False
    clock2.assert_not_waiting(th)

    clock1.tick(1)
    clock2.tick(2)
    clock1.converge(threads=[th])
    clock2.converge(threads=[th])
    clock1.assert_not_waiting(th)
    clock2.assert_not_waiting(th)