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 static org.junit.Assert.assertEquals;
23
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.TimeUnit;
26
27 import org.apache.mina.core.filterchain.IoFilter.NextFilter;
28 import org.apache.mina.core.session.DummySession;
29 import org.apache.mina.core.session.IdleStatus;
30 import org.apache.mina.core.session.IoSession;
31 import org.apache.mina.core.write.WriteRequest;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35
36
37
38
39
40
41 public class ExecutorFilterRegressionTest {
42 private ExecutorFilter filter;
43
44 public ExecutorFilterRegressionTest() {
45
46 }
47
48 @Before
49 public void setUp() throws Exception {
50 filter = new ExecutorFilter(8);
51 }
52
53 @After
54 public void tearDown() throws Exception {
55 ((ExecutorService) filter.getExecutor()).shutdown();
56 filter = null;
57 }
58
59 @Test
60 public void testEventOrder() throws Throwable {
61 final EventOrderChecker nextFilter = new EventOrderChecker();
62 final EventOrderCounter[] sessions = new EventOrderCounter[] {
63 new EventOrderCounter(), new EventOrderCounter(),
64 new EventOrderCounter(), new EventOrderCounter(),
65 new EventOrderCounter(), new EventOrderCounter(),
66 new EventOrderCounter(), new EventOrderCounter(),
67 new EventOrderCounter(), new EventOrderCounter(), };
68 final int loop = 1000000;
69 final int end = sessions.length - 1;
70 final ExecutorFilter filter = this.filter;
71 ExecutorService executor = (ExecutorService) filter.getExecutor();
72
73
74 for (int i = 0; i < loop; i++) {
75 Integer objI = new Integer(i);
76
77 for (int j = end; j >= 0; j--) {
78 filter.messageReceived(nextFilter, sessions[j], objI);
79 }
80
81 if (nextFilter.throwable != null) {
82 throw nextFilter.throwable;
83 }
84 }
85
86 executor.shutdown();
87 executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
88
89 for (int i = end; i >= 0; i--) {
90 assertEquals(loop - 1, sessions[i].lastCount.intValue());
91 }
92 }
93
94 private static class EventOrderCounter extends DummySession {
95 Integer lastCount = null;
96
97
98
99
100 public EventOrderCounter() {
101 super();
102 }
103
104 public synchronized void setLastCount(Integer newCount) {
105 if (lastCount != null) {
106 assertEquals(lastCount.intValue() + 1, newCount
107 .intValue());
108 }
109
110 lastCount = newCount;
111 }
112 }
113
114 private static class EventOrderChecker implements NextFilter {
115 Throwable throwable;
116
117
118
119
120 public EventOrderChecker() {
121 super();
122 }
123
124 public void sessionOpened(IoSession session) {
125
126 }
127
128 public void sessionClosed(IoSession session) {
129
130 }
131
132 public void sessionIdle(IoSession session, IdleStatus status) {
133
134 }
135
136 public void exceptionCaught(IoSession session, Throwable cause) {
137
138 }
139
140 public void messageReceived(IoSession session, Object message) {
141 try {
142 ((EventOrderCounter) session).setLastCount((Integer) message);
143 } catch (Throwable t) {
144 if (this.throwable == null) {
145 this.throwable = t;
146 }
147 }
148 }
149
150 public void messageSent(IoSession session, WriteRequest writeRequest) {
151
152 }
153
154 public void filterWrite(IoSession session, WriteRequest writeRequest) {
155
156 }
157
158 public void filterClose(IoSession session) {
159
160 }
161
162 public void sessionCreated(IoSession session) {
163
164 }
165 }
166 }