1 package org.apache.maven.surefire.util.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39
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 }