View Javadoc
1   package org.apache.maven.surefire.its.jiras;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.it.VerificationException;
23  import org.apache.maven.surefire.its.fixture.OutputValidator;
24  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
25  import org.apache.maven.surefire.its.fixture.SurefireLauncher;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import java.text.Format;
30  import java.text.NumberFormat;
31  import java.util.Iterator;
32  import java.util.Set;
33  import java.util.TreeSet;
34  
35  import static java.lang.String.format;
36  import static java.math.RoundingMode.DOWN;
37  import static java.util.Locale.ROOT;
38  import static org.hamcrest.CoreMatchers.anyOf;
39  import static org.hamcrest.CoreMatchers.containsString;
40  import static org.hamcrest.CoreMatchers.endsWith;
41  import static org.hamcrest.core.Is.is;
42  import static org.junit.Assert.assertThat;
43  import static org.junit.Assert.assertTrue;
44  
45  /**
46   * @author Kristian Rosenvold
47   */
48  public class Surefire747MethodParallelWithSuiteCountIT
49          extends SurefireJUnit4IntegrationTestCase
50  {
51      // if you want to change his constant, change it in SuiteTest1.java and SuiteTest2.java as well
52      private static final int PERFORMANCE_TEST_MULTIPLICATION_FACTOR = 4;
53  
54      private Format lowerScaleFormatter, noFractionalDigitsFormatter;
55  
56      private static Set<String> printTestLines( OutputValidator validator, String pattern )
57              throws VerificationException
58      {
59          Set<String> log = new TreeSet<String>( validator.loadLogLines() );
60          for ( Iterator<String> it = log.iterator(); it.hasNext(); )
61          {
62              String line = it.next();
63              if ( !line.contains( pattern ) )
64              {
65                  it.remove();
66              }
67          }
68          return log;
69      }
70  
71      private static long duration( String logLine )
72      {
73          return Integer.decode( logLine.split( "=" )[1] );
74      }
75  
76      @Before
77      public void init()
78      {
79          NumberFormat lowScaleFormatter = NumberFormat.getInstance( ROOT );
80          lowScaleFormatter.setRoundingMode( DOWN );
81          lowScaleFormatter.setMinimumFractionDigits( 1 );
82          lowScaleFormatter.setMaximumFractionDigits( 1 );
83          this.lowerScaleFormatter = lowScaleFormatter;
84  
85          NumberFormat noFractionalDigitsFormatter = NumberFormat.getInstance( ROOT );
86          noFractionalDigitsFormatter.setRoundingMode( DOWN );
87          noFractionalDigitsFormatter.setMinimumFractionDigits( 0 );
88          noFractionalDigitsFormatter.setMaximumFractionDigits( 0 );
89          this.noFractionalDigitsFormatter = noFractionalDigitsFormatter;
90      }
91  
92      @Test
93      public void testMethodsParallelWithSuite()
94              throws VerificationException
95      {
96          OutputValidator validator = unpack().executeTest().verifyErrorFree( 6 );
97          Set<String> testLines = printTestLines( validator, "test finished after duration=" );
98          assertThat( testLines.size(), is( 2 ) );
99          for ( String testLine : testLines )
100         {
101             long duration = duration( testLine );
102             long min = 250 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
103             long max = 750 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
104             assertTrue( format( "duration %d should be between %d and %d ms", duration, min, max ),
105                               duration > min && duration < max
106             );
107         }
108         Set<String> suiteLines = printTestLines( validator, "suite finished after duration=" );
109         assertThat( suiteLines.size(), is( 1 ) );
110         long duration = duration( suiteLines.iterator().next() );
111         long min = 750 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
112         long max = 1250 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
113         assertTrue( format( "duration %d should be between %d and %d ms", duration, min, max ),
114                           duration > min && duration < max
115         );
116 
117         String delayMin = lowerScaleFormatter.format( 0.98 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR * 0.5 );
118         String delayMax = noFractionalDigitsFormatter.format( PERFORMANCE_TEST_MULTIPLICATION_FACTOR * 0.5 ) + ".";
119 
120         for ( String line : validator.loadLogLines() )
121         {
122             if ( line.startsWith( "Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed:" ) )
123             {
124                 assertThat( line, anyOf( // 1.9xx to 2.xxx can vary depending on CI jobs
125                                                containsString( "Time elapsed: " + delayMin ),
126                                                containsString( "Time elapsed: " + delayMax )
127                         )
128                 );
129                 assertThat( line, anyOf(
130                                                endsWith( " s - in surefire747.SuiteTest1" ),
131                                                endsWith( " s - in surefire747.SuiteTest2" )
132                         )
133                 );
134             }
135         }
136     }
137 
138     @Test
139     public void testClassesParallelWithSuite()
140             throws VerificationException
141     {
142         OutputValidator validator = unpack().parallelClasses().executeTest().verifyErrorFree( 6 );
143         Set<String> testLines = printTestLines( validator, "test finished after duration=" );
144         assertThat( testLines.size(), is( 2 ) );
145         for ( String testLine : testLines )
146         {
147             long duration = duration( testLine );
148             long min = 1450 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
149             long max = 1750 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
150             assertTrue( format( "duration %d should be between %d and %d ms", duration, min, max ),
151                               duration > min && duration < max
152             );
153         }
154         Set<String> suiteLines = printTestLines( validator, "suite finished after duration=" );
155         assertThat( suiteLines.size(), is( 1 ) );
156         long duration = duration( suiteLines.iterator().next() );
157         long min = 1450 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
158         long max = 1750 * PERFORMANCE_TEST_MULTIPLICATION_FACTOR;
159         assertTrue( format( "duration %d should be between %d and %d ms", duration, min, max ),
160                           duration > min && duration < max
161         );
162     }
163 
164     public SurefireLauncher unpack()
165     {
166         return unpack( "junit47-parallel-with-suite" );
167     }
168 }