-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCounterTest.java
48 lines (40 loc) · 1.51 KB
/
CounterTest.java
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
48
package com.thread.concurrency.counter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;
@SpringBootTest
public class CounterTest {
public static Stream<Counter> counterProvider() {
return Stream.of(new LockCounter(), new PollingCounter());
}
private static void whenAdd(Counter counter, int nThreads, int addPerThread) {
try (ExecutorService executor = Executors.newFixedThreadPool(nThreads)) {
for (int i = 0; i < nThreads; i++) {
executor.submit(() -> {
for (int j = 0; j < addPerThread; j++) {
counter.add(1);
}
});
}
}
}
@ParameterizedTest
@MethodSource("counterProvider")
public void stressTest(Counter counter) {
// given
int nThreads = 100;
int addPerThread = 1000;
int expectedValue = counter.show() + nThreads * addPerThread;
// when
long start = System.currentTimeMillis();
whenAdd(counter, nThreads, addPerThread);
long end = System.currentTimeMillis();
// then
Assertions.assertEquals(expectedValue, counter.show());
System.out.println("Time elapsed: " + (end - start) + "ms");
}
}