Skip to content

Commit

Permalink
tests: add unit tests for InMemoryBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
wzieba committed Nov 7, 2023
1 parent 8a518ce commit 01503d6
Showing 1 changed file with 77 additions and 0 deletions.
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)
}
}
}

0 comments on commit 01503d6

Please sign in to comment.