def test_failures_cancel_flush(self, caplog, num_failures,
                                   expected_log_count, expected_cancel):
        """Test that too many failures will cancel further processing on flush.

        2 messages should not cancel on flush
        3 or more should cancel on flush
        The number of received messages will be based on how many have
        been processed before the flush was initiated.
        """
        responses.add(responses.POST, "http://localhost", status=404)
        for _ in range(num_failures):
            report_start_event("name", "description")
        flush_events()
        # Force a context switch. Without this, it's possible that the
        # expected log message hasn't made it to the log file yet
        time.sleep(0.01)

        # If we've pushed a bunch of messages, any number could have been
        # processed before we get to the flush.
        assert (expected_log_count <= caplog.text.count("Failed posting event")
                <= num_failures)
        cancelled_message = (
            "Multiple consecutive failures in WebHookHandler. "
            "Cancelling all queued events")
        if expected_cancel:
            assert cancelled_message in caplog.text
        else:
            assert cancelled_message not in caplog.text
 def test_webhook_handler(self, caplog):
     """Test the happy path."""
     responses.add(responses.POST, "http://localhost", status=200)
     report_start_event("name", "description")
     flush_events()
     assert 1 == caplog.text.count(
         "Read from http://localhost (200, 0b) after 1 attempts")
Exemple #3
0
 def test_report_start_event_passes_something_with_as_string_to_handlers(
         self, instantiated_handler_registry):
     event_name, event_description = 'my_test_event', 'my description'
     events.report_start_event(event_name, event_description)
     expected_string_representation = ': '.join(
         ['start', event_name, event_description])
     for _, handler in (
             instantiated_handler_registry.registered_items.items()):
         self.assertEqual(1, handler.publish_event.call_count)
         event = handler.publish_event.call_args[0][0]
         self.assertEqual(expected_string_representation, event.as_string())
Exemple #4
0
 def test_report_start_event_passes_something_with_as_string_to_handlers(
         self, instantiated_handler_registry):
     event_name, event_description = 'my_test_event', 'my description'
     events.report_start_event(event_name, event_description)
     expected_string_representation = ': '.join(
         ['start', event_name, event_description])
     for _, handler in (
             instantiated_handler_registry.registered_items.items()):
         self.assertEqual(1, handler.publish_event.call_count)
         event = handler.publish_event.call_args[0][0]
         self.assertEqual(expected_string_representation, event.as_string())
    def test_background_processing(self, caplog):
        """Test that processing happens in background.

        In the non-flush case, ensure that the event is still posted.
        Since the event is posted in the background, wait while looping.
        """
        responses.add(responses.POST, "http://localhost", status=200)
        report_start_event("name", "description")
        start_time = time.time()
        while time.time() - start_time < 3:
            with suppress(AssertionError):
                assert ("Read from http://localhost (200, 0b) after 1 attempts"
                        in caplog.text)
                break
        else:
            pytest.fail("Never got expected log message")
    def test_multiple_failures_no_flush(self, caplog):
        """Test we don't cancel posting if flush hasn't been requested.

        Since processing happens in the background, wait in a loop
        for all messages to be posted
        """
        responses.add(responses.POST, "http://localhost", status=404)
        for _ in range(20):
            report_start_event("name", "description")
        start_time = time.time()
        while time.time() - start_time < 3:
            with suppress(AssertionError):
                assert 20 == caplog.text.count("Failed posting event")
                break
        else:
            pytest.fail("Expected 20 failures, only got "
                        f"{caplog.text.count('Failed posting event')}")
 def test_404(self, caplog):
     """Test failure"""
     responses.add(responses.POST, "http://localhost", status=404)
     report_start_event("name", "description")
     flush_events()
     assert 1 == caplog.text.count("Failed posting event")