diff --git a/parsely/src/test/java/com/parsely/parselyandroid/InMemoryBufferTest.kt b/parsely/src/test/java/com/parsely/parselyandroid/InMemoryBufferTest.kt new file mode 100644 index 00000000..72fb7e52 --- /dev/null +++ b/parsely/src/test/java/com/parsely/parselyandroid/InMemoryBufferTest.kt @@ -0,0 +1,77 @@ +package com.parsely.parselyandroid + +import androidx.test.core.app.ApplicationProvider +import kotlin.time.Duration.Companion.seconds +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.cancel +import kotlinx.coroutines.test.advanceTimeBy +import kotlinx.coroutines.test.runCurrent +import kotlinx.coroutines.test.runTest +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@OptIn(ExperimentalCoroutinesApi::class) +@RunWith(RobolectricTestRunner::class) +internal class InMemoryBufferTest { + + private lateinit var sut: InMemoryBuffer + private val repository = FakeLocalStorageRepository() + + @Test + fun `when adding a new event, then save it to local storage`() = runTest { + // given + val event = mapOf("test" to 123) + sut = InMemoryBuffer(backgroundScope, repository) + + // when + sut.add(event) + advanceTimeBy(1.seconds) + runCurrent() + backgroundScope.cancel() + + // then + assertThat(repository.getStoredQueue()).containsOnlyOnce(event) + } + + @Test + fun `when adding multiple events in different intervals, then save all of them to local storage without duplicates`() = + runTest { + // given + val events = (0..2).map { mapOf("test" to it) } + sut = InMemoryBuffer(backgroundScope, repository) + + // when + sut.add(events[0]) + advanceTimeBy(1.seconds) + runCurrent() + + sut.add(events[1]) + advanceTimeBy(0.5.seconds) + runCurrent() + + sut.add(events[2]) + advanceTimeBy(0.5.seconds) + runCurrent() + + backgroundScope.cancel() + + // then + assertThat(repository.getStoredQueue()).containsOnlyOnceElementsOf(events) + } + + class FakeLocalStorageRepository() : + LocalStorageRepository(ApplicationProvider.getApplicationContext()) { + + private val events = mutableListOf?>() + + override suspend fun insertEvents(toInsert: List?>) { + events.addAll(toInsert) + } + + override fun getStoredQueue(): ArrayList?> { + return ArrayList(events) + } + } +}