Skip to content

Commit

Permalink
Refactored tests and related helpers as per review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Nov 25, 2024
1 parent 7f6d306 commit b52839e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
5 changes: 3 additions & 2 deletions chat-android/src/test/java/com/ably/chat/Sandbox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ internal fun Sandbox.createSandboxRealtime(chatClientId: String): AblyRealtime =
},
)

internal suspend fun Sandbox.getConnectedChatClient(): DefaultChatClient {
val realtime = createSandboxRealtime(apiKey)
internal suspend fun Sandbox.getConnectedChatClient(chatClientId: String = "sandbox-client"): DefaultChatClient {
val realtime = createSandboxRealtime(chatClientId)
realtime.ensureConnected()
return DefaultChatClient(realtime, ClientOptions())
}
Expand All @@ -78,6 +78,7 @@ private suspend fun AblyRealtime.ensureConnected() {
this.connection.on {
if (it.event == ConnectionEvent.connected) {
connectedDeferred.complete(Unit)
this.connection.off()
} else if (it.event != ConnectionEvent.connecting) {
connectedDeferred.completeExceptionally(serverError("ably connection failed"))
this.connection.off()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import io.mockk.justRun
import io.mockk.mockkStatic
import io.mockk.slot
import io.mockk.spyk
import io.mockk.unmockkStatic
import io.mockk.verify
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
Expand All @@ -38,6 +39,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Assert
import org.junit.Test

Expand All @@ -52,6 +54,11 @@ class AttachTest {
Dispatchers.Default.limitedParallelism(1) + CoroutineName("roomId"),
)

@After
fun tearDown() {
unmockkStatic(io.ably.lib.realtime.Channel::attachCoroutine)
}

@Test
fun `(CHA-RL1a) Attach success when room is already in attached state`() = runTest {
val statusLifecycle = spyk(DefaultRoomLifecycle(logger)).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.ably.chat.RoomStatus
import com.ably.chat.RoomStatusChange
import com.ably.chat.ablyException
import com.ably.chat.assertWaiter
import com.ably.chat.attachCoroutine
import com.ably.chat.detachCoroutine
import com.ably.chat.room.atomicCoroutineScope
import com.ably.chat.room.createMockLogger
Expand All @@ -22,6 +23,7 @@ import io.mockk.coVerify
import io.mockk.justRun
import io.mockk.mockkStatic
import io.mockk.spyk
import io.mockk.unmockkStatic
import io.mockk.verify
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
Expand All @@ -33,6 +35,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Assert
import org.junit.Test

Expand All @@ -47,6 +50,11 @@ class DetachTest {
Dispatchers.Default.limitedParallelism(1) + CoroutineName("roomId"),
)

@After
fun tearDown() {
unmockkStatic(io.ably.lib.realtime.Channel::attachCoroutine)
}

@Test
fun `(CHA-RL2a) Detach success when room is already in detached state`() = runTest {
val statusLifecycle = spyk(DefaultRoomLifecycle(logger)).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.ably.chat.RoomLifecycleManager
import com.ably.chat.RoomStatus
import com.ably.chat.RoomStatusChange
import com.ably.chat.assertWaiter
import com.ably.chat.attachCoroutine
import com.ably.chat.detachCoroutine
import com.ably.chat.room.atomicCoroutineScope
import com.ably.chat.room.createMockLogger
Expand All @@ -17,6 +18,7 @@ import io.mockk.every
import io.mockk.justRun
import io.mockk.mockkStatic
import io.mockk.spyk
import io.mockk.unmockkStatic
import io.mockk.verify
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
Expand All @@ -26,6 +28,7 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Assert
import org.junit.Test

Expand All @@ -39,6 +42,11 @@ class ReleaseTest {
Dispatchers.Default.limitedParallelism(1) + CoroutineName("roomId"),
)

@After
fun tearDown() {
unmockkStatic(io.ably.lib.realtime.Channel::attachCoroutine)
}

@Test
fun `(CHA-RL3a) Release success when room is already in released state`() = runTest {
val statusLifecycle = spyk(DefaultRoomLifecycle(logger)).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import io.mockk.every
import io.mockk.justRun
import io.mockk.mockkStatic
import io.mockk.spyk
import io.mockk.unmockkStatic
import io.mockk.verify
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Assert
import org.junit.Test

Expand All @@ -40,6 +42,11 @@ class RetryTest {
Dispatchers.Default.limitedParallelism(1) + CoroutineName("roomId"),
)

@After
fun tearDown() {
unmockkStatic(io.ably.lib.realtime.Channel::attachCoroutine)
}

@Test
fun `(CHA-RL5a) Retry detaches all contributors except the one that's provided (based on underlying channel CHA-RL5a)`() = runTest {
val statusLifecycle = spyk(DefaultRoomLifecycle(logger))
Expand Down Expand Up @@ -179,8 +186,9 @@ class RetryTest {

val contributors = createRoomFeatureMocks()
val messagesContributor = contributors.first { it.featureName == "messages" }
messagesContributor.channel.setState(ChannelState.failed)
messagesContributor.channel.reason = ErrorInfo("Failed channel messages", HttpStatusCode.InternalServerError)

val errorInfo = ErrorInfo("Failed channel messages", HttpStatusCode.InternalServerError)
messagesContributor.channel.setState(ChannelState.failed, errorInfo)

val roomLifecycle = spyk(RoomLifecycleManager(roomScope, statusLifecycle, contributors, logger))

Expand Down

0 comments on commit b52839e

Please sign in to comment.