def test_flush(self): buffer = Buffer(count_threshold=100, time_threshold=10) payload = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] records = [Record(payload=p) for p in payload] buffer.flush = False rvA = buffer(records[:6]) buffer.flush = True rvB = buffer(records[6:]) self.assertEqual(rvA, [], 'Should not contain any records yet') self.assertEqual(rvB, records, 'Should contain all records')
def test_flush(self, inlet, outlet): buffer = Buffer(count_threshold=100, time_threshold=10) payload = [1, 2, 3, 4] records = [Record(payload=p) for p in payload] link = Link(inlet, outlet, interval=1, processors=buffer, copy_records=False) inlet._pull = pull_mock(records[:2]) link.transfer() outlet._push.assert_called_with([], mock.ANY) # no records yet inlet._pull = pull_mock(records[2:]) link.transfer() outlet._push.assert_called_with([], mock.ANY) # no records yet buffer.flush = True inlet._pull = pull_mock([]) link.transfer() outlet._push.assert_called_with( records, mock.ANY) # all records should be flushed
def test_flush_after_shutdown(self, inlet, outlet): buffer = Buffer(count_threshold=100, time_threshold=10) counter_dict = {'counter': 0, 'records': []} link = Link(inlet, outlet, interval=0.01, processors=buffer, copy_records=False) planner = SchedulePlanner(link, refresh_interval=0.01) async def pull_coro(_): counter_dict['counter'] += 1 record = Record(payload=counter_dict['counter']) counter_dict['records'].append(record) return [record] mock_pull = MagicMock(side_effect=pull_coro) inlet._pull = mock_pull th = Thread(target=planner.start, daemon=True) th.start() time.sleep(0.1) planner.shutdown() th.join() calls = outlet._push.call_args_list for c in calls: self.assertEqual(c(), [], 'Should only contain empty record lists.') self.assertEqual(buffer.records, counter_dict['records'], 'All records should be stored in the buffer') planner.force_transfer() self.assertEqual(outlet._push.call_args(), [], 'Should return empty record list') buffer.flush = True planner.force_transfer() self.assertEqual(outlet._push.call_args[0][0], counter_dict['records'], 'Should return all records')