def test_intermittent_iteration(self, capped): assert not capped.count() # Start generating entries. t = Thread(target=gen_log_entries, args=(capped, )) t.start() count = 0 seen = None for record in tail(capped, timeout=2, aggregate=True): if count == 50: # Records are pooled in batches, so even after the query is timed out-i.e. we take # too long to do something in one of our iterations-we may still recieve additional # records before the pool drains and the cursor needs to pull more data from the # server. To avoid weird test failures, we break early here. Symptoms show up as # this test failing with "<semi-random large int> == 200" in the final count. # If you need to worry about this (i.e. having your own retry), track last seen. break count += 1 seen = record['_id'] for record in tail(capped, {'_id': {'$gt': seen}}, timeout=2, aggregate=True): count += 1 if record['message'] == 'last': break t.join() assert capped.count() == 100 assert count == 1000
def test_intermittent_iteration(self, capped): assert not capped.count() # Start generating entries. t = Thread(target=gen_log_entries, args=(capped, )) t.start() count = 0 seen = None for record in tail(capped, timeout=2, aggregate=True): if count == 50: # Records are pooled in batches, so even after the query is timed out-i.e. we take # too long to do something in one of our iterations-we may still recieve additional # records before the pool drains and the cursor needs to pull more data from the # server. To avoid weird test failures, we break early here. Symptoms show up as # this test failing with "<semi-random large int> == 200" in the final count. # If you need to worry about this (i.e. having your own retry), track last seen. break count += 1 seen = record['_id'] for record in tail(capped, {'_id': { '$gt': seen }}, timeout=2, aggregate=True): count += 1 if record['message'] == 'last': break t.join() assert capped.count() == 100 assert count == 1000
def test_single(self, capped): assert capped.count() == 0 result = capped.insert_one({'message': 'first'}).inserted_id assert capped.count() == 1 first = next(tail(capped)) assert first['message'] == 'first' assert first['_id'] == result
def test_basic_timeout(self, capped): capped.insert_one({'message': 'first'}) start = time() result = list(tail(capped, timeout=0.5)) delta = time() - start assert len(result) == capped.count() assert 0.25 < delta < 0.75
def test_long_iteration(self, capped): assert not capped.count() # Start generating entries. t = Thread(target=gen_log_entries, args=(capped, )) t.start() count = 0 for record in tail(capped, timeout=5): count += 1 if record['message'] == 'last': break # Note that the collection only allows 100 entries... assert capped.count() == 100 # But we successfully saw all 1000 generated records. :) assert count == 1000 t.join()
def test_empty_trap(self, capped): with pytest.raises(ValueError): list(tail(capped))
def test_capped_trap(self, uncapped): with pytest.raises(TypeError): list(tail(uncapped))