|
| 1 | +package io.sentry.logger |
| 2 | + |
| 3 | +import io.sentry.ISentryClient |
| 4 | +import io.sentry.SentryLogEvent |
| 5 | +import io.sentry.SentryLogEvents |
| 6 | +import io.sentry.SentryLogLevel |
| 7 | +import io.sentry.SentryNanotimeDate |
| 8 | +import io.sentry.SentryOptions |
| 9 | +import io.sentry.protocol.SentryId |
| 10 | +import io.sentry.test.DeferredExecutorService |
| 11 | +import io.sentry.test.injectForField |
| 12 | +import kotlin.test.Test |
| 13 | +import kotlin.test.assertEquals |
| 14 | +import kotlin.test.assertFalse |
| 15 | +import kotlin.test.assertTrue |
| 16 | +import org.mockito.kotlin.argumentCaptor |
| 17 | +import org.mockito.kotlin.atLeast |
| 18 | +import org.mockito.kotlin.mock |
| 19 | +import org.mockito.kotlin.verify |
| 20 | + |
| 21 | +class LoggerBatchProcessorTest { |
| 22 | + @Test |
| 23 | + fun `drops log events after reaching MAX_QUEUE_SIZE limit`() { |
| 24 | + // given |
| 25 | + val mockClient = mock<ISentryClient>() |
| 26 | + val mockExecutor = DeferredExecutorService() |
| 27 | + val options = SentryOptions() |
| 28 | + val processor = LoggerBatchProcessor(options, mockClient) |
| 29 | + processor.injectForField("executorService", mockExecutor) |
| 30 | + |
| 31 | + for (i in 1..1001) { |
| 32 | + val logEvent = |
| 33 | + SentryLogEvent(SentryId(), SentryNanotimeDate(), "log message $i", SentryLogLevel.INFO) |
| 34 | + processor.add(logEvent) |
| 35 | + } |
| 36 | + |
| 37 | + // run twice since a non full batch would be scheduled at the end |
| 38 | + mockExecutor.runAll() |
| 39 | + mockExecutor.runAll() |
| 40 | + |
| 41 | + // assert that the transport received 1000 log events |
| 42 | + val captor = argumentCaptor<SentryLogEvents>() |
| 43 | + verify(mockClient, atLeast(1)).captureBatchedLogEvents(captor.capture()) |
| 44 | + |
| 45 | + val allCapturedEvents = mutableListOf<SentryLogEvent>() |
| 46 | + captor.allValues.forEach { logEvents -> allCapturedEvents.addAll(logEvents.items) } |
| 47 | + |
| 48 | + assertEquals(1000, allCapturedEvents.size) |
| 49 | + |
| 50 | + // assert that log 1001 did not make it but log 1000 did get sent |
| 51 | + val log1000Found = allCapturedEvents.any { it.body == "log message 1000" } |
| 52 | + val log1001Found = allCapturedEvents.any { it.body == "log message 1001" } |
| 53 | + |
| 54 | + assertTrue(log1000Found, "Log 1000 should have been sent") |
| 55 | + assertFalse(log1001Found, "Log 1001 should not have been sent") |
| 56 | + } |
| 57 | +} |
0 commit comments