async def test_run__passes_correct_blocklists_to_subscriptions(mock_client): submission = MockSubmission("12322") api = MockExportAPI().with_submission(submission) watcher = SubscriptionWatcher(api, mock_client) method_called = MockMethod([submission]) watcher._get_new_results = method_called.async_call watcher.BACK_OFF = 1 watcher.blocklists = {156: {"test", "ych"}, -200: {"example"}} sub1 = MockSubscription("deer", 156) sub2 = MockSubscription("dog", -232) watcher.subscriptions = [sub1, sub2] task = asyncio.get_event_loop().create_task(watcher_killer(watcher)) # Run watcher await watcher.run() await task assert submission in sub1.submissions_checked assert len(sub1.blocklists) == 1 assert sub1.blocklists[0] in [ AndQuery([NotQuery(WordQuery("test")), NotQuery(WordQuery("ych"))]), AndQuery([NotQuery(WordQuery("ych")), NotQuery(WordQuery("test"))]) ] assert submission in sub2.submissions_checked assert len(sub2.blocklists) == 1 assert sub2.blocklists[0] == AndQuery([]) assert method_called.called
async def test_run__is_stopped_by_running_false(mock_client): api = MockExportAPI() s = SubscriptionWatcher(api, mock_client) # Shorten the wait s.BACK_OFF = 1 task = asyncio.get_event_loop().create_task(watcher_killer(s)) # Run watcher await s.run() assert True await task
async def test_run__calls_get_new_results(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) method_called = MockMethod([]) watcher._get_new_results = method_called.async_call # Shorten the wait watcher.BACK_OFF = 1 task = asyncio.get_event_loop().create_task(watcher_killer(watcher)) # Run watcher await watcher.run() await task assert method_called.called
async def test_run__sleeps_backoff_time(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) # Shorten the wait watcher.BACK_OFF = 3 api.call_after_x_browse = (lambda: watcher.stop(), 2) # Run watcher start_time = datetime.datetime.now() await watcher.run() end_time = datetime.datetime.now() time_waited = end_time - start_time assert 3 <= time_waited.seconds <= 5
async def test_run__can_exit_fast(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) # Shorten the wait watcher.BACK_OFF = 3 task = asyncio.get_event_loop().create_task(watcher_killer(watcher)) # Run watcher start_time = datetime.datetime.now() await watcher.run() end_time = datetime.datetime.now() await task time_waited = end_time - start_time assert time_waited.seconds <= 1
async def test_run__failed_to_send_doesnt_kill_watcher(mock_client): submission = MockSubmission("12322") api = MockExportAPI().with_browse_results([submission], 1) watcher = SubscriptionWatcher(api, mock_client) submission.send_message = lambda *args: (_ for _ in ()).throw(Exception) watcher.BACK_OFF = 3 sub1 = MockSubscription("deer", 0) watcher.subscriptions = [sub1] api.call_after_x_browse = (lambda: watcher.stop(), 2) # Run watcher start_time = datetime.datetime.now() await watcher.run() end_time = datetime.datetime.now() time_waited = end_time - start_time assert 3 <= time_waited.seconds <= 5
async def test_run__calls_update_latest_ids(mock_client): submission1 = MockSubmission("12322") submission2 = MockSubmission("12324") api = MockExportAPI().with_submissions([submission1, submission2]) watcher = SubscriptionWatcher(api, mock_client) mock_new_results = MockMethod([submission1, submission2]) watcher._get_new_results = mock_new_results.async_call mock_update_latest = MockMethod() watcher._update_latest_ids = mock_update_latest.call # Shorten the wait watcher.BACK_OFF = 1 task = asyncio.get_event_loop().create_task(watcher_killer(watcher)) # Run watcher await watcher.run() await task assert mock_update_latest.called assert mock_update_latest.args[0] == [submission2]
async def test_run__checks_all_new_results(mock_client): submission1 = MockSubmission("12322") submission2 = MockSubmission("12324") api = MockExportAPI().with_submissions([submission1, submission2]) watcher = SubscriptionWatcher(api, mock_client) method_called = MockMethod([submission1, submission2]) watcher._get_new_results = method_called.async_call watcher.BACK_OFF = 1 sub = MockSubscription("deer", 0) watcher.subscriptions = [sub] task = asyncio.get_event_loop().create_task(watcher_killer(watcher)) # Run watcher await watcher.run() await task assert submission1 in sub.submissions_checked assert submission2 in sub.submissions_checked assert method_called.called