2020-08-06 11:07:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2020 dorkbox, llc
|
|
|
|
* Copyright (C) 2014 ZeroTurnaround <support@zeroturnaround.com>
|
|
|
|
* Contains fragments of code from Apache Commons Exec, rights owned
|
|
|
|
* by Apache Software Foundation (ASF).
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-08-07 23:22:31 +02:00
|
|
|
package dorkbox.executor
|
2020-08-06 11:07:25 +02:00
|
|
|
|
2020-08-07 23:22:31 +02:00
|
|
|
import dorkbox.executor.samples.BigOutput
|
|
|
|
import dorkbox.executor.samples.TestSetup
|
2020-08-06 11:07:25 +02:00
|
|
|
import kotlinx.coroutines.runBlocking
|
2022-01-20 00:04:59 +01:00
|
|
|
import org.junit.jupiter.api.Assertions
|
|
|
|
import org.junit.jupiter.api.Test
|
2020-08-06 11:07:25 +02:00
|
|
|
import java.io.ByteArrayOutputStream
|
2021-01-03 20:25:26 +01:00
|
|
|
import java.util.concurrent.*
|
2020-08-06 11:07:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests reading large output that doesn't fit into a buffer between this process and sub process.
|
|
|
|
*
|
|
|
|
* @author Rein Raudjärv
|
|
|
|
*
|
|
|
|
* @see Executor
|
|
|
|
*
|
|
|
|
* @see BigOutput
|
|
|
|
*/
|
2021-01-03 20:25:26 +01:00
|
|
|
class BigOutputTest {
|
2020-08-06 11:07:25 +02:00
|
|
|
companion object {
|
|
|
|
private fun repeat(s: String): String {
|
|
|
|
val sb = StringBuffer(BigOutput.LENGTH)
|
|
|
|
for (i in 0 until BigOutput.LENGTH) sb.append(s)
|
|
|
|
return sb.toString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testDevNull() {
|
|
|
|
runBlocking {
|
2022-01-20 00:04:59 +01:00
|
|
|
bigOutput().start(1, TimeUnit.MINUTES)
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testDevNullAsShell() {
|
|
|
|
runBlocking {
|
|
|
|
// this is faster, because when as a shell, the process is piped to null instead, so there is no output
|
2022-01-20 00:04:59 +01:00
|
|
|
bigOutput().startAsShell(1, TimeUnit.MINUTES)
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testDevNullSeparate() {
|
|
|
|
runBlocking {
|
|
|
|
bigOutput().redirectErrorStream(false)
|
2022-01-20 00:04:59 +01:00
|
|
|
.start(1, TimeUnit.MINUTES)
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testReadOutputAndError() {
|
|
|
|
val output: String = bigOutput()
|
2020-08-10 23:35:09 +02:00
|
|
|
.enableRead()
|
2020-08-06 11:07:25 +02:00
|
|
|
.highPerformanceIO()
|
2022-01-20 00:04:59 +01:00
|
|
|
.startBlocking(1, TimeUnit.MINUTES)
|
2020-08-06 11:07:25 +02:00
|
|
|
.output.utf8()
|
|
|
|
|
2022-01-20 00:04:59 +01:00
|
|
|
Assertions.assertEquals(repeat("+-"), output)
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testReadOutputOnly() {
|
|
|
|
val output: String = bigOutput()
|
2020-08-10 23:35:09 +02:00
|
|
|
.enableRead()
|
2020-08-06 11:07:25 +02:00
|
|
|
.redirectErrorStream(false)
|
2022-01-20 00:04:59 +01:00
|
|
|
.startBlocking(1, TimeUnit.MINUTES)
|
2020-08-06 11:07:25 +02:00
|
|
|
.output.utf8()
|
|
|
|
|
2022-01-20 00:04:59 +01:00
|
|
|
Assertions.assertEquals(repeat("+"), output)
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testRedirectOutputOnly() {
|
|
|
|
val out = ByteArrayOutputStream()
|
|
|
|
|
|
|
|
bigOutput().redirectOutput(out)
|
|
|
|
.redirectErrorStream(false)
|
2022-01-20 00:04:59 +01:00
|
|
|
.startBlocking(1, TimeUnit.MINUTES)
|
2020-08-06 11:07:25 +02:00
|
|
|
|
2022-01-20 00:04:59 +01:00
|
|
|
Assertions.assertEquals(repeat("+"), String(out.toByteArray()))
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testRedirectErrorOnly() {
|
|
|
|
val err = ByteArrayOutputStream()
|
|
|
|
|
|
|
|
bigOutput().redirectError(err)
|
|
|
|
.redirectErrorStream(false)
|
2022-01-20 00:04:59 +01:00
|
|
|
.startBlocking(1, TimeUnit.MINUTES)
|
2020-08-06 11:07:25 +02:00
|
|
|
|
2022-01-20 00:04:59 +01:00
|
|
|
Assertions.assertEquals(repeat("-"), String(err.toByteArray()))
|
2020-08-06 11:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun bigOutput(): Executor {
|
|
|
|
// Use timeout in case we get stuck
|
|
|
|
return Executor("java", TestSetup.getFile(BigOutput::class.java))
|
|
|
|
}
|
|
|
|
}
|