Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
private var lastNetworkSnapshot: StreamNetworkInfo.Snapshot? = null
private var lastConnectionState: StreamConnectionState = StreamConnectionState.Idle

override suspend fun evaluate(

Check failure on line 44 in stream-android-core/src/main/java/io/getstream/android/core/internal/recovery/StreamConnectionRecoveryEvaluatorImpl.kt

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=GetStream_stream-core-android&issues=AaBntxh06bUR5Z-_bdh7&open=AaBntxh06bUR5Z-_bdh7&pullRequest=78
connectionState: StreamConnectionState,
lifecycleState: StreamLifecycleState,
networkState: StreamNetworkState,
Expand All @@ -59,6 +59,12 @@
val previousLifecycle = lastLifecycleState
val previousNetwork = lastNetworkState
val networkAvailable = networkState is StreamNetworkState.Available
// Only a reported loss counts as offline. `Unknown` means no callback has fired yet,
// which is not the same as "no network" and must not tear a connection down — the same
// way an `Unknown` lifecycle state is not treated as backgrounded below.
val networkLost =
networkState is StreamNetworkState.Disconnected ||
networkState is StreamNetworkState.Unavailable
val networkBecameAvailable =
networkAvailable && previousNetwork !is StreamNetworkState.Available
val lifecycleForeground = lifecycleState == StreamLifecycleState.Foreground
Expand All @@ -67,7 +73,7 @@

val shouldDisconnect =
(isConnected || isConnecting) &&
(!networkAvailable || lifecycleState == StreamLifecycleState.Background)
(networkLost || lifecycleState == StreamLifecycleState.Background)

val shouldConnect =
hasConnectedBefore &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,41 @@ class StreamConnectionRecoveryEvaluatorImplTest {
assertNull(recovery)
}

// `Unknown` is the state before any connectivity callback has fired, which happens when the
// platform reports no viable network at registration time. Tearing the in-flight connect down
// there turns a socket failure into a cancellation of the caller's connect().
@Test
fun `does not disconnect while connecting before the first network callback`() = runTest {
val evaluator = evaluator()

val recovery =
evaluator
.evaluate(
connectionState = StreamConnectionState.Connecting.Opening(TEST_USER_ID),
lifecycleState = StreamLifecycleState.Foreground,
networkState = StreamNetworkState.Unknown,
)
.getOrThrow()

assertNull(recovery)
}

@Test
fun `disconnects when the network is reported unavailable`() = runTest {
val evaluator = evaluator()

val recovery =
evaluator
.evaluate(
connectionState = connectedState(),
lifecycleState = StreamLifecycleState.Foreground,
networkState = StreamNetworkState.Unavailable,
)
.getOrThrow()

assertIs<Recovery.Disconnect<*>>(recovery)
}

@Test
fun `stays idle when returning foreground while already reconnecting`() = runTest {
val evaluator = evaluator()
Expand Down
Loading