1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.filter.executor;
21
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.TimeUnit;
24
25 import junit.framework.Assert;
26 import junit.framework.TestCase;
27
28 import org.apache.mina.core.filterchain.IoFilter.NextFilter;
29 import org.apache.mina.core.session.DummySession;
30 import org.apache.mina.core.session.IdleStatus;
31 import org.apache.mina.core.session.IoSession;
32 import org.apache.mina.core.write.WriteRequest;
33
34
35
36
37
38
39
40 public class ExecutorFilterRegressionTest extends TestCase {
41 private ExecutorFilter filter;
42
43 public ExecutorFilterRegressionTest() {
44 }
45
46 @Override
47 public void setUp() throws Exception {
48 filter = new ExecutorFilter(8);
49 }
50
51 @Override
52 public void tearDown() throws Exception {
53 ((ExecutorService) filter.getExecutor()).shutdown();
54 filter = null;
55 }
56
57 public void testEventOrder() throws Throwable {
58 final EventOrderChecker nextFilter = new EventOrderChecker();
59 final EventOrderCounter[] sessions = new EventOrderCounter[] {
60 new EventOrderCounter(), new EventOrderCounter(),
61 new EventOrderCounter(), new EventOrderCounter(),
62 new EventOrderCounter(), new EventOrderCounter(),
63 new EventOrderCounter(), new EventOrderCounter(),
64 new EventOrderCounter(), new EventOrderCounter(), };
65 final int loop = 10000000;
66 final int end = sessions.length - 1;
67 final ExecutorFilter filter = this.filter;
68 ExecutorService executor = (ExecutorService) filter.getExecutor();
69
70
71 for (int i = 0; i < loop; i++) {
72 Integer objI = new Integer(i);
73
74 for (int j = end; j >= 0; j--) {
75 filter.messageReceived(nextFilter, sessions[j], objI);
76 }
77
78 if (nextFilter.throwable != null) {
79 throw nextFilter.throwable;
80 }
81 }
82
83 executor.shutdown();
84 executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
85
86 for (int i = end; i >= 0; i--) {
87 Assert.assertEquals(loop - 1, sessions[i].lastCount.intValue());
88 }
89 }
90
91 private static class EventOrderCounter extends DummySession {
92 private Integer lastCount = null;
93
94 public synchronized void setLastCount(Integer newCount) {
95 if (lastCount != null) {
96 Assert.assertEquals(lastCount.intValue() + 1, newCount
97 .intValue());
98 }
99
100 lastCount = newCount;
101 }
102 }
103
104 private static class EventOrderChecker implements NextFilter {
105 private Throwable throwable;
106
107 public void sessionOpened(IoSession session) {
108 }
109
110 public void sessionClosed(IoSession session) {
111 }
112
113 public void sessionIdle(IoSession session, IdleStatus status) {
114 }
115
116 public void exceptionCaught(IoSession session, Throwable cause) {
117 }
118
119 public void messageReceived(IoSession session, Object message) {
120 try {
121 ((EventOrderCounter) session).setLastCount((Integer) message);
122 } catch (Throwable t) {
123 if (this.throwable == null) {
124 this.throwable = t;
125 }
126 }
127 }
128
129 public void messageSent(IoSession session, WriteRequest writeRequest) {
130 }
131
132 public void filterWrite(IoSession session, WriteRequest writeRequest) {
133 }
134
135 public void filterClose(IoSession session) {
136 }
137
138 public void sessionCreated(IoSession session) {
139 }
140 }
141
142 public static void main(String[] args) {
143 junit.textui.TestRunner.run(ExecutorFilterRegressionTest.class);
144 }
145 }