Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-room-connect-failure-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"client-sdk-android": patch
---

Fixed Room getting stuck in CONNECTING state after failed connect attempts.
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,12 @@ constructor(
}
connectJob.join()

error?.let { throw it }
error?.let {
if (it !is CancellationException) {
handleDisconnect(DisconnectReason.JOIN_FAILURE)
}
throw it
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,90 @@ class RoomTest {

assertEquals(update.sid, sid.sid)
}

@Test
fun connectFailureResetsStateToDisconnected() = runTest {
val connectException = RuntimeException("Connection failed")
rtcEngine.stub {
onBlocking { rtcEngine.join(any(), any(), anyOrNull(), anyOrNull()) }
.doSuspendableAnswer {
throw connectException
}
}
rtcEngine.stub {
onBlocking { rtcEngine.client }
.doReturn(Mockito.mock(SignalClient::class.java))
}

val eventCollector = EventCollector(room.events, coroutineRule.scope)

var caughtException: Throwable? = null
try {
room.connect(
url = TestData.EXAMPLE_URL,
token = "",
)
} catch (e: Throwable) {
caughtException = e
}

val events = eventCollector.stopCollecting()

// Verify exception was thrown (check message since coroutines may wrap exceptions)
assertEquals("Connection failed", caughtException?.message)

// Verify room state is reset to DISCONNECTED
assertEquals(Room.State.DISCONNECTED, room.state)

// Verify Disconnected event was posted with JOIN_FAILURE reason
val disconnectedEvents = events.filterIsInstance<RoomEvent.Disconnected>()
assertEquals(1, disconnectedEvents.size)
assertEquals(DisconnectReason.JOIN_FAILURE, disconnectedEvents[0].reason)
}

@Test
fun connectRetryAfterFailureSucceeds() = runTest {
val connectException = RuntimeException("Connection failed")
var shouldFail = true

rtcEngine.stub {
onBlocking { rtcEngine.join(any(), any(), anyOrNull(), anyOrNull()) }
.doSuspendableAnswer {
if (shouldFail) {
throw connectException
}
room.onJoinResponse(TestData.JOIN.join)
TestData.JOIN.join
}
}
rtcEngine.stub {
onBlocking { rtcEngine.client }
.doReturn(Mockito.mock(SignalClient::class.java))
}

// First connect attempt fails
try {
room.connect(
url = TestData.EXAMPLE_URL,
token = "",
)
} catch (e: Throwable) {
// Expected
}

// Verify room is in DISCONNECTED state after failure
assertEquals(Room.State.DISCONNECTED, room.state)

// Second connect attempt should succeed
shouldFail = false
room.connect(
url = TestData.EXAMPLE_URL,
token = "",
)

// Verify room connected successfully
val roomInfo = TestData.JOIN.join.room
assertEquals(roomInfo.name, room.name)
assertEquals(Room.Sid(roomInfo.sid), room.sid)
}
}