def test_run__passes_correct_blocklists_to_subscriptions(self, bot):
        submission = MockSubmission("12322")
        api = MockExportAPI().with_submission(submission)
        watcher = SubscriptionWatcher(api, bot)
        method_called = MockMethod([submission])
        watcher._get_new_results = method_called.call
        watcher.BACK_OFF = 1
        watcher.blocklists = {156: {"test", "ych"}, -200: {"example"}}
        sub1 = MockSubscription("deer", 156)
        sub2 = MockSubscription("dog", -232)
        watcher.subscriptions = [sub1, sub2]

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert submission in sub1.submissions_checked
        assert len(sub1.blocklists) == 1
        assert len(sub1.blocklists[0]) == 2
        assert "test" in sub1.blocklists[0]
        assert "ych" in sub1.blocklists[0]
        assert submission in sub2.submissions_checked
        assert len(sub2.blocklists) == 1
        assert len(sub2.blocklists[0]) == 0
        assert method_called.called
    def test_run__is_stopped_by_running_false(self, bot):
        api = MockExportAPI()
        s = SubscriptionWatcher(api, bot)
        # Shorten the wait
        s.BACK_OFF = 1

        thread = Thread(target=lambda: self.watcher_killer(s))
        thread.start()

        # Run watcher
        s.run()

        assert True
        thread.join()
    def test_run__calls_get_new_results(self, bot):
        api = MockExportAPI()
        watcher = SubscriptionWatcher(api, bot)
        method_called = MockMethod([])
        watcher._get_new_results = method_called.call
        # Shorten the wait
        watcher.BACK_OFF = 1

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert method_called.called
    def test_run__sleeps_backoff_time(self, bot):
        api = MockExportAPI()
        watcher = SubscriptionWatcher(api, bot)
        # Shorten the wait
        watcher.BACK_OFF = 3

        api.call_after_x_browse = (lambda: watcher.stop(), 2)

        # Run watcher
        start_time = datetime.datetime.now()
        watcher.run()
        end_time = datetime.datetime.now()

        time_waited = end_time - start_time
        assert 3 <= time_waited.seconds <= 5
    def test_run__failed_to_send_doesnt_kill_watcher(self, bot):
        submission = MockSubmission("12322")
        api = MockExportAPI().with_browse_results([submission], 1)
        watcher = SubscriptionWatcher(api, bot)
        watcher._send_update = 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()
        watcher.run()
        end_time = datetime.datetime.now()

        time_waited = end_time - start_time
        assert 3 <= time_waited.seconds <= 5
    def test_run__can_exit_fast(self, bot):
        api = MockExportAPI()
        watcher = SubscriptionWatcher(api, bot)
        # Shorten the wait
        watcher.BACK_OFF = 3

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()

        # Run watcher
        start_time = datetime.datetime.now()
        watcher.run()
        end_time = datetime.datetime.now()
        thread.join()

        time_waited = end_time - start_time
        assert time_waited.seconds <= 1
    def test_run__calls_update_latest_ids(self, bot):
        submission1 = MockSubmission("12322")
        submission2 = MockSubmission("12324")
        api = MockExportAPI().with_submissions([submission1, submission2])
        watcher = SubscriptionWatcher(api, bot)
        mock_new_results = MockMethod([submission1, submission2])
        watcher._get_new_results = mock_new_results.call
        mock_update_latest = MockMethod()
        watcher._update_latest_ids = mock_update_latest.call
        # Shorten the wait
        watcher.BACK_OFF = 1

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert mock_update_latest.called
        assert mock_update_latest.args[0] == [submission2]
    def test_run__checks_all_new_results(self, bot):
        submission1 = MockSubmission("12322")
        submission2 = MockSubmission("12324")
        api = MockExportAPI().with_submissions([submission1, submission2])
        watcher = SubscriptionWatcher(api, bot)
        method_called = MockMethod([submission1, submission2])
        watcher._get_new_results = method_called.call
        watcher.BACK_OFF = 1
        sub = MockSubscription("deer", 0)
        watcher.subscriptions = [sub]

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert submission1 in sub.submissions_checked
        assert submission2 in sub.submissions_checked
        assert method_called.called