Skip to content

Commit 6abd1a1

Browse files
authored
Merge pull request #618 from phiSgr/remove-stray-output
Prevent stray output from reaching stdout
2 parents f18d9a8 + cdf01fe commit 6abd1a1

File tree

6 files changed

+20
-25
lines changed

6 files changed

+20
-25
lines changed

executors/src/main/kotlin/JUnitExecutors.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ class JUnitExecutors {
2222
jUnitCore.run(request)
2323
}
2424
System.setOut(standardOutput)
25+
2526
standardOutput.print(mapper.writeValueAsString(output.groupBy({ it.className }, { it })))
27+
2628
}
2729
catch (e: Exception) {
28-
System.setOut(standardOutput)
29-
print("[\"")
30+
standardOutput.print("[\"")
3031
e.printStackTrace()
31-
print("\"]")
32+
standardOutput.print("\"]")
3233
}
3334
}
3435

executors/src/main/kotlin/JavaRunnerExecutor.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,13 @@ class JavaRunnerExecutor {
4141
}
4242
System.out.flush()
4343
System.err.flush()
44-
System.setOut(defaultOutputStream)
45-
outputObj.text = outputStream.toString()
44+
outputObj.text = synchronized(outputStream) { outputStream.toString() }
4645
.replace("</errStream><errStream>".toRegex(), "")
4746
.replace("</outStream><outStream>".toRegex(), "")
48-
print(mapper.writeValueAsString(outputObj))
47+
defaultOutputStream.print(mapper.writeValueAsString(outputObj))
4948
}
5049
catch (e: Throwable) {
51-
System.setOut(defaultOutputStream)
52-
System.out.println(mapper.writeValueAsString(RunOutput(exception = e)))
50+
defaultOutputStream.println(mapper.writeValueAsString(RunOutput(exception = e)))
5351
}
5452
}
5553
}

executors/src/main/kotlin/OutputStreams.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import kotlin.jvm.Throws
88
internal class ErrorStream(private val outputStream: OutputStream) : OutputStream() {
99

1010
@Throws(IOException::class)
11-
override fun write(b: Int) {
11+
override fun write(b: Int) = synchronized(outputStream) {
1212
outputStream.write("<errStream>".toByteArray())
1313
outputStream.write(b)
1414
outputStream.write("</errStream>".toByteArray())
1515
}
1616

1717
@Throws(IOException::class)
18-
override fun write(b: ByteArray) {
18+
override fun write(b: ByteArray) = synchronized(outputStream) {
1919
outputStream.write("<errStream>".toByteArray())
2020
outputStream.write(b)
2121
outputStream.write("</errStream>".toByteArray())
2222
}
2323

2424
@Throws(IOException::class)
25-
override fun write(b: ByteArray, offset: Int, length: Int) {
25+
override fun write(b: ByteArray, offset: Int, length: Int) = synchronized(outputStream) {
2626
outputStream.write("<errStream>".toByteArray())
2727
outputStream.write(b, offset, length)
2828
outputStream.write("</errStream>".toByteArray())
@@ -32,21 +32,21 @@ internal class ErrorStream(private val outputStream: OutputStream) : OutputStrea
3232
internal class OutStream(private val outputStream: OutputStream) : OutputStream() {
3333

3434
@Throws(IOException::class)
35-
override fun write(b: Int) {
35+
override fun write(b: Int) = synchronized(outputStream) {
3636
outputStream.write("<outStream>".toByteArray())
3737
outputStream.write(b)
3838
outputStream.write("</outStream>".toByteArray())
3939
}
4040

4141
@Throws(IOException::class)
42-
override fun write(b: ByteArray) {
42+
override fun write(b: ByteArray) = synchronized(outputStream) {
4343
outputStream.write("<outStream>".toByteArray())
4444
outputStream.write(b)
4545
outputStream.write("</outStream>".toByteArray())
4646
}
4747

4848
@Throws(IOException::class)
49-
override fun write(b: ByteArray, offset: Int, length: Int) {
49+
override fun write(b: ByteArray, offset: Int, length: Int) = synchronized(outputStream) {
5050
outputStream.write("<outStream>".toByteArray())
5151
outputStream.write(b, offset, length)
5252
outputStream.write("</outStream>".toByteArray())

executors/src/main/kotlin/TestListener.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ internal class TestListener : RunListener() {
5454
System.err.flush()
5555
JUnitExecutors.output[JUnitExecutors.output.size - 1].apply {
5656
executionTime = System.currentTimeMillis() - startTime
57-
output = testOutputStream?.toString().orEmpty()
57+
output = testOutputStream?.let {
58+
synchronized(it) { it.toString() }
59+
}.orEmpty()
5860
.replace("</errStream><errStream>".toRegex(), "")
5961
.replace("</outStream><outStream>".toRegex(), "")
6062
}

src/main/kotlin/com/compiler/server/model/ProgramOutput.kt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@ data class ProgramOutput(
2626
standardOutput.isBlank() -> ExecutionResult()
2727
else -> {
2828
try {
29-
// coroutines can produce incorrect output. see example in `base coroutines test 7`
30-
if (standardOutput.startsWith("{")) outputMapper.readValue(standardOutput, ExecutionResult::class.java)
31-
else {
32-
val result = outputMapper.readValue("{" + standardOutput.substringAfter("{"), ExecutionResult::class.java)
33-
result.apply {
34-
text = standardOutput.substringBefore("{") + text
35-
}
36-
}
29+
outputMapper.readValue(standardOutput, ExecutionResult::class.java)
3730
} catch (e: Exception) {
3831
ExecutionResult(exception = e.toExceptionDescriptor())
3932
}

src/test/kotlin/com/compiler/server/CoroutinesRunnerTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ class CoroutinesRunnerTest : BaseExecutorTest() {
5757

5858
@Test
5959
fun `base coroutines test 7`() {
60-
run(
60+
val result = run(
6161
code = "import kotlinx.coroutines.*\n\nfun main() = runBlocking {\nGlobalScope.launch {\n repeat(1000) { i ->\n println(\"I'm sleeping \$i ...\")\n delay(500L)\n }\n}\ndelay(1300L) // just quit after delay \n}",
62-
contains = "I'm sleeping 0 ...\nI'm sleeping 1 ...\nI'm sleeping 2 ...\n"
62+
contains = ""
6363
)
64+
Assertions.assertEquals("<outStream>I'm sleeping 0 ...\nI'm sleeping 1 ...\nI'm sleeping 2 ...\n</outStream>", result.text)
6465
}
6566

6667
@Test

0 commit comments

Comments
 (0)