Skip to content

Commit e437ca0

Browse files
authored
feat: reconnect socket when closed normally (WPB-21876) (#3856)
1 parent b2fb6e9 commit e437ca0

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventRepository.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,14 @@ internal class EventDataSource(
353353
)
354354
}
355355

356+
@Suppress("ThrowsCount")
356357
private suspend fun handleWebSocketClosure(webSocketEvent: WebSocketEvent.Close<ConsumableNotificationResponse>) {
357358
when (val cause = webSocketEvent.cause) {
358-
null -> logger.i("Websocket closed normally")
359+
null -> {
360+
logger.i("Websocket closed normally, will retry to keep connection alive")
361+
throw KaliumSyncException("Websocket closed normally", CoreFailure.Unknown(null))
362+
}
363+
359364
is IOException ->
360365
throw KaliumSyncException("Websocket disconnected", NetworkFailure.NoNetworkConnection(cause))
361366

logic/src/commonTest/kotlin/com/wire/kalium/logic/data/event/EventRepositoryTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,32 @@ class EventRepositoryTest {
585585
}
586586
}
587587

588+
@Test
589+
fun givenWebSocketClosedWithNullCause_whenHandlingClosure_thenShouldThrowKaliumSyncException() = runTest {
590+
val (_, repository) = Arrangement()
591+
.withClientHasConsumableNotifications(true)
592+
.withCurrentClientIdReturning(TestClient.CLIENT_ID)
593+
.withConsumeLiveEventsReturning(
594+
NetworkResponse.Success(
595+
value = flowOf(WebSocketEvent.Close(cause = null)),
596+
headers = emptyMap(),
597+
httpCode = 200
598+
)
599+
)
600+
.arrange()
601+
602+
val eitherFlow = repository.liveEvents()
603+
assertTrue(eitherFlow is Either.Right)
604+
605+
val flow = eitherFlow.value
606+
607+
val thrown = assertFailsWith<KaliumSyncException> {
608+
flow.collect {}
609+
}
610+
611+
assertIs<CoreFailure.Unknown>(thrown.coreFailureCause)
612+
}
613+
588614
private companion object {
589615
const val LAST_SAVED_EVENT_ID_KEY = "last_processed_event_id"
590616
val MEMBER_JOIN_EVENT = EventContentDTO.Conversation.MemberJoinDTO(

0 commit comments

Comments
 (0)