JUnit output collection is not thread safe

In some of our unit tests we have more than one thread outputting to the console. Once in a while this leads to crashes in output collection as can be seen in this stack trace from a test case:

Exception in thread "pool-3-thread-2" org.gradle.listener.ListenerNotificationException: Failed to notify test result processor.
 at org.gradle.messaging.dispatch.BroadcastDispatch.dispatch(BroadcastDispatch.java:97)
 at org.gradle.messaging.dispatch.BroadcastDispatch.dispatch(BroadcastDispatch.java:32)
 at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:75)
 at $Proxy4.output(Unknown Source)
 at org.gradle.api.internal.tasks.testing.processors.CaptureTestOutputTestResultProcessor$StdOutForwarder.onOutput(CaptureTestOutputTestResultProcessor.java:90)
 at org.gradle.logging.internal.DefaultStandardOutputRedirector$WriteAction.execute(DefaultStandardOutputRedirector.java:83)
 at org.gradle.logging.internal.DefaultStandardOutputRedirector$WriteAction.execute(DefaultStandardOutputRedirector.java:79)
 at org.gradle.util.LineBufferingOutputStream$StringOutput.write(LineBufferingOutputStream.java:147)
 at org.gradle.util.LineBufferingOutputStream.flush(LineBufferingOutputStream.java:120)
 at org.gradle.util.LineBufferingOutputStream.write(LineBufferingOutputStream.java:92)
 at java.io.OutputStream.write(OutputStream.java:99)
 at java.io.PrintStream.write(PrintStream.java:430)
 at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
 at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
 at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:85)
 at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:168)
 at java.io.PrintStream.newLine(PrintStream.java:496)
 at java.io.PrintStream.println(PrintStream.java:757)
 at org.gradle.util.LinePerThreadBufferingOutputStream.println(LinePerThreadBufferingOutputStream.java:205)
 at com.sony.zenobe.core.sample.SimulationSample$SimpleBattery.adaptChargeLevel(SimulationSample.java:82)
 at com.sony.zenobe.core.sample.SimulationSample$SimpleBattery.tick(SimulationSample.java:86)
 at com.sony.zenobe.core.sample.SimulationSample$SimpleBattery$1.run(SimulationSample.java:71)
 at com.sony.zenobe.core.actor.SynchronizingScheduler$SynchronizedRunnable.run(SynchronizingScheduler.java:52)
 at com.sony.zenobe.core.scheduler.ASAPScheduler$ScheduledRunnable.run(ASAPScheduler.java:228)
 at com.sony.zenobe.core.scheduler.ASAPScheduler$FixedRateScheduledRunnable.run(ASAPScheduler.java:249)
 at com.sony.zenobe.core.scheduler.ASAPScheduler$1.run(ASAPScheduler.java:87)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ArrayIndexOutOfBoundsException
 at java.lang.String.getChars(String.java:854)
 at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:391)
 at java.lang.StringBuilder.append(StringBuilder.java:119)
 at org.gradle.api.internal.tasks.testing.junit.JUnitXmlReportGenerator.output(JUnitXmlReportGenerator.java:60)
 at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
 at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
 at org.gradle.messaging.dispatch.BroadcastDispatch.dispatch(BroadcastDispatch.java:88)
 ... 28 more

StringBuilder is not thread-safe. That may be the cause of our symptoms.

It’s this bug: http://issues.gradle.org/browse/GRADLE-1513

We’ll try to fix it soon.

We also have this defect in milestone 8, when this defect will be resolved?

First thing to do would be to upgrade to 1.1 and check if the problem still exists.

in 1.1, this bug still alive.

and in 1.2, same.

Do you have a reliable way of reproducing this? If yes, can you share it?

Maybe I can share it, but not so soon. Please wait a week.

Sorry for late, I made & uploaded the project that can produce the problem. http://bit.ly/Q3vkwm

Thanks, that helped a lot. The fix is already in and will ship with 1.3.

In 1.3, that problem does not occurred. case closed! thanks a lot.

Sorry to disappoint you but it reproduce in gradle 1.3 org.gradle.listener.ListenerNotificationException: Failed to notify test result processor. at org.gradle.listener.BroadcastDispatch.dispatch(BroadcastDispatch.java:97)

Please create a new topic and provide all available information (stack trace, ways to reproduce, etc.).

That link no longer works. I wanted to see what kind of code does this.

Was a new topic ever created on this?

You just need to create a few threads and let them write to ‘System.out’. Our tests indicate that this problem is fixed.

I can’t recall seeing another topic on this. “Failed to notify test result processor” could mean many things. We’d have to see the full stack trace.