Exemplo n.º 1
0
def test_get_blocking_waits_for_item():
    q = PriorityQueue()
    r = []
    t = thread_get(r, q)
    t.start()
    q.put('A', 1)
    t.join()
    assert r == ['A']
Exemplo n.º 2
0
def test_put_no_delay():
    q = PriorityQueue()
    e = []
    t = thread_put(e, q, 'A', 1)
    t.start()
    t.join()
    assert not e
    assert q.get() == 'A'
Exemplo n.º 3
0
def test_get_blocking_result_from_nonempty():
    q = PriorityQueue()
    q.put('A', 1)
    r = []
    t = thread_get(r, q)
    t.start()
    t.join()
    assert r == ['A']
Exemplo n.º 4
0
def test_get_n_waits_for_items():
    q = PriorityQueue()
    r = []
    t = thread_get_n(r, q, 2)
    t.start()
    q.put('A', 1)
    q.put('B', 1)
    t.join()
    assert r == ['A', 'B']
Exemplo n.º 5
0
def test_get_n_from_nonempty():
    q = PriorityQueue()
    q.put('A', 2)
    q.put('B', 1)
    r = []
    t = thread_get_n(r, q, 2)
    t.start()
    t.join()
    assert r == ['A', 'B']
Exemplo n.º 6
0
def test_put_to_full_timeout():
    q = PriorityQueue(1)
    q.put('A', 2)
    e = []
    t = thread_put(e, q, 'B', 1)
    t.start()
    t.join()
    assert q.get() == 'A'
    assert isinstance(e[0], Full)
Exemplo n.º 7
0
def test_get_n_timeout():
    q = PriorityQueue()
    q.put('A', 1)
    r = []
    t = thread_get_n(r, q, 2)
    t.start()
    t.join()
    assert len(r) == 1
    assert isinstance(r[0], Empty)
Exemplo n.º 8
0
def test_get_blocking_timeout_with_delay_doesnt_timout(timer):
    q = PriorityQueue()
    r = []
    t = thread_get(r, q, timeout=0.5)
    with timer as m:
        t.start()
        time.sleep(0.1)
        q.put('A', 1)
        t.join()
    assert r == ['A']
    assert m.interval < 0.3  # estimate, should definitely finish in time
Exemplo n.º 9
0
def test_multiple_put():
    q = PriorityQueue()
    e = []
    t1 = thread_put(e, q, 'A', 2)
    t2 = thread_put(e, q, 'B', 1)
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    assert not e
    assert q.get() == 'A'
    assert q.get() == 'B'
Exemplo n.º 10
0
def test_multiple_consumers_with_preloaded_queue():
    q = PriorityQueue()
    r = []
    t1 = thread_get(r, q)
    t2 = thread_get(r, q)
    q.put('B', 1)
    q.put('A', 2)
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    assert r == ['A', 'B']
Exemplo n.º 11
0
def test_multiple_consumers_with_delayed_put_timeout():
    q = PriorityQueue()
    r = []
    t1 = thread_get(r, q, timeout=0.3)
    t2 = thread_get(r, q, timeout=0.3)
    t1.start()
    t2.start()
    q.put('A', 1)
    t1.join()
    t2.join()
    assert len(r) == 2
    assert r[0] == 'A'
    assert isinstance(r[1], Empty)
Exemplo n.º 12
0
def test_get_blocking_timeout():
    q = PriorityQueue()
    r = []
    t = thread_get(r, q)
    t.start()
    t.join()
    assert len(r) == 1
    assert isinstance(r[0], Empty)
Exemplo n.º 13
0
def test_sums_priorities_and_doesnt_add():
    q = PriorityQueue()
    q.put('A', 5)
    q.put('A', 0)
    assert q.get_nowait() == 'A'
    with pytest.raises(Empty):
        q.get_nowait()
Exemplo n.º 14
0
def test_multiple_consumers_with_preloaded_queue():
    q = PriorityQueue()
    r = []
    t1 = thread_get_n(r, q, 2)
    t2 = thread_get_n(r, q, 3)
    q.put('A', 5)
    q.put('B', 4)
    q.put('C', 3)
    q.put('D', 2)
    q.put('E', 1)
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    assert r == ['A', 'B', 'C', 'D', 'E']
Exemplo n.º 15
0
def test_many_producers_and_one_getn_consumer():
    q = PriorityQueue()
    r = []
    threads = []
    t_get = thread_get_n(r, q, len(string.ascii_letters))
    t_get.start()
    for priority, letter in enumerate(string.ascii_letters):
        threads.append(thread_put(r, q, letter, -priority))  # Reverse pririty
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    t_get.join()
    assert r == list(string.ascii_letters)
Exemplo n.º 16
0
def test_many_producers_and_consumers():
    q = PriorityQueue()
    r = []
    threads = []
    for priority, letter in enumerate(string.ascii_letters):
        threads.append(thread_put(r, q, letter, priority))
        threads.append(thread_get(r, q))
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    assert sorted(r) == sorted(
        string.ascii_letters
    )  # Has to be sorted, put() is called in random order
Exemplo n.º 17
0
def get_n_keeps_correct_order_after_pulling():
    q = PriorityQueue()
    r = []
    t = thread_get_n(r, q, 2)
    t.start()
    q.put('A', 1)
    time.sleep(0.1)
    q.put('B', 2)
    t.join()
    assert r == ['B', 'A']
Exemplo n.º 18
0
def test_put_delay_full_delay(timer):
    q = PriorityQueue(1)
    q.put('A', 2)
    e = []
    t = thread_put(e, q, 'B', 1, timeout=0.3)
    with timer as m:
        t.start()
        time.sleep(0.1)
        assert q.qsize() == 1
        assert q.get() == 'A'
        t.join()
    assert not e
    assert q.get() == 'B'
    assert m.interval < 0.3
Exemplo n.º 19
0
def test_multiple_consumers_with_delayed_put():
    q = PriorityQueue()
    r = []
    t1 = thread_get(r, q, timeout=0.5)
    t2 = thread_get(r, q, timeout=0.5)
    t1.start()
    t2.start()
    q.put('A', 2)
    time.sleep(0.1)
    q.put('B', 1)
    t1.join()
    t2.join()
    assert r == ['A', 'B']
Exemplo n.º 20
0
def test_qsize_reflects_merging():
    q = PriorityQueue()
    q.put('A', 1)
    q.put('A', 1)
    assert q.qsize() == 1
Exemplo n.º 21
0
def test_put_get_one_item():
    q = PriorityQueue()
    q.put('A', 0)
    assert q.get_nowait() == 'A'
Exemplo n.º 22
0
def test_get_reflects_priority():
    q = PriorityQueue()
    q.put('A', 0)
    q.put('B', 1)
    assert q.get_nowait() == 'B'
    assert q.get_nowait() == 'A'
Exemplo n.º 23
0
def test_get_removes_item():
    q = PriorityQueue()
    q.put('A', 0)
    q.get_nowait()
    with pytest.raises(Empty):
        q.get_nowait()
Exemplo n.º 24
0
def test_sums_priorities():
    q = PriorityQueue()
    q.put('A', 1)
    q.put('B', 2)
    q.put('A', 2)
    assert q.get_nowait() == 'A'
Exemplo n.º 25
0
def test_get_stable_ordering():
    q = PriorityQueue()
    q.put('A', 0)
    q.put('B', 0)
    assert q.get_nowait() == 'A'
    assert q.get_nowait() == 'B'
Exemplo n.º 26
0
def test_get_empty():
    q = PriorityQueue()
    with pytest.raises(Empty):
        q.get(False)
Exemplo n.º 27
0
def test_sums_negative_priorities():
    q = PriorityQueue()
    q.put('A', 3)
    q.put('B', 2)
    q.put('A', -2)
    assert q.get_nowait() == 'B'
Exemplo n.º 28
0
def test_qsize():
    q = PriorityQueue()
    assert q.qsize() == 0
    q.put('A', 1)
    assert q.qsize() == 1
    q.put('B', 1)
    assert q.qsize() == 2
    q.get_nowait()
    assert q.qsize() == 1
Exemplo n.º 29
0
def test_empty():
    q = PriorityQueue()
    assert q.empty()
    q.put('A', 1)
    assert not q.empty()
Exemplo n.º 30
0
def test_get_empty_raises_exception():
    q = PriorityQueue()
    with pytest.raises(Empty):
        q.get_nowait()