Skip to content

Commit

Permalink
Added simple flowemitter test with relevant comments for Emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Oct 25, 2024
1 parent 3b31ec3 commit e00ead4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
9 changes: 9 additions & 0 deletions chat-android/src/main/java/com/ably/chat/FlowEmitter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.launch

/**
* Interface implementation should work in both java and kotlin
*/
interface Emitter<V> {
fun emit(value: V)
fun on(block: suspend CoroutineScope.(V) -> Unit): Subscription
fun offAll()
}

/**
* Interface implementation should work in both java and kotlin
*/
interface EventEmitter<K, V> {
fun emit(event: K, value: V)
fun on(event: K, block: suspend CoroutineScope.(V) -> Unit): Subscription
Expand All @@ -39,6 +45,9 @@ open class FlowEmitter<V>(scope: CoroutineScope = CoroutineScope(Dispatchers.Def
}
}
return Subscription {
/**
* Seems only sensible way to cancel collector, since job.cancel cancels ongoing job
*/
keepCollecting.set(false)
}
}
Expand Down
34 changes: 34 additions & 0 deletions chat-android/src/test/java/com/ably/chat/FlowEmitterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ably.chat

import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Test

class FlowEmitterTest {
@Test
fun `should be able to emit and listen to event`() = runTest {
val flowEmitter = FlowEmitter<String>()
val receivedValues = mutableListOf<String>()

flowEmitter.emit("1")

val subscription = flowEmitter.on { received: String ->
receivedValues.add(received)
}

flowEmitter.emit("2")
flowEmitter.emit("3")
flowEmitter.emit("4")

subscription.unsubscribe()

flowEmitter.emit("5")
flowEmitter.emit("7")

assertWaiter { receivedValues.size == 3 }.join()

// Assertion fails because receivedValues returns empty values
// Seems collector works independently of emitter and can be late processing values
Assert.assertEquals(listOf("2", "3", "4"), receivedValues)
}
}
19 changes: 19 additions & 0 deletions chat-android/src/test/java/com/ably/chat/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import com.google.gson.JsonElement
import io.ably.lib.types.AsyncHttpPaginatedResponse
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout

fun buildAsyncHttpPaginatedResponse(items: List<JsonElement>): AsyncHttpPaginatedResponse {
val response = mockk<AsyncHttpPaginatedResponse>()
Expand Down Expand Up @@ -49,3 +55,16 @@ fun mockOccupancyApiResponse(realtimeClientMock: RealtimeClient, response: JsonE
)
}
}

suspend fun assertWaiter(timeoutInMs: Long = 10000, block : () -> Boolean): Job {
// Need to create coroutineScope because delay doesn't work in runTest default CoroutineScope
val scope = CoroutineScope(Dispatchers.Default)
return scope.launch {
withTimeout(timeoutInMs) {
do {
val success = block()
delay(100)
} while (!success)
}
}
}

0 comments on commit e00ead4

Please sign in to comment.