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.api.util.internal;
20  
21  import java.util.concurrent.ExecutionException;
22  import java.util.concurrent.FutureTask;
23  import java.util.concurrent.atomic.AtomicBoolean;
24  import java.util.concurrent.atomic.AtomicInteger;
25  
26  import org.junit.Test;
27  
28  import static org.apache.maven.surefire.api.util.internal.ConcurrencyUtils.runIfZeroCountDown;
29  import static org.hamcrest.MatcherAssert.assertThat;
30  import static org.hamcrest.Matchers.is;
31  import static org.junit.Assert.assertFalse;
32  import static org.junit.Assert.assertTrue;
33  
34  /**
35   * Concurrency utilities.
36   *
37   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
38   * @since 2.19
39   */
40  public class ConcurrencyUtilsTest {
41  
42      @Test
43      public void countDownShouldBeUnchangedAsZeroNegativeTest() {
44          AtomicInteger atomicCounter = new AtomicInteger(0);
45          AtomicBoolean runner = new AtomicBoolean();
46          runIfZeroCountDown(() -> runner.set(true), atomicCounter);
47          assertFalse(runner.get());
48          assertThat(atomicCounter.get(), is(0));
49      }
50  
51      @Test
52      public void countDownShouldBeUnchangedAsNegativeNegativeTest() {
53          AtomicInteger atomicCounter = new AtomicInteger(-1);
54          AtomicBoolean runner = new AtomicBoolean();
55          runIfZeroCountDown(() -> runner.set(true), atomicCounter);
56          assertFalse(runner.get());
57          assertThat(atomicCounter.get(), is(-1));
58      }
59  
60      @Test
61      public void countDownShouldBeDecreasedByOneThreadModification() {
62          AtomicInteger atomicCounter = new AtomicInteger(10);
63          AtomicBoolean runner = new AtomicBoolean();
64          runIfZeroCountDown(() -> runner.set(true), atomicCounter);
65          assertFalse(runner.get());
66          assertThat(atomicCounter.get(), is(9));
67      }
68  
69      @Test
70      public void countDownToZeroShouldBeDecreasedByOneThreadModification() {
71          AtomicInteger atomicCounter = new AtomicInteger(1);
72          AtomicBoolean runner = new AtomicBoolean();
73          runIfZeroCountDown(() -> runner.set(true), atomicCounter);
74          assertTrue(runner.get());
75          assertThat(atomicCounter.get(), is(0));
76      }
77  
78      @Test
79      public void countDownShouldBeDecreasedByTwoThreadsModification() throws ExecutionException, InterruptedException {
80          AtomicInteger atomicCounter = new AtomicInteger(3);
81  
82          FutureTask<Boolean> task = new FutureTask<>(() -> {
83              AtomicBoolean runner = new AtomicBoolean();
84              runIfZeroCountDown(() -> runner.set(true), atomicCounter);
85              return runner.get();
86          });
87          Thread t = new Thread(task);
88          t.start();
89  
90          AtomicBoolean runner = new AtomicBoolean();
91          runIfZeroCountDown(() -> runner.set(true), atomicCounter);
92          assertFalse(runner.get());
93  
94          assertFalse(task.get());
95  
96          assertThat(atomicCounter.get(), is(1));
97  
98          runner.set(false);
99          runIfZeroCountDown(() -> runner.set(true), atomicCounter);
100         assertTrue(runner.get());
101 
102         assertThat(atomicCounter.get(), is(0));
103     }
104 }