Skip to content

Commit

Permalink
Updated assertWaiter testUtil, refactored usages for the same
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Oct 28, 2024
1 parent 0910644 commit 4ef2a1a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
40 changes: 20 additions & 20 deletions chat-android/src/test/java/com/ably/chat/EmitterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AsyncEmitterTest {
emitter.emit(it)
}

assertWaiter { receivedValues.size == 10 }.join()
assertWaiter { receivedValues.size == 10 }
Assert.assertTrue(emitter.finishedProcessing)

Assert.assertEquals((0..9).toList(), receivedValues)
Expand Down Expand Up @@ -68,10 +68,10 @@ class AsyncEmitterTest {

emitter.emit("6")

assertWaiter { receivedValues1.size == 3 }.join()
assertWaiter { receivedValues1.size == 3 }
Assert.assertEquals(listOf("2", "3", "4"), receivedValues1)

assertWaiter { receivedValues2.size == 4 }.join()
assertWaiter { receivedValues2.size == 4 }
Assert.assertEquals(listOf("2", "3", "4", "5"), receivedValues2)

Assert.assertTrue(emitter.finishedProcessing)
Expand Down Expand Up @@ -108,10 +108,10 @@ class AsyncEmitterTest {

Assert.assertFalse(emitter.finishedProcessing) // Processing events

assertWaiter { emittedValues.size == 100 }.join()
assertWaiter { receivedValues1.size == 100 }.join()
assertWaiter { receivedValues2.size == 100 }.join()
assertWaiter { receivedValues3.size == 100 }.join()
assertWaiter { emittedValues.size == 100 }
assertWaiter { receivedValues1.size == 100 }
assertWaiter { receivedValues2.size == 100 }
assertWaiter { receivedValues3.size == 100 }

Assert.assertEquals(emittedValues, receivedValues1)
Assert.assertEquals(emittedValues, receivedValues2)
Expand Down Expand Up @@ -152,10 +152,10 @@ class AsyncEmitterTest {

Assert.assertFalse(emitter.finishedProcessing)

assertWaiter { emitted.size == 100000 }.join()
assertWaiter { receivedValues1.size == 100000 }.join()
assertWaiter { receivedValues2.size == 100000 }.join()
assertWaiter { receivedValues3.size == 100000 }.join()
assertWaiter { emitted.size == 100000 }
assertWaiter { receivedValues1.size == 100000 }
assertWaiter { receivedValues2.size == 100000 }
assertWaiter { receivedValues3.size == 100000 }

// Due to concurrent emits, emit order is not guaranteed
// i.e. assertEquals(emittedValues, receivedValues1) will fail
Expand Down Expand Up @@ -201,10 +201,10 @@ class AsyncEmitterTest {

Assert.assertFalse(emitter.finishedProcessing)

assertWaiter { emitted.size == 100 }.join()
assertWaiter { receivedValues1.size == 100 }.join()
assertWaiter { receivedValues2.size == 100 }.join()
assertWaiter { receivedValues3.size == 100 }.join()
assertWaiter { emitted.size == 100 }
assertWaiter { receivedValues1.size == 100 }
assertWaiter { receivedValues2.size == 100 }
assertWaiter { receivedValues3.size == 100 }

// Due to concurrent emits, emit order is not guaranteed
// i.e. assertEquals(emittedValues, receivedValues1) will fail
Expand Down Expand Up @@ -233,7 +233,7 @@ class AsyncEmitterTest {

emitter.emit(1)

assertWaiter { receivedValues.size == 1 }.join()
assertWaiter { receivedValues.size == 1 }
Assert.assertTrue(emitter.finishedProcessing)

Assert.assertEquals(1, receivedValues[0])
Expand Down Expand Up @@ -283,10 +283,10 @@ class AsyncEmitterTest {
val expectedReceivedValues2 = (0..99).toList().filter { it % 5 != 0 }
val expectedReceivedValues3 = (0..99).toList().filter { it % 7 != 0 }

assertWaiter { emittedValues.size == 100 }.join()
assertWaiter { receivedValues1.size == expectedReceivedValues1.size }.join()
assertWaiter { receivedValues2.size == expectedReceivedValues2.size }.join()
assertWaiter { receivedValues3.size == expectedReceivedValues3.size }.join()
assertWaiter { emittedValues.size == 100 }
assertWaiter { receivedValues1.size == expectedReceivedValues1.size }
assertWaiter { receivedValues2.size == expectedReceivedValues2.size }
assertWaiter { receivedValues3.size == expectedReceivedValues3.size }

Assert.assertEquals(expectedReceivedValues1, receivedValues1)
Assert.assertEquals(expectedReceivedValues2, receivedValues2)
Expand Down
10 changes: 3 additions & 7 deletions chat-android/src/test/java/com/ably/chat/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ 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.withContext
import kotlinx.coroutines.withTimeout

fun buildAsyncHttpPaginatedResponse(items: List<JsonElement>): AsyncHttpPaginatedResponse {
Expand Down Expand Up @@ -56,10 +54,8 @@ 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 {
suspend fun assertWaiter(timeoutInMs: Long = 10000, block: () -> Boolean) {
withContext(Dispatchers.Default) {
withTimeout(timeoutInMs) {
do {
val success = block()
Expand Down

0 comments on commit 4ef2a1a

Please sign in to comment.