1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.synchronisation;
23
24 import static net.grinder.testutility.AssertUtilities.assertContains;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotSame;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertSame;
30 import static org.junit.Assert.fail;
31
32 import java.util.Set;
33 import java.util.concurrent.atomic.AtomicInteger;
34
35 import net.grinder.synchronisation.BarrierGroup.Listener;
36 import net.grinder.synchronisation.messages.BarrierIdentity;
37
38 import org.junit.Before;
39 import org.junit.Test;
40
41
42
43
44
45
46
47
48 public class TestLocalBarrierGroups {
49
50 private static final BarrierIdentity ID1 = new MyBarrierIdentity("ID1");
51 private static final BarrierIdentity ID2 = new MyBarrierIdentity("ID2");
52
53 private static class MyBarrierIdentity implements BarrierIdentity {
54 private final String m_name;
55
56 public MyBarrierIdentity(String name) {
57 m_name = name;
58 }
59
60 @Override public String toString() {
61 return m_name;
62 }
63 }
64
65 private int m_awakenCount = 0;
66
67 private LocalBarrierGroups m_groups;
68
69 @Before public void setUp() {
70 m_groups = new LocalBarrierGroups();
71 }
72
73 @Test public void testCreateAndRetrieve() throws Exception {
74 assertNull(m_groups.getExistingGroup("A"));
75
76 final BarrierGroup a = m_groups.getGroup("A");
77 assertEquals("A", a.getName());
78 assertSame(a, m_groups.getGroup("A"));
79 assertNotSame(a, m_groups.getGroup("B"));
80
81 assertSame(a, m_groups.getExistingGroup("A"));
82
83 a.addBarrier();
84 assertEquals("(1 [])", a.toString());
85
86 a.removeBarriers(1);
87 assertEquals("(cancelled)", a.toString());
88
89 assertNotSame(a, m_groups.getGroup("A"));
90 }
91
92 private BarrierGroup createBarrierGroup(String groupName) {
93 final BarrierGroup bg = m_groups.getGroup(groupName);
94
95 bg.addListener(new Listener() {
96 public void awaken(Set<BarrierIdentity> waiters) {
97 ++m_awakenCount;
98 }
99 });
100
101 return bg;
102 }
103
104 @Test public void testBarrierGroup() {
105 final BarrierGroup bg = createBarrierGroup("Foo");
106
107 assertEquals("Foo", bg.getName());
108 assertEquals(0, m_awakenCount);
109 }
110
111 @Test public void testBarrierGroupAddWaiter() throws Exception {
112 final BarrierGroup bg = createBarrierGroup("Foo");
113
114 bg.addBarrier();
115 bg.addBarrier();
116
117 bg.addWaiter(ID1);
118 assertEquals(0, m_awakenCount);
119
120 assertEquals("(2 [ID1])", bg.toString());
121
122 bg.addWaiter(ID2);
123 assertEquals(1, m_awakenCount);
124
125 assertEquals("(2 [])", bg.toString());
126
127 bg.addWaiter(ID2);
128 assertEquals(1, m_awakenCount);
129
130 bg.addWaiter(ID1);
131 assertEquals(2, m_awakenCount);
132 }
133
134 @Test public void testRemoveBarriers() throws Exception {
135 final BarrierGroup bg = createBarrierGroup("Foo");
136
137 bg.addBarrier();
138 bg.addBarrier();
139 bg.addBarrier();
140
141 bg.addWaiter(ID1);
142 bg.addWaiter(ID2);
143 assertEquals(0, m_awakenCount);
144
145 bg.removeBarriers(1);
146 assertEquals(1, m_awakenCount);
147 }
148
149 @Test public void testRemoveTooManyBarriers() throws Exception {
150 final BarrierGroup bg = createBarrierGroup("Foo");
151
152 bg.addBarrier();
153 bg.addBarrier();
154 bg.addBarrier();
155
156 bg.removeBarriers(1);
157 bg.removeBarriers(1);
158
159 try {
160 bg.removeBarriers(2);
161 fail("Expected IllegalStateException");
162 }
163 catch (IllegalStateException e) {
164 }
165
166 assertEquals(0, m_awakenCount);
167 }
168
169 @Test public void testInvalidGroup() throws Exception {
170 final BarrierGroup bg = createBarrierGroup("Foo");
171
172 bg.addBarrier();
173 bg.removeBarriers(1);
174
175 try {
176 bg.addWaiter(null);
177 fail("Expected IllegalStateException");
178 }
179 catch (IllegalStateException e) {
180 }
181
182 try {
183 bg.addBarrier();
184 fail("Expected IllegalStateException");
185 }
186 catch (IllegalStateException e) {
187 }
188
189 try {
190 bg.removeBarriers(1);
191 fail("Expected IllegalStateException");
192 }
193 catch (IllegalStateException e) {
194 }
195
196 assertEquals(0, m_awakenCount);
197 }
198
199 @Test public void addMoreWaitersThanBarriers() throws Exception {
200 final BarrierGroup bg = createBarrierGroup("Foo");
201
202 try {
203 bg.addWaiter(ID2);
204 fail("Expected IllegalStateException");
205 }
206 catch (IllegalStateException e) {
207 }
208 }
209
210 @Test public void testCancelWaiter() throws Exception {
211 final BarrierGroup bg = createBarrierGroup("Foo");
212
213 bg.addBarrier();
214 bg.addBarrier();
215
216 bg.addWaiter(ID1);
217 assertEquals(0, m_awakenCount);
218
219 bg.cancelWaiter(ID2);
220
221 bg.cancelWaiter(ID1);
222
223 bg.addWaiter(ID2);
224 assertEquals(0, m_awakenCount);
225
226 bg.addWaiter(ID1);
227 assertEquals(1, m_awakenCount);
228 }
229
230 @Test public void testCancelAll() throws Exception {
231 final BarrierGroup bg = createBarrierGroup("Foo");
232 final BarrierGroup bg2 = createBarrierGroup("bah");
233
234 bg.addBarrier();
235 bg.addBarrier();
236 bg2.addBarrier();
237
238 bg.addWaiter(ID1);
239 assertEquals(0, m_awakenCount);
240
241 m_groups.cancelAll();
242
243 try {
244 bg.addWaiter(ID2);
245 fail("Expected IllegalStateException");
246 }
247 catch (IllegalStateException e) {
248 }
249 }
250
251 @Test public void testRemoveListener() throws Exception {
252 final BarrierGroup bg = createBarrierGroup("Foo");
253
254 final AtomicInteger awakenCount = new AtomicInteger();
255
256 final Listener listener = new Listener() {
257 public void awaken(Set<BarrierIdentity> waiters) {
258 awakenCount.incrementAndGet();
259 }
260 };
261
262 bg.addListener(listener);
263
264 bg.addBarrier();
265 bg.addWaiter(ID1);
266 assertEquals(1, m_awakenCount);
267 assertEquals(1, awakenCount.get());
268
269 bg.removeListener(listener);
270
271 bg.addWaiter(ID1);
272 assertEquals(2, m_awakenCount);
273 assertEquals(1, awakenCount.get());
274 }
275
276 @Test public void testGroupsToString() {
277 assertEquals("LocalBarrierGroups[{}]", m_groups.toString());
278
279 createBarrierGroup("foo");
280 createBarrierGroup("bar");
281
282 assertContains(m_groups.toString(), "foo");
283 assertContains(m_groups.toString(), "bar");
284 }
285 }