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