diff --git a/chat-android/src/test/java/com/ably/chat/Sandbox.kt b/chat-android/src/test/java/com/ably/chat/Sandbox.kt index 1f4e153f..fd74e3bd 100644 --- a/chat-android/src/test/java/com/ably/chat/Sandbox.kt +++ b/chat-android/src/test/java/com/ably/chat/Sandbox.kt @@ -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()) } @@ -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() diff --git a/chat-android/src/test/java/com/ably/chat/room/lifecycle/AttachTest.kt b/chat-android/src/test/java/com/ably/chat/room/lifecycle/AttachTest.kt index 5736ab88..9faf157f 100644 --- a/chat-android/src/test/java/com/ably/chat/room/lifecycle/AttachTest.kt +++ b/chat-android/src/test/java/com/ably/chat/room/lifecycle/AttachTest.kt @@ -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 @@ -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 @@ -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 { diff --git a/chat-android/src/test/java/com/ably/chat/room/lifecycle/DetachTest.kt b/chat-android/src/test/java/com/ably/chat/room/lifecycle/DetachTest.kt index 6fc78aac..256bbf5c 100644 --- a/chat-android/src/test/java/com/ably/chat/room/lifecycle/DetachTest.kt +++ b/chat-android/src/test/java/com/ably/chat/room/lifecycle/DetachTest.kt @@ -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 @@ -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 @@ -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 @@ -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 { diff --git a/chat-android/src/test/java/com/ably/chat/room/lifecycle/ReleaseTest.kt b/chat-android/src/test/java/com/ably/chat/room/lifecycle/ReleaseTest.kt index 0a05f3d6..5f70b4f8 100644 --- a/chat-android/src/test/java/com/ably/chat/room/lifecycle/ReleaseTest.kt +++ b/chat-android/src/test/java/com/ably/chat/room/lifecycle/ReleaseTest.kt @@ -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 @@ -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 @@ -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 @@ -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 { diff --git a/chat-android/src/test/java/com/ably/chat/room/lifecycle/RetryTest.kt b/chat-android/src/test/java/com/ably/chat/room/lifecycle/RetryTest.kt index 44b4c426..edfcafc4 100644 --- a/chat-android/src/test/java/com/ably/chat/room/lifecycle/RetryTest.kt +++ b/chat-android/src/test/java/com/ably/chat/room/lifecycle/RetryTest.kt @@ -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 @@ -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)) @@ -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))