Skip to content

Commit 5b6ad56

Browse files
authored
[testing] Simplify our implementation of runTest (#5935)
* remove our own implementation of runTest * make sure to close the connection at the end of the test
1 parent 9f630e5 commit 5b6ad56

File tree

21 files changed

+385
-565
lines changed

21 files changed

+385
-565
lines changed

libraries/apollo-runtime/src/androidInstrumentedTest/kotlin/instrumented/NetworkMonitorTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class NetworkMonitorTest {
3636

3737
@Test
3838
fun test() = mockServerTest(
39-
skipDelays = false,
4039
clientBuilder = {
4140
networkMonitor(NetworkMonitor(InstrumentationRegistry.getInstrumentation().context))
4241
retryOnError { true }

libraries/apollo-runtime/src/commonTest/kotlin/test/network/NetworkMonitorTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import com.apollographql.apollo3.mockserver.assertNoRequest
1313
import com.apollographql.apollo3.mockserver.enqueueString
1414
import com.apollographql.apollo3.network.NetworkMonitor
1515
import com.apollographql.apollo3.testing.FooQuery
16-
import com.apollographql.apollo3.testing.internal.ApolloTestResult
1716
import com.apollographql.apollo3.testing.internal.runTest
1817
import kotlinx.coroutines.CoroutineScope
1918
import kotlinx.coroutines.flow.Flow
@@ -24,6 +23,7 @@ import kotlinx.coroutines.flow.first
2423
import kotlinx.coroutines.flow.flow
2524
import kotlinx.coroutines.flow.mapNotNull
2625
import kotlinx.coroutines.flow.takeWhile
26+
import kotlinx.coroutines.test.TestResult
2727
import okio.use
2828
import kotlin.test.Test
2929
import kotlin.test.assertEquals
@@ -33,7 +33,7 @@ import kotlin.test.assertNull
3333

3434
class NetworkMonitorTest {
3535
@Test
36-
fun failFastIfOfflineTest(): ApolloTestResult {
36+
fun failFastIfOfflineTest(): TestResult {
3737
val fakeNetworkMonitor = FakeNetworkMonitor()
3838

3939
return mockServerTest(clientBuilder = {
@@ -57,7 +57,7 @@ class NetworkMonitorTest {
5757
}
5858

5959
@Test
60-
fun networkMonitorInterceptorTest(): ApolloTestResult {
60+
fun networkMonitorInterceptorTest(): TestResult {
6161
val fakeNetworkMonitor = FakeNetworkMonitor()
6262

6363
return mockServerTest(clientBuilder = {
@@ -116,7 +116,7 @@ class MockServerTest(val mockServer: MockServer, val apolloClient: ApolloClient,
116116
fun mockServerTest(
117117
clientBuilder: ApolloClient.Builder.() -> Unit = {},
118118
block: suspend MockServerTest.() -> Unit
119-
) = runTest(true) {
119+
) = runTest() {
120120
MockServer().use { mockServer ->
121121
ApolloClient.Builder()
122122
.serverUrl(mockServer.url())

libraries/apollo-runtime/src/commonTest/kotlin/test/network/WebSocketNetworkTransportTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class WebSocketNetworkTransportTest {
192192
}
193193

194194
@Test
195-
fun socketClosedEmitsException() = runTest(false) {
195+
fun socketClosedEmitsException() = runTest() {
196196
val mockServer = MockServer()
197197
ApolloClient.Builder()
198198
.serverUrl(mockServer.url())
@@ -390,7 +390,7 @@ class MockServerWebSocketTest(
390390
}
391391
}
392392

393-
fun mockServerWebSocketTest(customizeTransport: WebSocketNetworkTransport.Builder.() -> Unit = {}, block: suspend MockServerWebSocketTest.() -> Unit) = runTest(false) {
393+
fun mockServerWebSocketTest(customizeTransport: WebSocketNetworkTransport.Builder.() -> Unit = {}, block: suspend MockServerWebSocketTest.() -> Unit) = runTest() {
394394
MockServer().use { mockServer ->
395395

396396
ApolloClient.Builder()

libraries/apollo-runtime/src/jvmTest/kotlin/RetryWebSocketsTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import kotlin.time.Duration.Companion.seconds
3434

3535
class RetryWebSocketsTest {
3636
@Test
37-
fun retryIsWorking() = runTest(skipDelays = false) {
37+
fun retryIsWorking() = runTest {
3838
MockServer().use { mockServer ->
3939

4040
ApolloClient.Builder()
@@ -91,7 +91,7 @@ class RetryWebSocketsTest {
9191
}
9292

9393
@Test
94-
fun socketReopensAfterAnError() = runTest(false) {
94+
fun socketReopensAfterAnError() = runTest {
9595
var mockServer = MockServer()
9696

9797
ApolloClient.Builder()
@@ -152,7 +152,7 @@ class RetryWebSocketsTest {
152152
}
153153

154154
@Test
155-
fun retryCanBeDisabled() = runTest(skipDelays = false) {
155+
fun retryCanBeDisabled() = runTest {
156156
val mockServer = MockServer()
157157
ApolloClient.Builder()
158158
.serverUrl(mockServer.url())
@@ -191,7 +191,7 @@ class RetryWebSocketsTest {
191191
}
192192

193193
@Test
194-
fun subscriptionsAreNotRetriedByDefault() = runTest(skipDelays = false) {
194+
fun subscriptionsAreNotRetriedByDefault() = runTest {
195195
val mockServer = MockServer()
196196
ApolloClient.Builder()
197197
.serverUrl(mockServer.url())

libraries/apollo-testing-support/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ kotlin {
1818
implementation(libs.atomicfu.library.get().toString()) {
1919
because("We need locks in TestNetworkTransportHandler (we don't use the gradle plugin rewrite)")
2020
}
21-
implementation(libs.kotlinx.coroutines.test)
21+
api(libs.kotlinx.coroutines.test)
2222
}
2323
}
2424

libraries/apollo-testing-support/src/appleMain/kotlin/com/apollographql/apollo3/testing/internal/runTest.apple.kt

Lines changed: 0 additions & 43 deletions
This file was deleted.

libraries/apollo-testing-support/src/commonMain/kotlin/com/apollographql/apollo3/testing/internal/runTest.kt

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,40 @@ package com.apollographql.apollo3.testing.internal
22

33
import com.apollographql.apollo3.annotations.ApolloInternal
44
import kotlinx.coroutines.CoroutineScope
5-
import kotlin.coroutines.CoroutineContext
6-
import kotlin.coroutines.EmptyCoroutineContext
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.ExperimentalCoroutinesApi
7+
import kotlinx.coroutines.test.TestResult
8+
import kotlinx.coroutines.withContext
79

810
/**
9-
* Utility method that executes the given [block] with optional [before] and [after] blocks.
10-
*
11-
* When [skipDelays] is `true`, the block is executed in [kotlinx.coroutines.test.runTest], otherwise in `runBlocking`.
11+
* Utility method that executes the given [block] with optional [before] and [after] blocks and disables
12+
* the skipDelay behaviour from the coroutines `runTest`. Our tests use delay() in some situations.
1213
*/
1314
@ApolloInternal
14-
expect fun runTest(
15-
skipDelays: Boolean = false,
16-
context: CoroutineContext = EmptyCoroutineContext,
17-
before: suspend CoroutineScope.() -> Unit = {},
18-
after: suspend CoroutineScope.() -> Unit = {},
15+
fun runTest(
1916
block: suspend CoroutineScope.() -> Unit,
20-
): ApolloTestResult
17+
) = runTest(before = {}, after = {}, block)
2118

19+
/**
20+
* We should probably deprecate this overload and remove the before/after state and lateinit
21+
* variables in various tests.There are > 150 instances of it though, so I'm not pushing the
22+
* deprecation button just yet.
23+
*/
24+
@OptIn(ExperimentalCoroutinesApi::class)
2225
@ApolloInternal
23-
expect class ApolloTestResult
26+
fun runTest(
27+
before: suspend CoroutineScope.() -> Unit = {},
28+
after: suspend CoroutineScope.() -> Unit = {},
29+
block: suspend CoroutineScope.() -> Unit,
30+
): TestResult {
31+
return kotlinx.coroutines.test.runTest {
32+
/**
33+
* See https://github.com/Kotlin/kotlinx.coroutines/issues/3179
34+
*/
35+
withContext(Dispatchers.Default.limitedParallelism(1)) {
36+
before()
37+
block()
38+
after()
39+
}
40+
}
41+
}

libraries/apollo-testing-support/src/commonMain/kotlin/com/apollographql/apollo3/testing/runTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ class MockServerTest(val mockServer: MockServer, val apolloClient: ApolloClient,
2121
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
2222
@Suppress("DEPRECATION")
2323
fun mockServerTest(
24-
skipDelays: Boolean = true,
2524
clientBuilder: ApolloClient.Builder.() -> Unit = {},
2625
block: suspend MockServerTest.() -> Unit
27-
) = runTest(skipDelays) {
26+
) = runTest {
2827
MockServer().use { mockServer ->
2928
ApolloClient.Builder()
3029
.serverUrl(mockServer.url())

libraries/apollo-testing-support/src/jsMain/kotlin/com/apollographql/apollo3/testing/internal/runTest.js.kt

Lines changed: 0 additions & 49 deletions
This file was deleted.

libraries/apollo-testing-support/src/jvmMain/kotlin/com/apollographql/apollo3/testing/internal/runTest.jvm.kt

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)