|
| 1 | +import sys |
| 2 | +import threading |
| 3 | +import time |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +from logprise import Appriser, logger |
| 7 | + |
| 8 | + |
| 9 | +def test_uncaught_exception_hook(notify_mock, monkeypatch): |
| 10 | + """Test that uncaught exceptions trigger immediate notifications.""" |
| 11 | + # Save original excepthook |
| 12 | + original_excepthook = sys.excepthook |
| 13 | + |
| 14 | + # Create an Appriser instance |
| 15 | + appriser = Appriser() |
| 16 | + |
| 17 | + # Mock send_notification to track calls |
| 18 | + mock_send = MagicMock() |
| 19 | + monkeypatch.setattr(appriser, "send_notification", mock_send) |
| 20 | + |
| 21 | + # Simulate an uncaught exception |
| 22 | + try: |
| 23 | + # Trigger our custom excepthook |
| 24 | + sys.excepthook(ValueError, ValueError("Test exception"), None) |
| 25 | + finally: |
| 26 | + # Restore original excepthook |
| 27 | + sys.excepthook = original_excepthook |
| 28 | + |
| 29 | + # Verify send_notification was called |
| 30 | + mock_send.assert_called_once() |
| 31 | + |
| 32 | + |
| 33 | +def test_periodic_flush(notify_mock, monkeypatch): |
| 34 | + """Test that logs are periodically flushed.""" |
| 35 | + # Create an Appriser with a short flush interval for testing |
| 36 | + appriser = Appriser() |
| 37 | + appriser.flush_interval = 0.1 # 100ms for faster testing |
| 38 | + |
| 39 | + # Mock threading.Thread to control execution |
| 40 | + mock_thread = MagicMock() |
| 41 | + monkeypatch.setattr(threading, "Thread", mock_thread) |
| 42 | + |
| 43 | + # Re-initialize to trigger thread creation with mocked components |
| 44 | + appriser._start_periodic_flush() |
| 45 | + |
| 46 | + # Verify thread was started with correct parameters |
| 47 | + mock_thread.assert_called_once() |
| 48 | + assert mock_thread.call_args[1]["target"] == appriser._periodic_flush |
| 49 | + assert mock_thread.call_args[1]["daemon"] is True |
| 50 | + assert mock_thread.call_args[1]["name"] == "logprise-flush" |
| 51 | + mock_thread.return_value.start.assert_called_once() |
| 52 | + |
| 53 | + |
| 54 | +def test_periodic_flush_integration(notify_mock): |
| 55 | + """Test the periodic flush actually works (integration test).""" |
| 56 | + # Create an Appriser with a short flush interval |
| 57 | + appriser = Appriser(flush_interval=0.2) |
| 58 | + |
| 59 | + # Generate an error log (should be captured) |
| 60 | + logger.error("Test periodic flush") |
| 61 | + |
| 62 | + # Verify message is in the buffer |
| 63 | + assert len(appriser.buffer) == 1 |
| 64 | + |
| 65 | + # Wait for the periodic flush to happen |
| 66 | + time.sleep(0.3) # Wait longer than flush_interval |
| 67 | + |
| 68 | + # Buffer should be cleared and notification sent |
| 69 | + assert len(appriser.buffer) == 0 |
| 70 | + assert len(notify_mock) == 1 |
| 71 | + assert "Test periodic flush" in notify_mock[0]["body"] |
| 72 | + |
| 73 | + |
| 74 | +def test_stop_periodic_flush(): |
| 75 | + """Test that stop_periodic_flush properly terminates the flush thread.""" |
| 76 | + appriser = Appriser() |
| 77 | + |
| 78 | + # Create a mock thread |
| 79 | + mock_thread = MagicMock() |
| 80 | + appriser._flush_thread = mock_thread |
| 81 | + mock_thread.is_alive.return_value = True |
| 82 | + |
| 83 | + # Stop the periodic flush |
| 84 | + appriser.stop_periodic_flush() |
| 85 | + |
| 86 | + # Verify that stop_event was set and join was called |
| 87 | + assert appriser._stop_event.is_set() |
| 88 | + mock_thread.join.assert_called_once() |
| 89 | + |
| 90 | + |
| 91 | +def test_cleanup_method(notify_mock): |
| 92 | + """Test that cleanup method stops the flush thread and sends pending notifications.""" |
| 93 | + appriser = Appriser() |
| 94 | + |
| 95 | + # Mock stop_periodic_flush |
| 96 | + mock_stop = MagicMock() |
| 97 | + appriser.stop_periodic_flush = mock_stop |
| 98 | + |
| 99 | + # Add a log message |
| 100 | + logger.error("Test cleanup") |
| 101 | + |
| 102 | + # Call cleanup |
| 103 | + appriser.cleanup() |
| 104 | + |
| 105 | + # Verify stop_periodic_flush was called |
| 106 | + mock_stop.assert_called_once() |
| 107 | + |
| 108 | + # Verify notification was sent |
| 109 | + assert len(notify_mock) == 1 |
| 110 | + assert "Test cleanup" in notify_mock[0]["body"] |
| 111 | + assert len(appriser.buffer) == 0 |
| 112 | + |
| 113 | + |
| 114 | +def test_flush_only_if_buffer_has_content(notify_mock, monkeypatch): |
| 115 | + """Test that periodic flush only sends notifications if buffer has content.""" |
| 116 | + appriser = Appriser() |
| 117 | + |
| 118 | + # Empty the buffer |
| 119 | + appriser.buffer.clear() |
| 120 | + |
| 121 | + # Mock the stop_event.wait to return True to avoid infinite loop |
| 122 | + monkeypatch.setattr(appriser._stop_event, "wait", lambda timeout: True) |
| 123 | + |
| 124 | + # Create a mock for send_notification to track calls |
| 125 | + mock_send = MagicMock() |
| 126 | + monkeypatch.setattr(appriser, "send_notification", mock_send) |
| 127 | + |
| 128 | + # Simulate a periodic flush without any logs |
| 129 | + appriser._periodic_flush() |
| 130 | + |
| 131 | + # Verify send_notification was not called because buffer is empty |
| 132 | + mock_send.assert_not_called() |
| 133 | + |
| 134 | + # Reset the mock for the next test |
| 135 | + mock_send.reset_mock() |
| 136 | + |
| 137 | + # Add a log message |
| 138 | + logger.error("Test message") |
| 139 | + |
| 140 | + # Mock the stop_event.wait to return True again |
| 141 | + monkeypatch.setattr(appriser._stop_event, "wait", lambda timeout: True) |
| 142 | + |
| 143 | + # Now run _periodic_flush manually with content in the buffer |
| 144 | + # But we'll need to directly call the part that sends the notification |
| 145 | + # because the full method would exit due to our mock returning True |
| 146 | + if appriser.buffer: |
| 147 | + appriser.send_notification() |
| 148 | + |
| 149 | + # Now send_notification should be called |
| 150 | + mock_send.assert_called_once() |
0 commit comments