JUnit output collection is not thread safe


(Geert Premereur) #1

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.


(Szczepan Faber) #2

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

We’ll try to fix it soon.


(Gil Katz) #3

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


(Peter Niederwieser) #4

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


(ToruInoue) #5

in 1.1, this bug still alive.


(ToruInoue) #6

and in 1.2, same.


(Peter Niederwieser) #7

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


(ToruInoue) #8

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


(ToruInoue) #9

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


(Peter Niederwieser) #10

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


(ToruInoue) #11

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


(Gil Katz) #12

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)


(Peter Niederwieser) #13

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


(Jon Austen) #14

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


(Jon Austen) #15

Was a new topic ever created on this?


(Peter Niederwieser) #16

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


(Peter Niederwieser) #17

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.