Skip to content

Commit c112d92

Browse files
committed
Migrate testtool to ktor 3.0.0
1 parent c5278e9 commit c112d92

File tree

7 files changed

+53
-118
lines changed

7 files changed

+53
-118
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ kotlinx-kover = "0.8.3"
99
kotlinx-io = "0.5.4"
1010
kotlinx-serialization = "1.7.3"
1111
kotlinx-coroutines = "1.9.0"
12-
ktor = "2.3.12"
12+
ktor = "3.0.0"
1313

1414
# android
1515
android = "8.5.0"
@@ -21,7 +21,7 @@ maven-publish = "0.29.0"
2121

2222
# other
2323
bouncycastle = "1.78.1"
24-
logback = "1.2.11"
24+
logback = "1.5.9"
2525

2626
[libraries]
2727
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }

testtool/client/build.gradle.kts

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import org.jetbrains.kotlin.gradle.*
66
import org.jetbrains.kotlin.gradle.plugin.*
7-
import org.jetbrains.kotlin.gradle.plugin.mpp.*
8-
import org.jetbrains.kotlin.konan.target.*
97

108
plugins {
119
alias(libs.plugins.kotlin.multiplatform)
@@ -21,7 +19,6 @@ kotlin {
2119
browser()
2220
}
2321
wasmJs {
24-
// No support from KTOR
2522
nodejs()
2623
browser()
2724
}
@@ -33,6 +30,7 @@ kotlin {
3330
watchosX64()
3431
watchosArm32()
3532
watchosArm64()
33+
watchosDeviceArm64()
3634
watchosSimulatorArm64()
3735

3836
tvosX64()
@@ -46,74 +44,36 @@ kotlin {
4644
linuxArm64()
4745
mingwX64()
4846

49-
// No support from KTOR
50-
watchosDeviceArm64()
5147
androidNativeX64()
5248
androidNativeX86()
5349
androidNativeArm64()
5450
androidNativeArm32()
5551

56-
val supportsKtor: (KotlinCompilation<*>).() -> Boolean = {
57-
val konanTarget = (target as? KotlinNativeTarget)?.konanTarget
58-
when {
59-
konanTarget == KonanTarget.WATCHOS_DEVICE_ARM64 -> false
60-
konanTarget?.family == Family.ANDROID -> false
61-
platformType == KotlinPlatformType.wasm -> false
62-
else -> true
63-
}
64-
}
65-
6652
@OptIn(ExperimentalKotlinGradlePluginApi::class)
6753
applyDefaultHierarchyTemplate {
6854
common {
6955
group("nonJvm") {
7056
withCompilations { it.platformType != KotlinPlatformType.jvm }
7157
}
72-
group("ktor") {
73-
withCompilations(supportsKtor)
74-
}
75-
group("nonKtor") {
76-
withCompilations { !supportsKtor(it) }
77-
}
78-
group("cio") {
79-
withCompilations {
80-
val target = (it.target as? KotlinNativeTarget)?.konanTarget
81-
when {
82-
target == KonanTarget.WATCHOS_DEVICE_ARM64 -> false
83-
target?.family?.isAppleFamily == true -> true
84-
target?.family == Family.LINUX -> true
85-
else -> false
86-
}
87-
}
88-
}
8958
}
9059
}
9160

9261
sourceSets {
93-
val commonMain by getting {
94-
dependencies {
95-
api(libs.kotlinx.coroutines.core)
96-
}
62+
commonMain.dependencies {
63+
api(libs.kotlinx.coroutines.core)
64+
implementation(libs.ktor.client.core)
9765
}
98-
val ktorMain by getting {
99-
dependencies {
100-
implementation(libs.ktor.client.core)
101-
}
66+
jvmMain.dependencies {
67+
implementation(libs.ktor.client.okhttp)
10268
}
103-
val jvmMain by getting {
104-
dependencies {
105-
implementation(libs.ktor.client.okhttp)
106-
}
69+
linuxMain.dependencies {
70+
implementation(libs.ktor.client.cio)
10771
}
108-
val cioMain by getting {
109-
dependencies {
110-
implementation(libs.ktor.client.cio)
111-
}
72+
appleMain.dependencies {
73+
implementation(libs.ktor.client.cio)
11274
}
113-
val mingwMain by getting {
114-
dependencies {
115-
implementation(libs.ktor.client.winhttp)
116-
}
75+
mingwMain.dependencies {
76+
implementation(libs.ktor.client.winhttp)
11777
}
11878
}
11979
}

testtool/client/src/commonMain/kotlin/TesttoolClient.kt

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44

55
package dev.whyoleg.cryptography.testtool.client
66

7+
import io.ktor.client.*
8+
import io.ktor.client.plugins.*
9+
import io.ktor.client.request.*
10+
import io.ktor.client.statement.*
11+
import io.ktor.http.content.*
12+
import io.ktor.utils.io.*
13+
import io.ktor.utils.io.core.*
714
import kotlinx.coroutines.flow.*
15+
import kotlinx.io.*
816

917
object TesttoolClient {
1018

@@ -26,6 +34,32 @@ object TesttoolClient {
2634

2735
internal expect fun hostOverride(): String?
2836

29-
internal expect suspend fun postData(path: String, bytes: ByteArray): String
37+
private val client = HttpClient {
38+
expectSuccess = true
39+
install(DefaultRequest) {
40+
host = hostOverride() ?: ""
41+
port = 9000
42+
}
43+
install(HttpRequestRetry)
44+
}
45+
46+
internal suspend fun postData(path: String, bytes: ByteArray): String = client.post(path) {
47+
setBody(ByteArrayContent(bytes))
48+
}.bodyAsText()
49+
50+
internal fun getData(path: String): Flow<Pair<String, ByteArray>> = flow {
51+
val channel = client.get(path).bodyAsChannel()
52+
while (true) {
53+
val idLength = channel.readIntOrNull() ?: break
54+
val id = channel.readPacket(idLength).readString()
55+
val contentLength = channel.readInt()
56+
val content = channel.readPacket(contentLength).readByteArray()
57+
emit(id to content)
58+
}
59+
}
3060

31-
internal expect fun getData(path: String): Flow<Pair<String, ByteArray>>
61+
private suspend fun ByteReadChannel.readIntOrNull(): Int? {
62+
val packet = readRemaining(4)
63+
if (packet.remaining == 0L) return null
64+
return packet.readInt()
65+
}

testtool/client/src/ktorMain/kotlin/TesttoolClient.ktor.kt

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

testtool/client/src/nonKtorMain/kotlin/TesttoolClient.nonKtor.kt

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

testtool/server/src/main/kotlin/TesttoolServer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package dev.whyoleg.cryptography.testtool.server
77
import io.ktor.server.application.*
88
import io.ktor.server.engine.*
99
import io.ktor.server.netty.*
10-
import io.ktor.server.plugins.callloging.*
10+
import io.ktor.server.plugins.calllogging.*
1111
import io.ktor.server.plugins.cors.routing.*
1212
import io.ktor.server.routing.*
1313
import java.io.*

testtool/server/src/main/kotlin/compatibility.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import io.ktor.server.application.*
88
import io.ktor.server.response.*
99
import io.ktor.server.routing.*
1010
import io.ktor.utils.io.*
11-
import io.ktor.utils.io.core.*
11+
import kotlinx.io.*
1212
import java.nio.file.*
1313
import java.util.concurrent.atomic.*
1414
import kotlin.io.path.*
@@ -57,7 +57,7 @@ internal fun Route.routes(
5757
}
5858

5959
private suspend fun ApplicationCall.saveFile(path: Path, name: String) {
60-
val bytes = request.receiveChannel().readRemaining().readBytes()
60+
val bytes = request.receiveChannel().readRemaining().readByteArray()
6161

6262
path.createDirectories().resolve(name).writeBytes(bytes, StandardOpenOption.CREATE_NEW)
6363
}

0 commit comments

Comments
 (0)