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;
20  
21  import java.util.Arrays;
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  import org.apache.maven.surefire.its.fixture.OutputValidator;
26  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
27  import org.apache.maven.surefire.its.fixture.SurefireLauncher;
28  import org.apache.maven.surefire.its.fixture.TestFile;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertNotEquals;
34  import static org.junit.Assert.fail;
35  
36  /**
37   * Test forkCount and reuseForks
38   *
39   * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
40   */
41  public class ForkCountIT extends SurefireJUnit4IntegrationTestCase {
42  
43      private OutputValidator outputValidator;
44  
45      @BeforeClass
46      public static void installDumpPidPlugin() {
47          unpack(ForkCountIT.class, "test-helper-dump-pid-plugin", "plugin").executeInstall();
48      }
49  
50      @Test
51      public void testForkNever() {
52          String[] pids = doTest(unpack(getProject()).forkNever());
53          assertSamePids(pids);
54          assertEndWith(pids, "_1_1", 3);
55          assertEquals("my pid is equal to pid 1 of the test", getMainPID(), pids[0]);
56      }
57  
58      @Test
59      public void testForkOncePerThreadSingleThread() {
60          int threadCount = 1;
61          String[] pids = doTest(
62                  unpack(getProject()).setForkJvm().forkPerThread(threadCount).threadCount(threadCount));
63          assertSamePids(pids);
64          assertEndWith(pids, "_1_1", 3);
65          assertNotEquals("pid 1 is not the same as the main process' pid", pids[0], getMainPID());
66      }
67  
68      @Test
69      public void testForkOncePerThreadTwoThreads() {
70          int threadCount = 2;
71          String[] pids = doTest(unpack(getProject())
72                  .forkPerThread(threadCount)
73                  .threadCount(threadCount)
74                  .addGoal("-DsleepLength=7200"));
75          assertDifferentPids(pids, 2);
76          assertNotEquals("pid 1 is not the same as the main process' pid", pids[0], getMainPID());
77      }
78  
79      @Test
80      public void testForkCountOneNoReuse() {
81          String[] pids = doTest(unpack(getProject()).setForkJvm().forkCount(1).reuseForks(false));
82          assertDifferentPids(pids);
83          assertEndWith(pids, "_1_1", 3);
84          assertNotEquals("pid 1 is not the same as the main process' pid", pids[0], getMainPID());
85      }
86  
87      @Test
88      public void testForkCountOneReuse() {
89          String[] pids = doTest(unpack(getProject()).setForkJvm().forkCount(1).reuseForks(true));
90          assertSamePids(pids);
91          assertEndWith(pids, "_1_1", 3);
92          assertNotEquals("pid 1 is not the same as the main process' pid", pids[0], getMainPID());
93      }
94  
95      @Test
96      public void testForkCountTwoNoReuse() {
97          String[] pids = doTest(
98                  unpack(getProject()).setForkJvm().forkCount(2).reuseForks(false).addGoal("-DsleepLength=7200"));
99          assertDifferentPids(pids);
100         assertNotEquals("pid 1 is not the same as the main process' pid", pids[0], getMainPID());
101     }
102 
103     @Test
104     public void testForkCountTwoReuse() {
105         String[] pids =
106                 doTest(unpack(getProject()).forkCount(2).reuseForks(true).addGoal("-DsleepLength=7200"));
107         assertDifferentPids(pids, 2);
108         assertNotEquals("pid 1 is not the same as the main process' pid", pids[0], getMainPID());
109     }
110 
111     private void assertEndWith(String[] pids, String suffix, int expectedMatches) {
112         int matches = 0;
113         for (String pid : pids) {
114             if (pid.endsWith(suffix)) {
115                 matches++;
116             }
117         }
118 
119         assertEquals("suffix " + suffix + " matched the correct number of pids", expectedMatches, matches);
120     }
121 
122     private void assertDifferentPids(String[] pids, int numOfDifferentPids) {
123         Set<String> pidSet = new HashSet<>(Arrays.asList(pids));
124         assertEquals("number of different pids is not as expected", numOfDifferentPids, pidSet.size());
125     }
126 
127     @Test
128     public void testForkOnce() {
129         String[] pids = doTest(unpack(getProject()).forkOnce());
130         assertSamePids(pids);
131         assertNotEquals("pid 1 is not the same as the main process' pid", pids[0], getMainPID());
132     }
133 
134     private String getMainPID() {
135         final TestFile targetFile = outputValidator.getTargetFile("maven.pid");
136         String pid = targetFile.slurpFile();
137         return pid + " testValue_1_1";
138     }
139 
140     private void assertSamePids(String[] pids) {
141         assertEquals("pid 1 didn't match pid 2", pids[0], pids[1]);
142         assertEquals("pid 1 didn't match pid 3", pids[0], pids[2]);
143     }
144 
145     private void assertDifferentPids(String[] pids) {
146         if (pids[0].equals(pids[1])) {
147             fail("pid 1 matched pid 2: " + pids[0]);
148         }
149 
150         if (pids[0].equals(pids[2])) {
151             fail("pid 1 matched pid 3: " + pids[0]);
152         }
153 
154         if (pids[1].equals(pids[2])) {
155             fail("pid 2 matched pid 3: " + pids[0]);
156         }
157     }
158 
159     private String[] doTest(SurefireLauncher forkLauncher) {
160         forkLauncher.sysProp("testProperty", "testValue_${surefire.threadNumber}_${surefire.forkNumber}");
161         forkLauncher.addGoal("org.apache.maven.plugins.surefire:maven-dump-pid-plugin:dump-pid");
162         outputValidator = forkLauncher.executeTest();
163         outputValidator.verifyErrorFreeLog().assertTestSuiteResults(3, 0, 0, 0);
164         String[] pids = new String[3];
165         for (int i = 1; i <= pids.length; i++) {
166             final TestFile targetFile = outputValidator.getTargetFile("test" + i + "-pid");
167             String pid = targetFile.slurpFile();
168             pids[i - 1] = pid;
169         }
170         return pids;
171     }
172 
173     protected String getProject() {
174         return "fork-count";
175     }
176 }