-
Notifications
You must be signed in to change notification settings - Fork 0
/
UseCases_Scenarios.java
145 lines (118 loc) · 3.94 KB
/
UseCases_Scenarios.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package pl.amazingcode.threadscollider.single;
import static org.assertj.core.api.BDDAssertions.then;
import static pl.amazingcode.threadscollider.ThreadsCollider.ThreadsColliderBuilder.threadsCollider;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.RepeatedTest;
import pl.amazingcode.threadscollider.Processors;
import pl.amazingcode.threadscollider.ThreadsCollider;
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
final class UseCases_Scenarios {
@RepeatedTest(10)
void Thread_safe_adding_to_set() {
// Given
Set<String> set = Collections.synchronizedSet(new HashSet<>());
List<Exception> exceptions = Collections.synchronizedList(new ArrayList<>());
// When
try (ThreadsCollider threadsCollider =
threadsCollider()
.withAction(() -> set.add("foo"))
.times(Processors.ALL)
.withThreadsExceptionsConsumer(exceptions::add)
.withAwaitTerminationTimeout(100)
.asMilliseconds()
.build()) {
threadsCollider.collide();
}
// Then
then(set).hasSize(1).containsExactly("foo");
}
@RepeatedTest(10)
void Thread_safe_adding_to_list() {
// Given
List<String> list = Collections.synchronizedList(new ArrayList<>());
// When
try (ThreadsCollider threadsCollider =
threadsCollider().withAction(() -> list.add("bar")).times(Processors.ALL).build()) {
threadsCollider.collide();
}
// Then
then(list).hasSize(Processors.ALL).containsOnly("bar");
}
@RepeatedTest(10)
void Thread_safe_adding_to_map() {
// Given
Map<String, String> map = new ConcurrentHashMap<>();
// When
try (ThreadsCollider threadsCollider =
threadsCollider().withAction(() -> map.put("foo", "bar")).times(Processors.ALL).build()) {
threadsCollider.collide();
}
// Then
then(map).hasSize(1).containsEntry("foo", "bar");
}
@RepeatedTest(10)
void Thread_safe_adding_to_map_when_absent() {
// Given
Map<String, String> map = new ConcurrentHashMap<>();
// When
try (ThreadsCollider threadsCollider =
threadsCollider()
.withAction(() -> map.putIfAbsent("foo", "bar"))
.times(Processors.ALL)
.build()) {
threadsCollider.collide();
}
// Then
then(map).hasSize(1).containsEntry("foo", "bar");
}
@RepeatedTest(10)
void Log_failed_threads_exceptions() {
// Given
Runnable failingRunnable =
() -> {
throw new IllegalStateException("message");
};
List<Exception> exceptions = Collections.synchronizedList(new ArrayList<>());
int threadsCount = 10;
// When
try (ThreadsCollider threadsCollider =
threadsCollider()
.withAction(failingRunnable)
.times(threadsCount)
.withThreadsExceptionsConsumer(exceptions::add)
.build()) {
threadsCollider.collide();
}
// Then
then(exceptions).hasSize(threadsCount).map(Exception::getMessage).containsOnly("message");
}
@RepeatedTest(10)
void Log_failed_threads_exceptions_in_thread_safe_manner() {
// Given
Runnable failingRunnable =
() -> {
throw new IllegalStateException("message");
};
List<Exception> exceptions = new ArrayList<>();
int threadsCount = 10;
// When
try (ThreadsCollider threadsCollider =
threadsCollider()
.withAction(failingRunnable)
.times(threadsCount)
.withThreadsExceptionsConsumer(exceptions::add)
.build()) {
threadsCollider.collide();
}
// Then
then(exceptions).hasSize(threadsCount).map(Exception::getMessage).containsOnly("message");
}
}