-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestServer.kt
57 lines (48 loc) · 1.43 KB
/
TestServer.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package pt.isel.pc.problemsets.utils
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import kotlin.concurrent.thread
class TestServer private constructor(
private val process: Process
) : AutoCloseable {
private val stdOutQueue = LinkedBlockingQueue<String?>()
private val readerThread = thread(isDaemon = true) {
while (true) {
val line = process.inputReader().readLine() ?: break
println("server: $line")
stdOutQueue.put(line)
}
}
fun sendSignal() {
process.destroy()
}
fun join() {
process.waitFor()
readerThread.join()
}
fun waitFor(pred: (String) -> Boolean) {
while (true) {
val line = stdOutQueue.poll(10, TimeUnit.SECONDS)
?: throw TimeoutException("timeout waiting for standard output line")
if (pred(line)) {
return
}
}
}
companion object {
fun start(): TestServer {
return TestServer(
// command may differ from other operating systems
ProcessBuilder("build/install/jvm/bin/jvm.bat")
.redirectErrorStream(true)
.start()
)
}
}
override fun close() {
if (process.isAlive) {
process.destroyForcibly()
}
}
}