forked from npryce/goos-code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtomicBigCounterTests.java
More file actions
47 lines (35 loc) · 1.2 KB
/
Copy pathAtomicBigCounterTests.java
File metadata and controls
47 lines (35 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package book.example.threading.races;
import org.junit.Test;
import java.math.BigInteger;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
public class AtomicBigCounterTests {
AtomicBigCounter counter = new AtomicBigCounter();
@Test
public void
isInitiallyZero() {
assertThat(counter.count(), equalTo(BigInteger.ZERO));
}
@Test
public void
canIncreaseCounter() {
counter.inc();
assertThat(counter.count(), equalTo(BigInteger.valueOf(1)));
counter.inc();
assertThat(counter.count(), equalTo(BigInteger.valueOf(2)));
counter.inc();
assertThat(counter.count(), equalTo(BigInteger.valueOf(3)));
}
@Test
public void
canIncrementCounterFromMultipleThreadsSimultaneously() throws InterruptedException {
MultithreadedStressTester stressTester = new MultithreadedStressTester(25000);
stressTester.stress(new Runnable() {
public void run() {
counter.inc();
}
});
stressTester.shutdown();
assertThat("final count", counter.count(), equalTo(BigInteger.valueOf(stressTester.totalActionCount())));
}
}