Skip to content

Commit

Permalink
Add timeout example
Browse files Browse the repository at this point in the history
  • Loading branch information
BppleMan committed Dec 28, 2023
1 parent 8391030 commit 7367c60
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions kommand-examples/timeout/build.gradle.kts
@@ -0,0 +1,79 @@
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform

val currentPlatform: Platform = when {
DefaultNativePlatform.getCurrentOperatingSystem().isMacOsX && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.MACOS_X64
DefaultNativePlatform.getCurrentOperatingSystem().isMacOsX && DefaultNativePlatform.getCurrentArchitecture().isArm64 -> Platform.MACOS_ARM64
DefaultNativePlatform.getCurrentOperatingSystem().isLinux && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.LINUX_X64
DefaultNativePlatform.getCurrentOperatingSystem().isLinux && DefaultNativePlatform.getCurrentArchitecture().isArm64 -> Platform.LINUX_ARM64
DefaultNativePlatform.getCurrentOperatingSystem().isWindows && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.MINGW_X64
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}

plugins {
kotlin("multiplatform")
}

repositories {
mavenCentral()
gradlePluginPortal()
}

kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "17"
}
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}

val nativeTarget = when (currentPlatform) {
Platform.MACOS_X64 -> macosX64()
Platform.MACOS_ARM64 -> macosArm64()
Platform.LINUX_X64 -> linuxX64()
Platform.LINUX_ARM64 -> linuxArm64()
Platform.MINGW_X64 -> mingwX64()
}

nativeTarget.apply {
binaries {
executable {
entryPoint = "com.kgit2.kommand.main"
}
}
}

applyDefaultHierarchyTemplate()

sourceSets {
// add opt-in
all {
languageSettings.optIn("kotlinx.cinterop.UnsafeNumber")
languageSettings.optIn("kotlinx.cinterop.ExperimentalForeignApi")
languageSettings.optIn("kotlin.experimental.ExperimentalNativeApi")
languageSettings.optIn("kotlin.native.runtime.NativeRuntimeApi")
languageSettings.optIn("kotlin.ExperimentalStdlibApi")
}

commonMain {
dependencies {
implementation(rootProject)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0-RC2")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0")
}
}
}
}

enum class Platform(
val archName: String
) {
MACOS_X64("x86_64-apple-darwin"),
MACOS_ARM64("aarch64-apple-darwin"),
LINUX_X64("x86_64-unknown-linux-gnu"),
LINUX_ARM64("aarch64-unknown-linux-gnu"),
MINGW_X64("x86_64-pc-windows-gnu"),
;
}
@@ -0,0 +1,60 @@
package com.kgit2.kommand

import com.kgit2.kommand.process.Command
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import kotlinx.datetime.Clock

fun main() = runBlocking(Dispatchers.Default) {
// Sleep with regular
val start = Clock.System.now()
val status = Command("sleep").arg("5").status()
println("status: $status elapsed: ${Clock.System.now() - start}")

// Sleep with timeout detection and timeout determination
val start2 = Clock.System.now()
val child = Command("sleep").arg("5").spawn()
val childJob = async(Dispatchers.IO) {
runCatching {
child.wait()
}.onFailure {
println("child result: $it")
}.getOrNull()
}
runCatching {
withTimeout(3000) {
childJob.await()
}
}.onSuccess {
println("status: $it elapsed: ${Clock.System.now() - start2}")
}.onFailure {
child.kill()
println("status: $it elapsed: ${Clock.System.now() - start2}")
}

// Sleep with timeout detection and determination that it will not timeout
val start3 = Clock.System.now()
val child2 = Command("sleep").arg("2").spawn()
val childJob2 = async(Dispatchers.IO) {
runCatching {
child2.wait()
}.onFailure {
println("child result: $it")
}.getOrNull()
}
runCatching {
withTimeout(3000) {
childJob2.await()
}
}.onSuccess {
println("status: $it elapsed: ${Clock.System.now() - start3}")
}.onFailure {
child2.kill()
println("status: $it elapsed: ${Clock.System.now() - start3}")
}

Unit
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Expand Up @@ -25,3 +25,4 @@ include(":kommand-examples")
include(":kommand-examples:example1")
include(":kommand-examples:example2")
include(":kommand-examples:example3")
include(":kommand-examples:timeout")

0 comments on commit 7367c60

Please sign in to comment.