Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate worker console output #561

Merged
merged 1 commit into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/main/kotlin/io/bazel/worker/IO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,28 @@ import java.io.ByteArrayOutputStream
import java.io.Closeable
import java.io.InputStream
import java.io.PrintStream
import java.nio.charset.StandardCharsets

class IO(
val input: InputStream,
val output: PrintStream,
val captured: ByteArrayOutputStream,
private val captured: ByteArrayOutputStream,
private val restore: () -> Unit = {}
) : Closeable {

/**
* Reads the captured std out and err as a UTF-8 string and then resets the
* captured ByteArrayOutputStream.
*
* Resetting the ByteArrayOutputStream prevents the worker from returning
* the same console output multiple times
**/
fun readCapturedAsUtf8String(): String {
val out = captured.toByteArray().toString(StandardCharsets.UTF_8)
captured.reset()
return out
}

companion object {
fun capture(): IO {
val stdErr = System.err
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/bazel/worker/PersistentWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class PersistentWorker(
val response = WorkerProtocol.WorkResponse.newBuilder().apply {
output = listOf(
result.log.out.toString(),
io.captured.toByteArray().toString(UTF_8)
io.readCapturedAsUtf8String()
).filter { it.isNotBlank() }.joinToString("\n")
exitCode = result.status.exit
requestId = request.requestId
Expand Down
14 changes: 13 additions & 1 deletion src/test/kotlin/io/bazel/worker/IOTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,19 @@ class IOTest {
assertThat(captured.written()).isEmpty()
IO.capture().use { io ->
println("foo foo is on the loose")
assertThat(io.captured.written()).isEqualTo("foo foo is on the loose\n")
assertThat(io.readCapturedAsUtf8String()).isEqualTo("foo foo is on the loose\n")
}
assertThat(captured.written()).isEmpty()
}

@Test
fun captureDoesNotRepeatOutput() {
assertThat(captured.written()).isEmpty()
IO.capture().use { io ->
println("foo foo is on the loose")
assertThat(io.readCapturedAsUtf8String()).isEqualTo("foo foo is on the loose\n")
println("bar bar is on the loose")
assertThat(io.readCapturedAsUtf8String()).isEqualTo("bar bar is on the loose\n")
}
assertThat(captured.written()).isEmpty()
}
Expand Down