-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add unit tests for
InMemoryBuffer
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
parsely/src/test/java/com/parsely/parselyandroid/InMemoryBufferTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Map<String, Any?>?>() | ||
|
||
override suspend fun insertEvents(toInsert: List<Map<String, Any?>?>) { | ||
events.addAll(toInsert) | ||
} | ||
|
||
override fun getStoredQueue(): ArrayList<Map<String, Any?>?> { | ||
return ArrayList(events) | ||
} | ||
} | ||
} |