Skip to content

Commit 7367c60

Browse files
committed
Add timeout example
1 parent 8391030 commit 7367c60

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
2+
3+
val currentPlatform: Platform = when {
4+
DefaultNativePlatform.getCurrentOperatingSystem().isMacOsX && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.MACOS_X64
5+
DefaultNativePlatform.getCurrentOperatingSystem().isMacOsX && DefaultNativePlatform.getCurrentArchitecture().isArm64 -> Platform.MACOS_ARM64
6+
DefaultNativePlatform.getCurrentOperatingSystem().isLinux && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.LINUX_X64
7+
DefaultNativePlatform.getCurrentOperatingSystem().isLinux && DefaultNativePlatform.getCurrentArchitecture().isArm64 -> Platform.LINUX_ARM64
8+
DefaultNativePlatform.getCurrentOperatingSystem().isWindows && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.MINGW_X64
9+
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
10+
}
11+
12+
plugins {
13+
kotlin("multiplatform")
14+
}
15+
16+
repositories {
17+
mavenCentral()
18+
gradlePluginPortal()
19+
}
20+
21+
kotlin {
22+
jvm {
23+
compilations.all {
24+
kotlinOptions.jvmTarget = "17"
25+
}
26+
withJava()
27+
testRuns["test"].executionTask.configure {
28+
useJUnitPlatform()
29+
}
30+
}
31+
32+
val nativeTarget = when (currentPlatform) {
33+
Platform.MACOS_X64 -> macosX64()
34+
Platform.MACOS_ARM64 -> macosArm64()
35+
Platform.LINUX_X64 -> linuxX64()
36+
Platform.LINUX_ARM64 -> linuxArm64()
37+
Platform.MINGW_X64 -> mingwX64()
38+
}
39+
40+
nativeTarget.apply {
41+
binaries {
42+
executable {
43+
entryPoint = "com.kgit2.kommand.main"
44+
}
45+
}
46+
}
47+
48+
applyDefaultHierarchyTemplate()
49+
50+
sourceSets {
51+
// add opt-in
52+
all {
53+
languageSettings.optIn("kotlinx.cinterop.UnsafeNumber")
54+
languageSettings.optIn("kotlinx.cinterop.ExperimentalForeignApi")
55+
languageSettings.optIn("kotlin.experimental.ExperimentalNativeApi")
56+
languageSettings.optIn("kotlin.native.runtime.NativeRuntimeApi")
57+
languageSettings.optIn("kotlin.ExperimentalStdlibApi")
58+
}
59+
60+
commonMain {
61+
dependencies {
62+
implementation(rootProject)
63+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0-RC2")
64+
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0")
65+
}
66+
}
67+
}
68+
}
69+
70+
enum class Platform(
71+
val archName: String
72+
) {
73+
MACOS_X64("x86_64-apple-darwin"),
74+
MACOS_ARM64("aarch64-apple-darwin"),
75+
LINUX_X64("x86_64-unknown-linux-gnu"),
76+
LINUX_ARM64("aarch64-unknown-linux-gnu"),
77+
MINGW_X64("x86_64-pc-windows-gnu"),
78+
;
79+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.kgit2.kommand
2+
3+
import com.kgit2.kommand.process.Command
4+
import kotlinx.coroutines.Dispatchers
5+
import kotlinx.coroutines.IO
6+
import kotlinx.coroutines.async
7+
import kotlinx.coroutines.runBlocking
8+
import kotlinx.coroutines.withTimeout
9+
import kotlinx.datetime.Clock
10+
11+
fun main() = runBlocking(Dispatchers.Default) {
12+
// Sleep with regular
13+
val start = Clock.System.now()
14+
val status = Command("sleep").arg("5").status()
15+
println("status: $status elapsed: ${Clock.System.now() - start}")
16+
17+
// Sleep with timeout detection and timeout determination
18+
val start2 = Clock.System.now()
19+
val child = Command("sleep").arg("5").spawn()
20+
val childJob = async(Dispatchers.IO) {
21+
runCatching {
22+
child.wait()
23+
}.onFailure {
24+
println("child result: $it")
25+
}.getOrNull()
26+
}
27+
runCatching {
28+
withTimeout(3000) {
29+
childJob.await()
30+
}
31+
}.onSuccess {
32+
println("status: $it elapsed: ${Clock.System.now() - start2}")
33+
}.onFailure {
34+
child.kill()
35+
println("status: $it elapsed: ${Clock.System.now() - start2}")
36+
}
37+
38+
// Sleep with timeout detection and determination that it will not timeout
39+
val start3 = Clock.System.now()
40+
val child2 = Command("sleep").arg("2").spawn()
41+
val childJob2 = async(Dispatchers.IO) {
42+
runCatching {
43+
child2.wait()
44+
}.onFailure {
45+
println("child result: $it")
46+
}.getOrNull()
47+
}
48+
runCatching {
49+
withTimeout(3000) {
50+
childJob2.await()
51+
}
52+
}.onSuccess {
53+
println("status: $it elapsed: ${Clock.System.now() - start3}")
54+
}.onFailure {
55+
child2.kill()
56+
println("status: $it elapsed: ${Clock.System.now() - start3}")
57+
}
58+
59+
Unit
60+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ include(":kommand-examples")
2525
include(":kommand-examples:example1")
2626
include(":kommand-examples:example2")
2727
include(":kommand-examples:example3")
28+
include(":kommand-examples:timeout")

0 commit comments

Comments
 (0)