Matti MK
02/18/2022, 9:49 PMKtor
HttpClient
.
Tests work just fine on Android, but on iOS getting:
kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.test.TestScopeImpl@338a688
I pretty much copy+pasted the ktor test sample. Source in comments. Any tips would be welcome 👍Matti MK
02/18/2022, 9:50 PMclass ApiClient(engine: HttpClientEngine) {
private val httpClient = HttpClient(engine)
suspend fun getIp(): String = httpClient.get("<https://api.ipify.org/?format=json>")
}
class ApiClientTest {
@Test
fun sampleClientTest() = runTest {
val mockEngine = MockEngine { request ->
respond(
content = ByteReadChannel("""{"ip":"127.0.0.1"}"""),
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/json")
)
}
val apiClient2 = ApiClient(mockEngine)
assertTrue(apiClient2.getIp().contains("127.0.0.1"))
}
}
Source: https://github.com/ktorio/ktor-documentation/blob/main/codeSnippets/snippets/client-testing-mock/src/test/kotlin/ApplicationTest.kt
Stacktrace:
kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.test.TestScopeImpl@b4a041f8
kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.test.TestScopeImpl@b4a041f8
at kotlin.Throwable#<init>(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt:24)
at kotlin.Exception#<init>(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:23)
at kotlin.RuntimeException#<init>(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:34)
at kotlin.native.concurrent.InvalidMutabilityException#<init>(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Freezing.kt:24)
at <global>.ThrowInvalidMutabilityException(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:109)
at <global>.MutationCheck(Unknown Source)
at kotlinx.coroutines.test.TestScopeImpl.<set-finished>#internal(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/common/src/TestScope.kt:169)
at kotlinx.coroutines.test.TestScopeImpl#leave(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/common/src/TestScope.kt:195)
at kotlinx.coroutines.test.runTest$<anonymous>_3$<anonymous>_4#internal(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/common/src/TestBuilders.kt:167)
at kotlinx.coroutines.test.$runTest$<anonymous>_3$<anonymous>_4$FUNCTION_REFERENCE$2.invoke#internal(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/common/src/TestBuilders.kt:167)
at kotlinx.coroutines.test.$runTestCoroutineCOROUTINE$0#invokeSuspend(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/common/src/TestBuilders.kt:233)
at kotlinx.coroutines.test#runTestCoroutine(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/common/src/TestBuilders.kt:198)
at kotlinx.coroutines.test.$runTest$<anonymous>_3COROUTINE$1.invokeSuspend#internal(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/common/src/TestBuilders.kt:167)
at kotlinx.coroutines.test.$runTest$<anonymous>_3COROUTINE$1.invoke#internal(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/common/src/TestBuilders.kt:166)
at kotlinx.coroutines.test.$createTestResult$lambda-0COROUTINE$6.invokeSuspend#internal(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/native/src/TestBuilders.kt:13)
at kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/coroutines/ContinuationImpl.kt:30)
at kotlinx.coroutines.DispatchedTask#run(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Debug.kt:17)
at kotlinx.coroutines.EventLoopImplBase#processNextEvent(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/EventLoop.common.kt:281)
at kotlinx.coroutines#runEventLoop(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:87)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking#internal(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:74)
at kotlinx.coroutines#runBlocking(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/libraries/stdlib/src/kotlin/contracts/ContractBuilder.kt:56)
at kotlinx.coroutines#runBlocking$default(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:35)
at kotlinx.coroutines.test#createTestResult(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/native/src/TestBuilders.kt:12)
at kotlinx.coroutines.test#runTest__at__kotlinx.coroutines.test.TestScope(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/common/src/TestBuilders.kt:166)
at kotlinx.coroutines.test#runTest(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/common/src/TestBuilders.kt:154)
at kotlinx.coroutines.test#runTest$default(/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-test/common/src/TestBuilders.kt:147)
Matti MK
02/18/2022, 9:52 PM1.6.3
• coroutines-test: 1.6.0
• kotlin version: 1.6.10
Matti MK
02/18/2022, 9:59 PMJohn O'Reilly
02/19/2022, 9:33 AMMatti MK
02/19/2022, 9:57 PMJohn O'Reilly
02/19/2022, 10:00 PMnative-mt
coroutines will be at least deprecated in Kotlin 1.7Matti MK
02/19/2022, 10:01 PMJohn O'Reilly
02/19/2022, 10:02 PMJohn O'Reilly
02/19/2022, 10:03 PMMatti MK
02/19/2022, 10:07 PMMatti MK
02/19/2022, 10:07 PMMatti MK
02/19/2022, 10:11 PM