|
1 | 1 | package io.sentry.logger |
2 | 2 |
|
| 3 | +import io.sentry.DataCategory |
3 | 4 | import io.sentry.ISentryClient |
4 | 5 | import io.sentry.SentryLogEvent |
5 | 6 | import io.sentry.SentryLogEvents |
6 | 7 | import io.sentry.SentryLogLevel |
7 | 8 | import io.sentry.SentryNanotimeDate |
8 | 9 | import io.sentry.SentryOptions |
| 10 | +import io.sentry.clientreport.ClientReportTestHelper |
| 11 | +import io.sentry.clientreport.DiscardReason |
| 12 | +import io.sentry.clientreport.DiscardedEvent |
9 | 13 | import io.sentry.protocol.SentryId |
10 | 14 | import io.sentry.test.DeferredExecutorService |
11 | 15 | import io.sentry.test.injectForField |
| 16 | +import io.sentry.util.JsonSerializationUtils |
12 | 17 | import kotlin.test.Test |
13 | 18 | import kotlin.test.assertEquals |
14 | 19 | import kotlin.test.assertFalse |
@@ -54,4 +59,43 @@ class LoggerBatchProcessorTest { |
54 | 59 | assertTrue(log1000Found, "Log 1000 should have been sent") |
55 | 60 | assertFalse(log1001Found, "Log 1001 should not have been sent") |
56 | 61 | } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun `records client report when log event is dropped due to queue overflow`() { |
| 65 | + // given |
| 66 | + val mockClient = mock<ISentryClient>() |
| 67 | + val mockExecutor = DeferredExecutorService() |
| 68 | + val options = SentryOptions() |
| 69 | + val processor = LoggerBatchProcessor(options, mockClient) |
| 70 | + processor.injectForField("executorService", mockExecutor) |
| 71 | + |
| 72 | + // fill the queue to MAX_QUEUE_SIZE |
| 73 | + for (i in 1..1000) { |
| 74 | + val logEvent = |
| 75 | + SentryLogEvent(SentryId(), SentryNanotimeDate(), "log message $i", SentryLogLevel.INFO) |
| 76 | + processor.add(logEvent) |
| 77 | + } |
| 78 | + |
| 79 | + // add one more log event that should be dropped |
| 80 | + val droppedLogEvent = |
| 81 | + SentryLogEvent(SentryId(), SentryNanotimeDate(), "dropped log", SentryLogLevel.INFO) |
| 82 | + processor.add(droppedLogEvent) |
| 83 | + |
| 84 | + // calculate expected bytes for the dropped log event |
| 85 | + val expectedBytes = |
| 86 | + JsonSerializationUtils.byteSizeOf(options.serializer, options.logger, droppedLogEvent) |
| 87 | + |
| 88 | + // verify that a client report was recorded for the dropped log item and bytes |
| 89 | + val expectedEvents = |
| 90 | + mutableListOf( |
| 91 | + DiscardedEvent(DiscardReason.QUEUE_OVERFLOW.reason, DataCategory.LogItem.category, 1), |
| 92 | + DiscardedEvent( |
| 93 | + DiscardReason.QUEUE_OVERFLOW.reason, |
| 94 | + DataCategory.Attachment.category, |
| 95 | + expectedBytes, |
| 96 | + ), |
| 97 | + ) |
| 98 | + |
| 99 | + ClientReportTestHelper.assertClientReport(options.clientReportRecorder, expectedEvents) |
| 100 | + } |
57 | 101 | } |
0 commit comments