Repository: maven-surefire Updated Branches: refs/heads/master 84fd7235b -> c14b25fe5
[SUREFIRE-1037] Elapsed time is reported incorrectly for tests run in parallel Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/c14b25fe Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/c14b25fe Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/c14b25fe Branch: refs/heads/master Commit: c14b25fe501193ce5779ff7d5da2590164ca7b8e Parents: 84fd723 Author: Tibor17 <tibo...@lycos.com> Authored: Sun May 10 23:09:40 2015 +0200 Committer: Tibor17 <tibo...@lycos.com> Committed: Sun May 10 23:09:40 2015 +0200 ---------------------------------------------------------------------- .../surefire/its/Junit47concurrencyIT.java | 26 ++++++- ...refire747MethodParallelWithSuiteCountIT.java | 19 +++++ .../test/resources/concurrentjunit47/pom.xml | 3 +- .../src/test/java/junit47/BasicTest.java | 21 ++++-- .../maven/surefire/junitcore/TestMethod.java | 33 ++++++--- .../maven/surefire/junitcore/TestSet.java | 78 +++++++------------- 6 files changed, 106 insertions(+), 74 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/c14b25fe/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Junit47concurrencyIT.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Junit47concurrencyIT.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Junit47concurrencyIT.java index ba3ecf5..0821763 100644 --- a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Junit47concurrencyIT.java +++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Junit47concurrencyIT.java @@ -19,9 +19,14 @@ package org.apache.maven.surefire.its; * under the License. */ +import org.apache.maven.surefire.its.fixture.OutputValidator; import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase; import org.junit.Test; +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; + /** * Basic suite test using all known versions of JUnit 4.x * @@ -34,9 +39,22 @@ public class Junit47concurrencyIT public void test47() throws Exception { - // todo: Align with others - unpack( "concurrentjunit47" ).sysProp( "junitVersion", "4.7" ).setJUnitVersion( - "4.7" ).executeTest().verifyErrorFree( 1 ); - + OutputValidator validator = unpack( "concurrentjunit47" ) + .sysProp( "junitVersion", "4.7" ) + .setJUnitVersion( "4.7" ) + .executeTest() + .verifyErrorFree( 4 ); + String result = null; + for ( String line : validator.loadLogLines() ) + { + if ( line.startsWith( "Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed:" ) ) + { + result = line; + break; + } + } + assertNotNull( result); + assertThat( result, anyOf( containsString( "Time elapsed: 1." ), containsString( "Time elapsed: 0.9" ) ) ); + assertThat( result, endsWith( " sec - in concurrentjunit47.src.test.java.junit47.BasicTest" ) ); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/c14b25fe/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire747MethodParallelWithSuiteCountIT.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire747MethodParallelWithSuiteCountIT.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire747MethodParallelWithSuiteCountIT.java index bedf3a8..0e480a9 100644 --- a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire747MethodParallelWithSuiteCountIT.java +++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire747MethodParallelWithSuiteCountIT.java @@ -29,6 +29,9 @@ import java.util.Iterator; import java.util.Set; import java.util.TreeSet; +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.endsWith; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -80,6 +83,22 @@ public class Surefire747MethodParallelWithSuiteCountIT long min = 750, max = 1250; assertTrue( String.format( "duration %d should be between %d and %d millis", duration, min, max ), duration > min && duration < max ); + + for ( String line : validator.loadLogLines() ) + { + if ( line.startsWith( "Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed:" ) ) + { + assertThat( line, anyOf( // 0.5 sec, the delta -1.0/+0.3 can be varying depending on CI jobs + containsString( "Time elapsed: 0.4" ), + containsString( "Time elapsed: 0.5" ), + containsString( "Time elapsed: 0.6" ), + containsString( "Time elapsed: 0.7" ), + containsString( "Time elapsed: 0.8" ) ) ); + assertThat( line, anyOf( + endsWith(" sec - in surefire747.SuiteTest1" ), + endsWith(" sec - in surefire747.SuiteTest2" ) ) ); + } + } } @Test http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/c14b25fe/surefire-integration-tests/src/test/resources/concurrentjunit47/pom.xml ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/concurrentjunit47/pom.xml b/surefire-integration-tests/src/test/resources/concurrentjunit47/pom.xml index ab4a7fb..ecb8860 100644 --- a/surefire-integration-tests/src/test/resources/concurrentjunit47/pom.xml +++ b/surefire-integration-tests/src/test/resources/concurrentjunit47/pom.xml @@ -57,7 +57,8 @@ <artifactId>maven-surefire-plugin</artifactId> <configuration> <parallel>methods</parallel> - <threadCount>2</threadCount> + <perCoreThreadCount>false</perCoreThreadCount> + <threadCount>3</threadCount> </configuration> </plugin> </plugins> http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/c14b25fe/surefire-integration-tests/src/test/resources/concurrentjunit47/src/test/java/junit47/BasicTest.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/resources/concurrentjunit47/src/test/java/junit47/BasicTest.java b/surefire-integration-tests/src/test/resources/concurrentjunit47/src/test/java/junit47/BasicTest.java index b85ec42..1f11060 100644 --- a/surefire-integration-tests/src/test/resources/concurrentjunit47/src/test/java/junit47/BasicTest.java +++ b/surefire-integration-tests/src/test/resources/concurrentjunit47/src/test/java/junit47/BasicTest.java @@ -21,19 +21,16 @@ package concurrentjunit47.src.test.java.junit47; import org.junit.*; +import java.util.concurrent.TimeUnit; public class BasicTest { - private boolean setUpCalled = false; - private static boolean tearDownCalled = false; - @Before public void setUp() { setUpCalled = true; - tearDownCalled = false; System.out.println( "Called setUp" ); } @@ -41,7 +38,6 @@ public class BasicTest public void tearDown() { setUpCalled = false; - tearDownCalled = true; System.out.println( "Called tearDown" ); } @@ -51,6 +47,21 @@ public class BasicTest Assert.assertTrue( "setUp was not called", setUpCalled ); } + @Test + public void a() throws Exception { + TimeUnit.SECONDS.sleep( 1 ); + } + + @Test + public void b() throws Exception { + TimeUnit.SECONDS.sleep( 1 ); + } + + @Test + public void c() throws Exception { + TimeUnit.SECONDS.sleep( 1 ); + } + @AfterClass public static void oneTimeTearDown() { http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/c14b25fe/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestMethod.java ---------------------------------------------------------------------- diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestMethod.java b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestMethod.java index dc1ae27..5b9e8ac 100644 --- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestMethod.java +++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestMethod.java @@ -40,7 +40,7 @@ class TestMethod private final long startTime; - private long endTime; + private volatile long endTime; private volatile ReportEntry testFailure; @@ -52,31 +52,31 @@ class TestMethod private volatile LogicalStream output; - public TestMethod( ReportEntry description, TestSet testSet ) + TestMethod( ReportEntry description, TestSet testSet ) { this.description = description; this.testSet = testSet; startTime = System.currentTimeMillis(); } - public void testFinished() + void testFinished() { setEndTime(); } - public void testIgnored( ReportEntry description ) + void testIgnored( ReportEntry description ) { ignored = description; setEndTime(); } - public void testFailure( ReportEntry failure ) + void testFailure( ReportEntry failure ) { this.testFailure = failure; setEndTime(); } - public void testError( ReportEntry failure ) + void testError( ReportEntry failure ) { this.testError = failure; setEndTime(); @@ -87,13 +87,22 @@ class TestMethod this.endTime = System.currentTimeMillis(); } - public int getElapsed() + int getElapsed() { return endTime > 0 ? (int) ( endTime - startTime ) : 0; } + long getStartTime() + { + return startTime; + } + + long getEndTime() + { + return endTime; + } - public void replay( RunListener reporter ) + void replay( RunListener reporter ) { if ( ignored != null ) @@ -129,25 +138,25 @@ class TestMethod reportEntry.getStackTraceWriter(), getElapsed(), reportEntry.getMessage() ); } - public void attachToThread() + void attachToThread() { TEST_METHOD.set( this ); ConsoleOutputReceiverForCurrentThread.set( this ); } - public void detachFromCurrentThread() + void detachFromCurrentThread() { TEST_METHOD.remove(); ConsoleOutputReceiverForCurrentThread.remove(); } - public static TestMethod getThreadTestMethod() + static TestMethod getThreadTestMethod() { return TEST_METHOD.get(); } - public LogicalStream getLogicalStream() + LogicalStream getLogicalStream() { if ( output == null ) { http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/c14b25fe/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestSet.java ---------------------------------------------------------------------- diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestSet.java b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestSet.java index 42e65f5..bddbbe4 100644 --- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestSet.java +++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestSet.java @@ -24,7 +24,6 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; -import org.apache.maven.surefire.report.ConsoleOutputReceiver; import org.apache.maven.surefire.report.ReportEntry; import org.apache.maven.surefire.report.RunListener; import org.apache.maven.surefire.report.SimpleReportEntry; @@ -53,10 +52,6 @@ public class TestSet private final AtomicBoolean played = new AtomicBoolean(); - private volatile LogicalStream beforeClass; - - private volatile LogicalStream afterClass; - public TestSet( Description testSetDescription ) { this.testSetDescription = testSetDescription; @@ -64,40 +59,41 @@ public class TestSet public void replay( RunListener target ) { - if ( !played.compareAndSet( false, true ) ) + if ( played.compareAndSet( false, true ) ) { - return; - } + try + { + ReportEntry report = createReportEntry( null ); - try - { - ReportEntry report = createReportEntry( null ); + target.testSetStarting( report ); - target.testSetStarting( report ); + long startTile = 0; + long endTime = 0; + for ( TestMethod testMethod : testMethods ) + { + if ( startTile == 0 || testMethod.getStartTime() < startTile ) + { + startTile = testMethod.getStartTime(); + } - if ( beforeClass != null ) - { - beforeClass.writeDetails( ( (ConsoleOutputReceiver) target ) ); - } + if ( endTime == 0 || testMethod.getEndTime() > endTime ) + { + endTime = testMethod.getEndTime(); + } - int elapsed = 0; - for ( TestMethod testMethod : testMethods ) - { - elapsed += testMethod.getElapsed(); - testMethod.replay( target ); - } + testMethod.replay( target ); + } + + int elapsed = (int) ( endTime - startTile ); - report = createReportEntry( elapsed ); + report = createReportEntry( elapsed ); - if ( afterClass != null ) + target.testSetCompleted( report ); + } + catch ( Exception e ) { - afterClass.writeDetails( ( (ConsoleOutputReceiver) target ) ); + throw new RuntimeException( e ); } - target.testSetCompleted( report ); - } - catch ( Exception e ) - { - throw new RuntimeException( e ); } } @@ -159,26 +155,4 @@ public class TestSet { return TEST_SET.get(); } - - public LogicalStream getClassLevelLogicalStream() - { - if ( numberOfCompletedChildren.get() > 0 ) - { - if ( afterClass == null ) - { - afterClass = new LogicalStream(); - } - return afterClass; - } - else - { - if ( beforeClass == null ) - { - beforeClass = new LogicalStream(); - } - return beforeClass; - } - } - - }