[SUREFIRE] refactoring removed locks in TestProvidingInputStream
Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/81e667be Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/81e667be Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/81e667be Branch: refs/heads/master Commit: 81e667be8892c4e49e9552c48c1f263bd92160c6 Parents: 5ea1905 Author: Tibor17 <[email protected]> Authored: Mon Jul 13 23:21:55 2015 +0200 Committer: Tibor17 <[email protected]> Committed: Thu Jul 23 23:28:08 2015 +0200 ---------------------------------------------------------------------- .../TestProvidingInputStream.java | 118 ++++++++++--------- 1 file changed, 62 insertions(+), 56 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/81e667be/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/TestProvidingInputStream.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/TestProvidingInputStream.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/TestProvidingInputStream.java index 0001817..e08edf7 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/TestProvidingInputStream.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/TestProvidingInputStream.java @@ -23,8 +23,8 @@ import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.util.Queue; -import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.ReentrantLock; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicBoolean; import static org.apache.maven.surefire.util.internal.StringUtils.encodeStringForForkCommunication; @@ -43,20 +43,18 @@ public class TestProvidingInputStream extends InputStream implements NotifiableTestStream { - private final ReentrantLock lock = new ReentrantLock(); - - private final Condition lockCondition = lock.newCondition(); + private final Semaphore semaphore = new Semaphore( 0 ); private final Queue<String> testItemQueue; + private final AtomicBoolean closed = new AtomicBoolean(); + private byte[] currentBuffer; private int currentPos; private volatile FlushReceiverProvider flushReceiverProvider; - private volatile boolean closed; - /** * C'tor * @@ -75,60 +73,78 @@ public class TestProvidingInputStream this.flushReceiverProvider = flushReceiverProvider; } + /** + * Used by single thread in StreamFeeder. + * + * @return {@inheritDoc} + * @throws IOException {@inheritDoc} + */ @SuppressWarnings( "checkstyle:magicnumber" ) @Override public int read() throws IOException { - lock.lock(); - try + if ( closed.get() ) { - if ( closed ) - { - throw new EOFException( "closed unexpectedly" ); - } - else + throw new EOFException( "closed unexpectedly" ); + } + else + { + byte[] buffer = currentBuffer; + + if ( buffer == null ) { - if ( null == currentBuffer ) + if ( flushReceiverProvider != null ) { - if ( null != flushReceiverProvider && null != flushReceiverProvider.getFlushReceiver() ) + FlushReceiver flushing = flushReceiverProvider.getFlushReceiver(); + if ( flushing != null ) { - flushReceiverProvider.getFlushReceiver().flush(); + flushing.flush(); } + } - lockCondition.awaitUninterruptibly(); - - if ( closed ) - { - throw new EOFException( "closed unexpectedly" ); - } + awaitNextTest(); - String currentElement = testItemQueue.poll(); - if ( currentElement != null ) - { - currentBuffer = encodeStringForForkCommunication( currentElement ); - currentPos = 0; - } - else - { - return -1; - } + if ( closed.get() ) + { + throw new EOFException( "closed unexpectedly" ); } - if ( currentPos < currentBuffer.length ) + String currentElement = testItemQueue.poll(); + if ( currentElement != null ) { - return currentBuffer[currentPos++] & 0xff; + currentBuffer = encodeStringForForkCommunication( currentElement ); + buffer = currentBuffer; + currentPos = 0; } else { - currentBuffer = null; - return '\n' & 0xff; + return -1; } } + + if ( currentPos < buffer.length ) + { + return buffer[currentPos++] & 0xff; + } + else + { + currentBuffer = null; + return '\n' & 0xff; + } + } + } + + private void awaitNextTest() + throws IOException + { + try + { + semaphore.acquire(); } - finally + catch ( InterruptedException e ) { - lock.unlock(); + throw new IOException( e ); } } @@ -137,30 +153,20 @@ public class TestProvidingInputStream */ public void provideNewTest() { - lock.lock(); - try - { - lockCondition.signalAll(); - } - finally - { - lock.unlock(); - } + semaphore.release(); } @Override public void close() { - closed = true; - lock.lock(); - try + if ( closed.compareAndSet( false, true ) ) { currentBuffer = null; - lockCondition.signalAll(); - } - finally - { - lock.unlock(); + int permits = semaphore.drainPermits(); + if ( permits == 0 ) + { + semaphore.release(); + } } } } \ No newline at end of file
