Пример #1
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()
Пример #2
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']
Пример #3
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']
Пример #4
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
Пример #5
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']
Пример #6
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)
Пример #7
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']
Пример #8
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)
Пример #9
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']
Пример #10
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
Пример #11
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']
Пример #12
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']
Пример #13
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)
Пример #14
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
Пример #15
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']
Пример #16
0
def test_qsize_reflects_merging():
    q = PriorityQueue()
    q.put('A', 1)
    q.put('A', 1)
    assert q.qsize() == 1
Пример #17
0
def test_zero_capacity_is_unlimited():
    q = PriorityQueue(0)
    q.put('A', 1)
    q.put('B', 1)
    assert q.qsize() == 2
Пример #18
0
def test_put_full_raises_exception():
    q = PriorityQueue(1)
    q.put('A', 1)
    with pytest.raises(Full):
        q.put_nowait('B', 1)
Пример #19
0
def test_put_duplicate_to_full():
    q = PriorityQueue(1)
    q.put('A', 1)
    assert q.full()
    q.put('A', 1)
    assert q.full()
Пример #20
0
def test_put_get_one_item():
    q = PriorityQueue()
    q.put('A', 0)
    assert q.get_nowait() == 'A'
Пример #21
0
def test_get_removes_item():
    q = PriorityQueue()
    q.put('A', 0)
    q.get_nowait()
    with pytest.raises(Empty):
        q.get_nowait()
Пример #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'
Пример #23
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'
Пример #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'
Пример #25
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'
Пример #26
0
def test_empty():
    q = PriorityQueue()
    assert q.empty()
    q.put('A', 1)
    assert not q.empty()