Skip to content

Commit

Permalink
refactor: introduce QueueManager interface
Browse files Browse the repository at this point in the history
Makes creating fake objects less problematic by removing a need to call real object constructor.
  • Loading branch information
wzieba committed Nov 17, 2023
1 parent 77303c6 commit be74b28
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.io.ObjectOutputStream
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock

interface QueueRepository {
internal interface QueueRepository {
suspend fun remove(toRemove: List<Map<String, Any?>?>)
suspend fun getStoredQueue(): ArrayList<Map<String, Any?>?>
suspend fun insertEvents(toInsert: List<Map<String, Any?>?>)
Expand Down
10 changes: 5 additions & 5 deletions parsely/src/main/java/com/parsely/parselyandroid/SendEvents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.coroutines.sync.withLock

internal class SendEvents(
private val flushManager: FlushManager,
private val localStorageRepository: QueueRepository,
private val repository: QueueRepository,
private val parselyAPIConnection: ParselyAPIConnection,
private val scope: CoroutineScope
) {
Expand All @@ -18,7 +18,7 @@ internal class SendEvents(
operator fun invoke(isDebug: Boolean) {
scope.launch {
mutex.withLock {
val eventsToSend = localStorageRepository.getStoredQueue()
val eventsToSend = repository.getStoredQueue()

if (eventsToSend.isEmpty()) {
flushManager.stop()
Expand All @@ -32,16 +32,16 @@ internal class SendEvents(

if (isDebug) {
ParselyTracker.PLog("Debug mode on. Not sending to Parse.ly")
localStorageRepository.remove(eventsToSend)
repository.remove(eventsToSend)
} else {
ParselyTracker.PLog("Requested %s", ParselyTracker.ROOT_URL)
parselyAPIConnection.send(jsonPayload)
.fold(
onSuccess = {
ParselyTracker.PLog("Pixel request success")
localStorageRepository.remove(eventsToSend)
repository.remove(eventsToSend)
ParselyTracker.PLog("Event queue empty, flush timer cleared.")
if (localStorageRepository.getStoredQueue().isEmpty()) {
if (repository.getStoredQueue().isEmpty()) {
flushManager.stop()
}
},
Expand Down
22 changes: 10 additions & 12 deletions parsely/src/test/java/com/parsely/parselyandroid/SendEventsTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.parsely.parselyandroid

import androidx.test.core.app.ApplicationProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
Expand All @@ -22,7 +20,7 @@ class SendEventsTest {
// given
sut = SendEvents(
FakeFlushManager(),
FakeLocalStorageRepository(),
FakeRepository(),
FakeParselyAPIConnection(),
this
)
Expand All @@ -32,14 +30,14 @@ class SendEventsTest {
runCurrent()

// then
assertThat(FakeLocalStorageRepository().getStoredQueue()).isEmpty()
assertThat(FakeRepository().getStoredQueue()).isEmpty()
}

@Test
fun `given non-empty local storage and debug mode off, when sending events, then events are sent and removed from local storage`() =
runTest {
// given
val repository = FakeLocalStorageRepository().apply {
val repository = FakeRepository().apply {
insertEvents(listOf(mapOf("test" to 123)))
}
val parselyAPIConnection = FakeParselyAPIConnection().apply {
Expand All @@ -64,7 +62,7 @@ class SendEventsTest {
fun `given non-empty local storage and debug mode on, when sending events, then events are not sent and removed from local storage`() =
runTest {
// given
val repository = FakeLocalStorageRepository().apply {
val repository = FakeRepository().apply {
insertEvents(listOf(mapOf("test" to 123)))
}
sut = SendEvents(
Expand All @@ -86,7 +84,7 @@ class SendEventsTest {
fun `given non-empty local storage and debug mode off, when sending events fails, then events are not removed from local storage`() =
runTest {
// given
val repository = FakeLocalStorageRepository().apply {
val repository = FakeRepository().apply {
insertEvents(listOf(mapOf("test" to 123)))
}
val parselyAPIConnection = FakeParselyAPIConnection().apply {
Expand All @@ -112,7 +110,7 @@ class SendEventsTest {
runTest {
// given
val flushManager = FakeFlushManager()
val repository = FakeLocalStorageRepository().apply {
val repository = FakeRepository().apply {
insertEvents(listOf(mapOf("test" to 123)))
}
val parselyAPIConnection = FakeParselyAPIConnection().apply {
Expand All @@ -138,7 +136,7 @@ class SendEventsTest {
runTest {
// given
val flushManager = FakeFlushManager()
val repository = FakeLocalStorageRepository().apply {
val repository = FakeRepository().apply {
insertEvents(listOf(mapOf("test" to 123)))
}
val parselyAPIConnection = FakeParselyAPIConnection().apply {
Expand All @@ -164,7 +162,7 @@ class SendEventsTest {
runTest {
// given
val flushManager = FakeFlushManager()
val repository = object : FakeLocalStorageRepository() {
val repository = object : FakeRepository() {
override suspend fun getStoredQueue(): ArrayList<Map<String, Any?>?> {
return ArrayList(listOf(mapOf("test" to 123)))
}
Expand Down Expand Up @@ -193,7 +191,7 @@ class SendEventsTest {
val flushManager = FakeFlushManager()
sut = SendEvents(
flushManager,
FakeLocalStorageRepository(),
FakeRepository(),
FakeParselyAPIConnection(),
this
)
Expand Down Expand Up @@ -222,7 +220,7 @@ class SendEventsTest {
get() = TODO("Not implemented")
}

private class FakeLocalStorageRepository : QueueRepository {
private open class FakeRepository : QueueRepository {
private var storage = emptyList<Map<String, Any?>?>()

override suspend fun insertEvents(toInsert: List<Map<String, Any?>?>) {
Expand Down

0 comments on commit be74b28

Please sign in to comment.