From b83975ba724a5223027b43c9975edfcae030ae00 Mon Sep 17 00:00:00 2001 From: Cristian Monforte Date: Mon, 17 Nov 2025 17:16:00 +0100 Subject: [PATCH] Fix flaky test on event repository. --- .../metrics/store/EventRepository.kt | 10 +----- .../metrics/store/RealEventRepositoryTest.kt | 36 +++++++------------ 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/attributed-metrics/attributed-metrics-impl/src/main/java/com/duckduckgo/app/attributed/metrics/store/EventRepository.kt b/attributed-metrics/attributed-metrics-impl/src/main/java/com/duckduckgo/app/attributed/metrics/store/EventRepository.kt index d2d8bc4016dc..2bbecc6527d7 100644 --- a/attributed-metrics/attributed-metrics-impl/src/main/java/com/duckduckgo/app/attributed/metrics/store/EventRepository.kt +++ b/attributed-metrics/attributed-metrics-impl/src/main/java/com/duckduckgo/app/attributed/metrics/store/EventRepository.kt @@ -17,12 +17,8 @@ package com.duckduckgo.app.attributed.metrics.store import com.duckduckgo.app.attributed.metrics.api.EventStats -import com.duckduckgo.app.di.AppCoroutineScope -import com.duckduckgo.common.utils.DispatcherProvider import com.duckduckgo.di.scopes.AppScope import com.squareup.anvil.annotations.ContributesBinding -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.launch import javax.inject.Inject interface EventRepository { @@ -40,8 +36,6 @@ interface EventRepository { class RealEventRepository @Inject constructor( private val eventDao: EventDao, private val attributedMetricsDateUtils: AttributedMetricsDateUtils, - @AppCoroutineScope private val appCoroutineScope: CoroutineScope, - private val dispatcherProvider: DispatcherProvider, ) : EventRepository { override suspend fun collectEvent(eventName: String) { val today = attributedMetricsDateUtils.getCurrentDate() @@ -73,8 +67,6 @@ class RealEventRepository @Inject constructor( } override suspend fun deleteAllEvents() { - appCoroutineScope.launch(dispatcherProvider.io()) { - eventDao.deleteAllEvents() - } + eventDao.deleteAllEvents() } } diff --git a/attributed-metrics/attributed-metrics-impl/src/test/java/com/duckduckgo/app/attributed/metrics/store/RealEventRepositoryTest.kt b/attributed-metrics/attributed-metrics-impl/src/test/java/com/duckduckgo/app/attributed/metrics/store/RealEventRepositoryTest.kt index fbf86cbc38e0..7f99a8b70250 100644 --- a/attributed-metrics/attributed-metrics-impl/src/test/java/com/duckduckgo/app/attributed/metrics/store/RealEventRepositoryTest.kt +++ b/attributed-metrics/attributed-metrics-impl/src/test/java/com/duckduckgo/app/attributed/metrics/store/RealEventRepositoryTest.kt @@ -21,8 +21,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import com.duckduckgo.app.attributed.metrics.FakeAttributedMetricsDateUtils import com.duckduckgo.common.test.CoroutineTestRule -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.advanceUntilIdle import kotlinx.coroutines.test.runTest import org.junit.After import org.junit.Before @@ -31,7 +29,6 @@ import org.junit.Test import org.junit.runner.RunWith import java.time.LocalDate -@OptIn(ExperimentalCoroutinesApi::class) @RunWith(AndroidJUnit4::class) class RealEventRepositoryTest { @get:Rule @@ -52,13 +49,10 @@ class RealEventRepositoryTest { ).build() eventDao = db.eventDao() testDateProvider = FakeAttributedMetricsDateUtils(LocalDate.of(2025, 10, 3)) - repository = - RealEventRepository( - eventDao = eventDao, - attributedMetricsDateUtils = testDateProvider, - appCoroutineScope = coroutineTestRule.testScope, - dispatcherProvider = coroutineTestRule.testDispatcherProvider, - ) + repository = RealEventRepository( + eventDao = eventDao, + attributedMetricsDateUtils = testDateProvider, + ) } @After @@ -147,19 +141,15 @@ class RealEventRepositoryTest { } @Test - fun whenDeleteAllEventsThenRemoveAllEvents() = - runTest { - // Setup data - eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-10-03")) - eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-10-02")) - eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-09-03")) - - repository.deleteAllEvents() + fun whenDeleteAllEventsThenRemoveAllEvents() = runTest { + // Setup data + eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-10-03")) + eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-10-02")) + eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-09-03")) - // Wait for the background coroutine to complete - advanceUntilIdle() + repository.deleteAllEvents() - val remainingEvents = eventDao.getEventsByNameAndTimeframe("test_event", "2025-09-03", "2025-10-03") - assert(remainingEvents.isEmpty()) - } + val remainingEvents = eventDao.getEventsByNameAndTimeframe("test_event", "2025-09-03", "2025-10-03") + assert(remainingEvents.isEmpty()) + } }