def test_longer_random_sequence_of_queue_ops(): """A long random sequence of added and retrieved values""" q = RandomShufflingBuffer(100, 80) for _ in six.moves.xrange(10000): if q.can_add(): q.add_many(np.random.random((np.random.randint(1, 10),))) assert q.size < 100 + 10 for _ in range(np.random.randint(1, 10)): if not q.can_retrieve(): break # Make sure never get to less than `min_after_retrieve` elements assert 80 <= q.size q.retrieve()
def test_random_shuffling_buffer_can_add_retrieve_flags(): """Check can_add/can_retrieve flags at all possible states""" q = RandomShufflingBuffer(5, 3) # Empty buffer. Can start adding, nothing to retrieve yet assert q.size == 0 assert q.can_add() assert not q.can_retrieve() # Under min_after_retrieve elements, so can not retrieve just yet q.add_many([1, 2]) assert q.can_add() assert not q.can_retrieve() assert q.size == 2 # Got to min_after_retrieve elements, can start retrieving q.add_many([3]) assert q.can_retrieve() assert q.size == 3 # But when we retrieve we are again under min_after_retrieve, so can not retrieve again q.retrieve() assert not q.can_retrieve() assert q.size == 2 # Getting back to the retrievable state with enough items in the buffer q.add_many([4, 5]) assert q.can_add() assert q.can_retrieve() assert q.size == 4 # Can overrun the capacity (as long as below extra_capacity), but can not add if we are above # shuffling_buffer_capacity q.add_many([6, 7, 8, 9]) assert not q.can_add() assert q.can_retrieve() assert q.size == 8 # Getting one out. Still have more than shuffling_buffer_capacity q.retrieve() assert not q.can_add() assert q.can_retrieve() assert q.size == 7 # Retrieve enough to get back to addable state [q.retrieve() for _ in range(4)] assert q.can_add() assert q.can_retrieve() assert q.size == 3 # Retrieve the last element so we go under min_after_retrieve and can not retrieve any more q.retrieve() assert q.can_add() assert not q.can_retrieve() with pytest.raises(RuntimeError): q.retrieve() assert q.size == 2 # finish() will allow us to deplete the buffer completely q.finish() assert not q.can_add() assert q.can_retrieve() assert q.size == 2 q.retrieve() assert not q.can_add() assert q.can_retrieve() assert q.size == 1 q.retrieve() assert not q.can_add() assert not q.can_retrieve() assert q.size == 0