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