Author: krosenvold Date: Thu Dec 16 09:32:19 2010 New Revision: 1049843 URL: http://svn.apache.org/viewvc?rev=1049843&view=rev Log: [SUREFIRE-665] Intermittent failure of logging test output to file
Changed solution. Added: maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/SynchronizedOutputConsumer.java (with props) Modified: maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/FileOutputConsumerProxy.java Modified: maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java?rev=1049843&r1=1049842&r2=1049843&view=diff ============================================================================== --- maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java (original) +++ maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java Thu Dec 16 09:32:19 2010 @@ -24,6 +24,7 @@ import org.apache.maven.plugin.surefire. import org.apache.maven.plugin.surefire.booterclient.output.StandardOutputConsumer; import org.apache.maven.plugin.surefire.booterclient.output.SupressFooterOutputConsumerProxy; import org.apache.maven.plugin.surefire.booterclient.output.SupressHeaderOutputConsumerProxy; +import org.apache.maven.plugin.surefire.booterclient.output.SynchronizedOutputConsumer; import org.apache.maven.surefire.booter.Classpath; import org.apache.maven.surefire.booter.ProviderConfiguration; import org.apache.maven.surefire.booter.ProviderFactory; @@ -170,8 +171,7 @@ public class ForkStarter { BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration, properties ); - surefireProperties = - booterSerializer.serialize( providerConfiguration, startupConfiguration, testSet ); + surefireProperties = booterSerializer.serialize( providerConfiguration, startupConfiguration, testSet ); if ( forkConfiguration.getSystemProperties() != null ) { @@ -205,11 +205,13 @@ public class ForkStarter final boolean willBeSharingConsumer = startupConfiguration.isRedirectTestOutputToFile(); ForkingStreamConsumer out = - getForkingStreamConsumer( showHeading, showFooter, startupConfiguration.isRedirectTestOutputToFile() ); + getForkingStreamConsumer( showHeading, showFooter, startupConfiguration.isRedirectTestOutputToFile(), + willBeSharingConsumer ); StreamConsumer err = willBeSharingConsumer ? out - : getForkingStreamConsumer( showHeading, showFooter, startupConfiguration.isRedirectTestOutputToFile() ); + : getForkingStreamConsumer( showHeading, showFooter, startupConfiguration.isRedirectTestOutputToFile(), + false ); if ( forkConfiguration.isDebug() ) { @@ -267,7 +269,7 @@ public class ForkStarter } private ForkingStreamConsumer getForkingStreamConsumer( boolean showHeading, boolean showFooter, - boolean redirectTestOutputToFile ) + boolean redirectTestOutputToFile, boolean mustBeThreadSafe ) { OutputConsumer outputConsumer = new StandardOutputConsumer(); @@ -286,6 +288,11 @@ public class ForkStarter outputConsumer = new SupressFooterOutputConsumerProxy( outputConsumer ); } + if ( mustBeThreadSafe ) + { + outputConsumer = new SynchronizedOutputConsumer( outputConsumer ); + } + return new ForkingStreamConsumer( outputConsumer ); } } Modified: maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/FileOutputConsumerProxy.java URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/FileOutputConsumerProxy.java?rev=1049843&r1=1049842&r2=1049843&view=diff ============================================================================== --- maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/FileOutputConsumerProxy.java (original) +++ maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/FileOutputConsumerProxy.java Thu Dec 16 09:32:19 2010 @@ -31,6 +31,9 @@ import java.io.PrintWriter; /** * Surefire output consumer proxy that writes test output to a {...@link File} for each test suite. * + * This class is not threadsafe, but can be encapsulated with a SynchronizedOutputConsumer. It may still be + * accessed from different threads (serially). + * * @author <a href="mailto:car...@apache.org">Carlos Sanchez</a> * @version $Id$ * @since 2.1 @@ -84,7 +87,7 @@ public class FileOutputConsumerProxy super.testSetStarting( reportEntry ); } - public synchronized void testSetCompleted() + public void testSetCompleted() { if ( printWriter == null ) { @@ -104,9 +107,8 @@ public class FileOutputConsumerProxy /** * Write the output to the current test file * <p/> - * This method may be called from multiple threads */ - public synchronized void consumeOutputLine( String line ) + public void consumeOutputLine( String line ) { if ( printWriter == null ) { Added: maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/SynchronizedOutputConsumer.java URL: http://svn.apache.org/viewvc/maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/SynchronizedOutputConsumer.java?rev=1049843&view=auto ============================================================================== --- maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/SynchronizedOutputConsumer.java (added) +++ maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/SynchronizedOutputConsumer.java Thu Dec 16 09:32:19 2010 @@ -0,0 +1,69 @@ +package org.apache.maven.plugin.surefire.booterclient.output; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.surefire.report.ReportEntry; + +/** + * Imposes synchronization on a non-thredsafe OutputConsumer + * + * @author Kristian Rosenvold + */ +public class SynchronizedOutputConsumer + implements OutputConsumer +{ + + final OutputConsumer target; + + public SynchronizedOutputConsumer( OutputConsumer target ) + { + this.target = target; + } + + public synchronized void consumeHeaderLine( String line ) + { + target.consumeHeaderLine( line ); + } + + public synchronized void consumeMessageLine( String line ) + { + target.consumeMessageLine( line ); + } + + public synchronized void consumeFooterLine( String line ) + { + target.consumeFooterLine( line ); + } + + public synchronized void consumeOutputLine( String line ) + { + target.consumeOutputLine( line ); + } + + public synchronized void testSetStarting( ReportEntry reportEntry ) + { + target.testSetStarting( reportEntry ); + } + + public synchronized void testSetCompleted() + { + target.testSetCompleted(); + } +} Propchange: maven/surefire/trunk/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/SynchronizedOutputConsumer.java ------------------------------------------------------------------------------ svn:eol-style = native