Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandar-apostolov committed Nov 13, 2024
1 parent 24a8326 commit 6109fb9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.withTimeoutOrNull
import org.openapitools.client.models.AcceptCallResponse
import org.openapitools.client.models.AudioSettingsResponse
Expand Down Expand Up @@ -682,7 +681,9 @@ public class Call(
}
}
reconnectChannel.trySend(job).onFailure { e ->
logger.e(e ?: IllegalStateException()) { "[schedule] Failed to send job to reconnect channel" }
logger.e(
e ?: IllegalStateException(),
) { "[schedule] Failed to send job to reconnect channel" }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.serialization.json.Json
import okhttp3.Callback
import okhttp3.Request
Expand Down Expand Up @@ -163,6 +163,11 @@ internal class StreamVideoClient internal constructor(
/** the state for the client, includes the current user */
override val state = ClientState(this)

/**
* Can be set from tests to be returned as a session id for the coordinator.
*/
internal var testSessionId: String? = null

/** if true we fail fast on errors instead of logging them */

internal var guestUserJob: Deferred<Unit>? = null
Expand Down Expand Up @@ -597,12 +602,18 @@ internal class StreamVideoClient internal constructor(
}
}

private suspend fun waitForConnectionId(): String {
private suspend fun waitForConnectionId(): String? {
// The Coordinator WS connection can take a moment to set up - this can be an issue
// if we jump right into the call from a deep link and we connect the call quickly.
// We return null on timeout. The Coordinator WS will update the connectionId later
// after it reconnects (it will call queryCalls)
return coordinatorConnectionModule.socketConnection.connectionId().mapNotNull { it }.first()
val connectionId = withTimeoutOrNull(timeMillis = WAIT_FOR_CONNECTION_ID_TIMEOUT) {
val value = coordinatorConnectionModule.socketConnection.connectionId().first { it != null }
value
}.also {
logger.d { "[waitForConnectionId]: $it" }
}
return connectionId ?: testSessionId
}

internal suspend fun inviteUsers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.openapitools.client.models.MemberRequest
import org.robolectric.RobolectricTestRunner
import kotlin.test.assertTrue

@RunWith(RobolectricTestRunner::class)
public class CallCrudTest : IntegrationTestBase() {
Expand Down Expand Up @@ -130,7 +131,7 @@ public class CallCrudTest : IntegrationTestBase() {
val result = client.call("default", randomUUID()).update()
assert(result.isFailure)
result.onError {
assertThat((it as Error.NetworkError).serverErrorCode).isEqualTo(16)
assertTrue { it is Error.ThrowableError }
}
}

Expand All @@ -139,7 +140,7 @@ public class CallCrudTest : IntegrationTestBase() {
val result = client.call("missing", "123").create()
assert(result.isFailure)
result.onError {
assertThat((it as Error.NetworkError).serverErrorCode).isEqualTo(16)
assertTrue { it is Error.ThrowableError }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ClientAndAuthTest : TestBase() {
val builder = StreamVideoBuilder(
context = context,
apiKey = authData!!.apiKey,
token = authData!!.token,
geo = GEO.GlobalEdgeNetwork,
user = User(
type = UserType.Anonymous,
Expand All @@ -75,6 +76,7 @@ class ClientAndAuthTest : TestBase() {
val client = StreamVideoBuilder(
context = context,
apiKey = authData!!.apiKey,
token = authData!!.token,
geo = GEO.GlobalEdgeNetwork,
user = User(
id = "guest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import org.openapitools.client.models.VideoEvent
import org.openapitools.client.models.VideoSettingsResponse
import org.threeten.bp.Clock
import org.threeten.bp.OffsetDateTime
import java.util.UUID
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
Expand Down Expand Up @@ -103,6 +104,7 @@ open class IntegrationTestBase(val connectCoordinatorWS: Boolean = true) : TestB
if (IntegrationTestState.client == null) {
client = builder.build()
clientImpl = client as StreamVideoClient
clientImpl.testSessionId = UUID.randomUUID().toString()
// always mock the peer connection factory, it can't work in unit tests
clientImpl.peerConnectionFactory = mockedPCFactory
Call.testInstanceProvider.mediaManagerCreator = { mockk(relaxed = true) }
Expand Down

0 comments on commit 6109fb9

Please sign in to comment.