Skip to content

Commit

Permalink
fix: remove AutoCloseable from Cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
iseki0 committed Mar 22, 2024
1 parent 3fe3f51 commit 3384b82
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
8 changes: 1 addition & 7 deletions src/main/java/space/iseki/cmdpipe/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.concurrent.ForkJoinPool;

@SuppressWarnings("unused")
public interface Cmd extends AutoCloseable {
public interface Cmd {
static <R> @NotNull StreamProcessor<@NotNull OutputStream, R> write(@NotNull Cmd.StreamProcessor.H<@NotNull OutputStream, R> h) {
return new StreamProcessorImpl<>(h);
}
Expand All @@ -20,12 +20,6 @@ public interface Cmd extends AutoCloseable {
return new StreamProcessorImpl<>(h);
}


@Override
default void close() {
stopAll(true);
}

/**
* Get the first process of the command.
*
Expand Down
51 changes: 43 additions & 8 deletions src/test/kotlin/space/iseki/cmdpipe/CmdTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package space.iseki.cmdpipe

import org.junit.jupiter.api.Assumptions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.assertTimeoutPreemptively
import java.io.IOException
import java.nio.charset.Charset
import java.time.Duration
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class CmdTest {
Expand All @@ -17,10 +23,9 @@ class CmdTest {
val stderr = Cmd.read { ctx ->
ctx.stream.bufferedReader(Charset.defaultCharset()).readText().prependIndent("stderr> ")
}
Cmd.Builder().cmdline(cmdError).handleStdout(stdout).handleStderr(stderr).start().use {
println(stdout.future().get())
println(stderr.future().get())
}
Cmd.Builder().cmdline(cmdError).handleStdout(stdout).handleStderr(stderr).start()
println(stdout.future().get())
println(stderr.future().get())
assertTrue { stderr.future().get().contains("xxxx") }
}

Expand All @@ -32,10 +37,40 @@ class CmdTest {
val stderr = Cmd.read { ctx ->
ctx.stream.bufferedReader(Charset.defaultCharset()).readText().prependIndent("stderr> ")
}
Cmd.Builder().cmdline(cmd).handleStdout(stdout).handleStderr(stderr).start().use {
println(stdout.future().get())
println(stderr.future().get())
Cmd.Builder().cmdline(cmd).handleStdout(stdout).handleStderr(stderr).start()
println(stdout.future().get())
println(stderr.future().get())
}

@Test
fun testThrows1() {
val e = assertThrows<IOException> { Cmd.Builder().cmdline("something_does_not_exist").start() }
println(e.message)
assertTrue { e.message!!.contains("error=2") }
}

@Test
fun testRunNodeKill() {
val stdout = Cmd.read {
it.stream.bufferedReader(Charset.defaultCharset()).readText()
}
val node = try {
Cmd.Builder().cmdline("node").handleStdout(stdout).start()
} catch (e: IOException) {
if (e.message!!.contains("error=2")) {
Assumptions.assumeTrue(false, "node not found")
}
throw e
}
val p = node.process
assertTrue { p.isAlive }
node.stopAll(true)
assertFalse { p.isAlive }
assertTimeoutPreemptively(Duration.ofSeconds(1)) {
println(stdout.future().get().prependIndent("> "))
println("node pid: ${node.process.pid()}")
println("node exit: ${node.process.exitValue()}")
}
}

}
}

0 comments on commit 3384b82

Please sign in to comment.