1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.handler;
21
22 import static org.junit.Assert.assertEquals;
23
24 import java.lang.reflect.Field;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.atomic.AtomicInteger;
27
28 import org.apache.mina.core.filterchain.IoFilterChain;
29 import org.apache.mina.core.service.IoHandlerAdapter;
30 import org.apache.mina.core.session.DummySession;
31 import org.apache.mina.core.session.IoSession;
32 import org.apache.mina.filter.executor.ExecutorFilter;
33 import org.apache.mina.filter.executor.OrderedThreadPoolExecutor;
34 import org.apache.mina.filter.executor.PriorityThreadPoolExecutor;
35 import org.apache.mina.filter.executor.UnorderedThreadPoolExecutor;
36 import org.junit.Test;
37
38
39
40
41
42
43
44 public class DIRMINA1156Test
45 {
46
47
48
49
50
51
52 @Test
53 public void testOrderedThreadPoolExecutorSessionHandlerThrowingError() throws Exception
54 {
55
56 final boolean[] filterTriggered = {false};
57 int corePoolSize = 1;
58 DummySession session = new DummySession();
59 IoFilterChain chain = session.getFilterChain();
60 OrderedThreadPoolExecutor executor = new OrderedThreadPoolExecutor(corePoolSize,1);
61 chain.addLast("executor", new ExecutorFilter(executor));
62 session.setHandler( new IoHandlerAdapter() {
63 @Override
64 public void messageReceived(IoSession session, Object message) throws Exception {
65 filterTriggered[0] = true;
66 throw new Error("An Error thrown during unit testing.");
67 }
68 });
69
70
71 try {
72 chain.fireMessageReceived("foo");
73
74
75 executor.shutdown();
76 executor.awaitTermination(10, TimeUnit.SECONDS);
77 if (!filterTriggered[0]) {
78 throw new IllegalStateException("Bug in test implementation: the session handler was never invoked.");
79 }
80
81
82 final Field idleWorkersField = OrderedThreadPoolExecutor.class.getDeclaredField("idleWorkers");
83 idleWorkersField.setAccessible(true);
84 final AtomicInteger idleWorkers = (AtomicInteger) idleWorkersField.get(executor);
85 assertEquals("After all tasks have finished, the amount of workers that are idle should equal the amount of workers, but did not.", executor.getPoolSize(), idleWorkers.get());
86 } finally {
87
88 if (!executor.isShutdown()) {
89 executor.shutdownNow();
90 }
91 }
92 }
93
94
95
96
97
98
99
100
101 @Test
102 public void testOrderedThreadPoolExecutorSessionHandlerThrowingRuntimeException() throws Exception
103 {
104
105 final boolean[] filterTriggered = {false};
106 int corePoolSize = 1;
107 DummySession session = new DummySession();
108 IoFilterChain chain = session.getFilterChain();
109 OrderedThreadPoolExecutor executor = new OrderedThreadPoolExecutor(corePoolSize,1);
110 chain.addLast("executor", new ExecutorFilter(executor));
111 session.setHandler( new IoHandlerAdapter() {
112 @Override
113 public void messageReceived(IoSession session, Object message) throws Exception {
114 filterTriggered[0] = true;
115 throw new RuntimeException("A RuntimeException thrown during unit testing.");
116 }
117 });
118
119
120 try {
121 chain.fireMessageReceived("foo");
122
123
124 executor.shutdown();
125 executor.awaitTermination(10, TimeUnit.SECONDS);
126 if (!filterTriggered[0]) {
127 throw new IllegalStateException("Bug in test implementation: the session handler was never invoked.");
128 }
129
130
131 final Field idleWorkersField = OrderedThreadPoolExecutor.class.getDeclaredField("idleWorkers");
132 idleWorkersField.setAccessible(true);
133 final AtomicInteger idleWorkers = (AtomicInteger) idleWorkersField.get(executor);
134 assertEquals("After all tasks have finished, the amount of workers that are idle should equal the amount of workers, but did not.", executor.getPoolSize(), idleWorkers.get());
135 } finally {
136
137 if (!executor.isShutdown()) {
138 executor.shutdownNow();
139 }
140 }
141 }
142
143
144
145
146
147
148
149
150 @Test
151 public void testOrderedThreadPoolExecutorSessionHandlerThrowingCheckedException() throws Exception
152 {
153
154 final boolean[] filterTriggered = {false};
155 int corePoolSize = 1;
156 DummySession session = new DummySession();
157 IoFilterChain chain = session.getFilterChain();
158 OrderedThreadPoolExecutor executor = new OrderedThreadPoolExecutor(corePoolSize,1);
159 chain.addLast("executor", new ExecutorFilter(executor));
160 session.setHandler( new IoHandlerAdapter() {
161 @Override
162 public void messageReceived(IoSession session, Object message) throws Exception {
163 filterTriggered[0] = true;
164 throw new Exception("A (checked) Exception thrown during unit testing.");
165 }
166 });
167
168
169 try {
170 chain.fireMessageReceived("foo");
171
172
173 executor.shutdown();
174 executor.awaitTermination(10, TimeUnit.SECONDS);
175 if (!filterTriggered[0]) {
176 throw new IllegalStateException("Bug in test implementation: the session handler was never invoked.");
177 }
178
179
180 final Field idleWorkersField = OrderedThreadPoolExecutor.class.getDeclaredField("idleWorkers");
181 idleWorkersField.setAccessible(true);
182 final AtomicInteger idleWorkers = (AtomicInteger) idleWorkersField.get(executor);
183 assertEquals("After all tasks have finished, the amount of workers that are idle should equal the amount of workers, but did not.", executor.getPoolSize(), idleWorkers.get());
184 } finally {
185
186 if (!executor.isShutdown()) {
187 executor.shutdownNow();
188 }
189 }
190 }
191
192
193
194
195
196
197
198 @Test
199 public void testUnorderedThreadPoolExecutorSessionHandlerThrowingError() throws Exception
200 {
201
202 final boolean[] filterTriggered = {false};
203 int corePoolSize = 1;
204 DummySession session = new DummySession();
205 IoFilterChain chain = session.getFilterChain();
206 UnorderedThreadPoolExecutor executor = new UnorderedThreadPoolExecutor(corePoolSize,1);
207 chain.addLast("executor", new ExecutorFilter(executor));
208 session.setHandler( new IoHandlerAdapter() {
209 @Override
210 public void messageReceived(IoSession session, Object message) throws Exception {
211 filterTriggered[0] = true;
212 throw new Error("An Error thrown during unit testing.");
213 }
214 });
215
216
217 try {
218 chain.fireMessageReceived("foo");
219
220
221 executor.shutdown();
222 executor.awaitTermination(10, TimeUnit.SECONDS);
223 if (!filterTriggered[0]) {
224 throw new IllegalStateException("Bug in test implementation: the session handler was never invoked.");
225 }
226
227
228 final Field idleWorkersField = UnorderedThreadPoolExecutor.class.getDeclaredField("idleWorkers");
229 idleWorkersField.setAccessible(true);
230 final AtomicInteger idleWorkers = (AtomicInteger) idleWorkersField.get(executor);
231 assertEquals("After all tasks have finished, the amount of workers that are idle should equal the amount of workers, but did not.", executor.getPoolSize(), idleWorkers.get());
232 } finally {
233
234 if (!executor.isShutdown()) {
235 executor.shutdownNow();
236 }
237 }
238 }
239
240
241
242
243
244
245
246
247 @Test
248 public void testUnorderedThreadPoolExecutorSessionHandlerThrowingRuntimeException() throws Exception
249 {
250
251 final boolean[] filterTriggered = {false};
252 int corePoolSize = 1;
253 DummySession session = new DummySession();
254 IoFilterChain chain = session.getFilterChain();
255 UnorderedThreadPoolExecutor executor = new UnorderedThreadPoolExecutor(corePoolSize,1);
256 chain.addLast("executor", new ExecutorFilter(executor));
257 session.setHandler( new IoHandlerAdapter() {
258 @Override
259 public void messageReceived(IoSession session, Object message) throws Exception {
260 filterTriggered[0] = true;
261 throw new RuntimeException("A RuntimeException thrown during unit testing.");
262 }
263 });
264
265
266 try {
267 chain.fireMessageReceived("foo");
268
269
270 executor.shutdown();
271 executor.awaitTermination(10, TimeUnit.SECONDS);
272 if (!filterTriggered[0]) {
273 throw new IllegalStateException("Bug in test implementation: the session handler was never invoked.");
274 }
275
276
277 final Field idleWorkersField = UnorderedThreadPoolExecutor.class.getDeclaredField("idleWorkers");
278 idleWorkersField.setAccessible(true);
279 final AtomicInteger idleWorkers = (AtomicInteger) idleWorkersField.get(executor);
280 assertEquals("After all tasks have finished, the amount of workers that are idle should equal the amount of workers, but did not.", executor.getPoolSize(), idleWorkers.get());
281 } finally {
282
283 if (!executor.isShutdown()) {
284 executor.shutdownNow();
285 }
286 }
287 }
288
289
290
291
292
293
294
295
296 @Test
297 public void testUnorderedThreadPoolExecutorSessionHandlerThrowingCheckedException() throws Exception
298 {
299
300 final boolean[] filterTriggered = {false};
301 int corePoolSize = 1;
302 DummySession session = new DummySession();
303 IoFilterChain chain = session.getFilterChain();
304 UnorderedThreadPoolExecutor executor = new UnorderedThreadPoolExecutor(corePoolSize,1);
305 chain.addLast("executor", new ExecutorFilter(executor));
306 session.setHandler( new IoHandlerAdapter() {
307 @Override
308 public void messageReceived(IoSession session, Object message) throws Exception {
309 filterTriggered[0] = true;
310 throw new Exception("A (checked) Exception thrown during unit testing.");
311 }
312 });
313
314
315 try {
316 chain.fireMessageReceived("foo");
317
318
319 executor.shutdown();
320 executor.awaitTermination(10, TimeUnit.SECONDS);
321 if (!filterTriggered[0]) {
322 throw new IllegalStateException("Bug in test implementation: the session handler was never invoked.");
323 }
324
325
326 final Field idleWorkersField = UnorderedThreadPoolExecutor.class.getDeclaredField("idleWorkers");
327 idleWorkersField.setAccessible(true);
328 final AtomicInteger idleWorkers = (AtomicInteger) idleWorkersField.get(executor);
329 assertEquals("After all tasks have finished, the amount of workers that are idle should equal the amount of workers, but did not.", executor.getPoolSize(), idleWorkers.get());
330 } finally {
331
332 if (!executor.isShutdown()) {
333 executor.shutdownNow();
334 }
335 }
336 }
337
338
339
340
341
342
343
344 @Test
345 public void testPriorityThreadPoolExecutorSessionHandlerThrowingError() throws Exception
346 {
347
348 final boolean[] filterTriggered = {false};
349 int corePoolSize = 1;
350 DummySession session = new DummySession();
351 IoFilterChain chain = session.getFilterChain();
352 PriorityThreadPoolExecutor executor = new PriorityThreadPoolExecutor(corePoolSize,1);
353 chain.addLast("executor", new ExecutorFilter(executor));
354 session.setHandler( new IoHandlerAdapter() {
355 @Override
356 public void messageReceived(IoSession session, Object message) throws Exception {
357 filterTriggered[0] = true;
358 throw new Error("An Error thrown during unit testing.");
359 }
360 });
361
362
363 try {
364 chain.fireMessageReceived("foo");
365
366
367 executor.shutdown();
368 executor.awaitTermination(10, TimeUnit.SECONDS);
369 if (!filterTriggered[0]) {
370 throw new IllegalStateException("Bug in test implementation: the session handler was never invoked.");
371 }
372
373
374 final Field idleWorkersField = PriorityThreadPoolExecutor.class.getDeclaredField("idleWorkers");
375 idleWorkersField.setAccessible(true);
376 final AtomicInteger idleWorkers = (AtomicInteger) idleWorkersField.get(executor);
377 assertEquals("After all tasks have finished, the amount of workers that are idle should equal the amount of workers, but did not.", executor.getPoolSize(), idleWorkers.get());
378 } finally {
379
380 if (!executor.isShutdown()) {
381 executor.shutdownNow();
382 }
383 }
384 }
385
386
387
388
389
390
391
392
393 @Test
394 public void testPriorityThreadPoolExecutorSessionHandlerThrowingRuntimeException() throws Exception
395 {
396
397 final boolean[] filterTriggered = {false};
398 int corePoolSize = 1;
399 DummySession session = new DummySession();
400 IoFilterChain chain = session.getFilterChain();
401 PriorityThreadPoolExecutor executor = new PriorityThreadPoolExecutor(corePoolSize,1);
402 chain.addLast("executor", new ExecutorFilter(executor));
403 session.setHandler( new IoHandlerAdapter() {
404 @Override
405 public void messageReceived(IoSession session, Object message) throws Exception {
406 filterTriggered[0] = true;
407 throw new RuntimeException("A RuntimeException thrown during unit testing.");
408 }
409 });
410
411
412 try {
413 chain.fireMessageReceived("foo");
414
415
416 executor.shutdown();
417 executor.awaitTermination(10, TimeUnit.SECONDS);
418 if (!filterTriggered[0]) {
419 throw new IllegalStateException("Bug in test implementation: the session handler was never invoked.");
420 }
421
422
423 final Field idleWorkersField = PriorityThreadPoolExecutor.class.getDeclaredField("idleWorkers");
424 idleWorkersField.setAccessible(true);
425 final AtomicInteger idleWorkers = (AtomicInteger) idleWorkersField.get(executor);
426 assertEquals("After all tasks have finished, the amount of workers that are idle should equal the amount of workers, but did not.", executor.getPoolSize(), idleWorkers.get());
427 } finally {
428
429 if (!executor.isShutdown()) {
430 executor.shutdownNow();
431 }
432 }
433 }
434
435
436
437
438
439
440
441
442 @Test
443 public void testPriorityThreadPoolExecutorSessionHandlerThrowingCheckedException() throws Exception
444 {
445
446 final boolean[] filterTriggered = {false};
447 int corePoolSize = 1;
448 DummySession session = new DummySession();
449 IoFilterChain chain = session.getFilterChain();
450 PriorityThreadPoolExecutor executor = new PriorityThreadPoolExecutor(corePoolSize,1);
451 chain.addLast("executor", new ExecutorFilter(executor));
452 session.setHandler( new IoHandlerAdapter() {
453 @Override
454 public void messageReceived(IoSession session, Object message) throws Exception {
455 filterTriggered[0] = true;
456 throw new Exception("A (checked) Exception thrown during unit testing.");
457 }
458 });
459
460
461 try {
462 chain.fireMessageReceived("foo");
463
464
465 executor.shutdown();
466 executor.awaitTermination(10, TimeUnit.SECONDS);
467 if (!filterTriggered[0]) {
468 throw new IllegalStateException("Bug in test implementation: the session handler was never invoked.");
469 }
470
471
472 final Field idleWorkersField = PriorityThreadPoolExecutor.class.getDeclaredField("idleWorkers");
473 idleWorkersField.setAccessible(true);
474 final AtomicInteger idleWorkers = (AtomicInteger) idleWorkersField.get(executor);
475 assertEquals("After all tasks have finished, the amount of workers that are idle should equal the amount of workers, but did not.", executor.getPoolSize(), idleWorkers.get());
476 } finally {
477
478 if (!executor.isShutdown()) {
479 executor.shutdownNow();
480 }
481 }
482 }
483 }