@@ -5,12 +5,14 @@ import io.sentry.DateUtils
55import io.sentry.IContinuousProfiler
66import io.sentry.IScope
77import io.sentry.IScopes
8+ import io.sentry.ISentryClient
89import io.sentry.ReplayController
910import io.sentry.ScopeCallback
1011import io.sentry.SentryLevel
1112import io.sentry.SentryOptions
1213import io.sentry.Session
1314import io.sentry.Session.State
15+ import io.sentry.test.ImmediateExecutorService
1416import io.sentry.transport.ICurrentDateProvider
1517import kotlin.test.BeforeTest
1618import kotlin.test.Test
@@ -36,10 +38,13 @@ class LifecycleWatcherTest {
3638 val replayController = mock<ReplayController >()
3739 val continuousProfiler = mock<IContinuousProfiler >()
3840
41+ val client = mock<ISentryClient >()
42+
3943 fun getSUT (
4044 sessionIntervalMillis : Long = 0L,
4145 enableAutoSessionTracking : Boolean = true,
4246 enableAppLifecycleBreadcrumbs : Boolean = true,
47+ enableLogFlushing : Boolean = true,
4348 session : Session ? = null,
4449 ): LifecycleWatcher {
4550 val argumentCaptor: ArgumentCaptor <ScopeCallback > =
@@ -49,15 +54,20 @@ class LifecycleWatcherTest {
4954 whenever(scopes.configureScope(argumentCaptor.capture())).thenAnswer {
5055 argumentCaptor.value.run (scope)
5156 }
57+ whenever(scope.client).thenReturn(client)
58+
5259 options.setReplayController(replayController)
5360 options.setContinuousProfiler(continuousProfiler)
61+ options.executorService = ImmediateExecutorService ()
5462 whenever(scopes.options).thenReturn(options)
63+ whenever(scopes.globalScope).thenReturn(scope)
5564
5665 return LifecycleWatcher (
5766 scopes,
5867 sessionIntervalMillis,
5968 enableAutoSessionTracking,
6069 enableAppLifecycleBreadcrumbs,
70+ enableLogFlushing,
6171 dateProvider,
6272 )
6373 }
@@ -295,4 +305,27 @@ class LifecycleWatcherTest {
295305 watcher.onBackground()
296306 verify(fixture.replayController, timeout(10000 )).stop()
297307 }
308+
309+ @Test
310+ fun `flush logs when going in background` () {
311+ val watcher = fixture.getSUT(enableLogFlushing = true )
312+
313+ watcher.onForeground()
314+ watcher.onBackground()
315+
316+ watcher.onForeground()
317+ watcher.onBackground()
318+
319+ verify(fixture.client, times(2 )).flushLogs(any())
320+ }
321+
322+ @Test
323+ fun `do not flush logs when going in background when logging is disabled` () {
324+ val watcher = fixture.getSUT(enableLogFlushing = false )
325+
326+ watcher.onForeground()
327+ watcher.onBackground()
328+
329+ verify(fixture.client, never()).flushLogs(any())
330+ }
298331}
0 commit comments