首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于Ktor通用测试的

基于Ktor通用测试的
EN

Stack Overflow用户
提问于 2022-02-03 16:40:03
回答 1查看 240关注 0票数 0

我有一个多平台库,可以用Ktor执行一些API调用(2.0.0-beta-1)

代码语言:javascript
复制
class DiscoveryServicesImpl(private val client: HttpClient) : DiscoveryServices {
    override suspend fun getServers(domain: String): DAAServers {
        return client.get(Servers.Domain(domain = domain)).body()
    }
}

// Where client is created with specific engine (OkHttp for Android and Darwin for iOS)

此代码按预期工作(在Android和iOS应用程序中都实现)

为了保护这段代码,我添加了一个单元测试来验证url是否构建得很好。

代码语言:javascript
复制
@Test
fun getServersSuccessful() {
    runBlocking {
        var _request: HttpRequestData? = null

        //Given
        val mockEngine = MockEngine {
            _request = it
        }
        val discoveryApiMock = DiscoveryApi(mockEngine)
        val service = DiscoveryServicesImpl(discoveryApiMock.client)

        //When
        val servers: DAAServers = service.getServers(domain)

        //Then
        assertEquals("https://****/$domain", _request!!.url.toString())
    }

}

但是,当运行此测试时,我得到了一个错误(仅用于iOS测试):

代码语言:javascript
复制
> Task :api:discovery:iosTest

***.discovery.DiscoveryServicesImplTest.getServersSuccessful FAILED
    kotlin.native.concurrent.InvalidMutabilityException at /Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt:24



kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.native.internal.Ref@15cb7c8
kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.native.internal.Ref@15cb7c8
    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 kotlin.native.internal.Ref#<set-element>(/Users/teamcity1/teamcity_work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Ref.kt:12)

它只发生在ios目标上

为了解决这个问题,我在任何地方都添加了这个代码(commonMain,commonTest,iosMain,iosTest),但是它没有改变任何东西

代码语言:javascript
复制
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-native-mt") {
    version {
        strictly("1.6.0-native-mt")
    }
}

下面是我的分级文件:

代码语言:javascript
复制
kotlin {
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(Ktor.clientCore)
                implementation(Ktor.clientResources)
                implementation(Ktor.contentNegotiation)
                implementation(Ktor.clientJson)
                implementation(Ktor.clientLogging)
                implementation(Ktor.clientAuth)
                implementation(Koin.koinMultiplatform)
                implementation(project(":serialization"))
                implementation(project(":transverse:log"))

                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-native-mt") {
                    version {
                        strictly("1.6.0-native-mt")
                    }
                }

            }
        }

        val commonTest by getting {
            dependencies {
                implementation(Ktor.clientMock)
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0-native-mt") {
                    version {
                        strictly("1.6.0-native-mt")
                    }
                }
            }
        }


        val iosMain by getting {
            dependencies {
                implementation(Ktor.clientDarwin)
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-03 17:09:38

如果您使用的是最新版本的ktor和coroutines,那么您可能需要使用新的内存模型,并且只使用1.6.0 kotlinx.coroutines,而不是本机mt版本。

https://blog.jetbrains.com/kotlin/2021/08/try-the-new-kotlin-native-memory-manager-development-preview/

https://github.com/touchlab/KaMPKit/blob/main/gradle.properties#L26

Jetbrains的大多数库开发人员可能会从现在开始关注新的内存模型,因此,除非在近期内对移动有明确的关注,否则我会这样做。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70975057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档