For #18836: prevent StartupActivityLog from growing infinitely.

We do this is as a separate commit over the original implementation
because it's simpler to implement the class without this optimization.
upstream-sync
Michael Comella 3 years ago committed by Michael Comella
parent cde954f3a7
commit e864e74960

@ -23,6 +23,10 @@ private val logger = Logger("StartupActivityLog")
* A record of the [Activity] created, started, and stopped events as well as [Application]
* foreground and background events. See [log] for the log. This class is expected to be
* registered in [Application.onCreate] by calling [registerInAppOnCreate].
*
* To prevent this list from growing infinitely, we clear the list when the application is stopped.
* This is acceptable from the current requirements: we never need to inspect more than the current
* start up.
*/
class StartupActivityLog {
@ -66,8 +70,9 @@ class StartupActivityLog {
}
override fun onStop(owner: LifecycleOwner) {
_log.add(LogEntry.AppStopped)
logEntries()
_log.clear() // Optimization: see class kdoc for details.
_log.add(LogEntry.AppStopped)
}
}

@ -50,15 +50,20 @@ class StartupActivityLogTest {
@Test // we test start and stop individually due to the clear-on-stop behavior.
fun `WHEN app observer start is called THEN it is added directly to the log`() {
assertTrue(log.log.isEmpty())
val expected = mutableListOf<LogEntry>()
appObserver.onStart(mockk())
expected.add(LogEntry.AppStarted)
assertEquals(expected, log.log)
assertEquals(listOf(LogEntry.AppStarted), log.log)
appObserver.onStart(mockk())
assertEquals(listOf(LogEntry.AppStarted, LogEntry.AppStarted), log.log)
}
@Test // we test start and stop individually due to the clear-on-stop behavior.
fun `WHEN app observer stop is called THEN it is added directly to the log`() {
assertTrue(log.log.isEmpty())
appObserver.onStop(mockk())
expected.add(LogEntry.AppStopped)
assertEquals(expected, log.log)
assertEquals(listOf(LogEntry.AppStopped), log.log)
}
@Test
@ -81,6 +86,19 @@ class StartupActivityLogTest {
assertEquals(expected, log.log)
}
@Test
fun `WHEN app STOPPED is called THEN the log is emptied expect for the stop event`() {
assertTrue(log.log.isEmpty())
activityCallbacks.onActivityCreated(mockk(), null)
activityCallbacks.onActivityStarted(mockk())
appObserver.onStart(mockk())
assertEquals(3, log.log.size)
appObserver.onStop(mockk())
assertEquals(listOf(LogEntry.AppStopped), log.log)
}
@Test
fun `GIVEN debug log level WHEN logEntries is called THEN there is no logcat call`() {
log.logEntries(logger, Priority.DEBUG)

Loading…
Cancel
Save