View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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.apache.mina.filter.FilterEvent;
33  import org.junit.After;
34  import org.junit.Before;
35  import org.junit.Test;
36  
37  /**
38   * TODO Add documentation
39   * 
40   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
41   */
42  public class ExecutorFilterRegressionTest {
43      private ExecutorFilter filter;
44  
45      public ExecutorFilterRegressionTest() {
46          // Do nothing
47      }
48  
49      @Before
50      public void setUp() throws Exception {
51          filter = new ExecutorFilter(8);
52      }
53  
54      @After
55      public void tearDown() throws Exception {
56          ((ExecutorService) filter.getExecutor()).shutdown();
57          filter = null;
58      }
59  
60      @Test
61      public void testEventOrder() throws Throwable {
62          final EventOrderChecker nextFilter = new EventOrderChecker();
63          final EventOrderCounter[] sessions = new EventOrderCounter[] { new EventOrderCounter(),
64                  new EventOrderCounter(), new EventOrderCounter(), new EventOrderCounter(), new EventOrderCounter(),
65                  new EventOrderCounter(), new EventOrderCounter(), new EventOrderCounter(), new EventOrderCounter(),
66                  new EventOrderCounter(), };
67          final int loop = 1000000;
68          final int end = sessions.length - 1;
69          final ExecutorFilter filter = this.filter;
70          ExecutorService executor = (ExecutorService) filter.getExecutor();
71          //executor.setKeepAliveTime(3, TimeUnit.SECONDS);
72  
73          for (int i = 0; i < loop; i++) {
74              Integer objI = new Integer(i);
75  
76              for (int j = end; j >= 0; j--) {
77                  filter.messageReceived(nextFilter, sessions[j], objI);
78              }
79  
80              if (nextFilter.throwable != null) {
81                  throw nextFilter.throwable;
82              }
83          }
84  
85          executor.shutdown();
86          executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
87  
88          for (int i = end; i >= 0; i--) {
89              assertEquals(loop - 1, sessions[i].lastCount.intValue());
90          }
91      }
92  
93      private static class EventOrderCounter extends DummySession {
94          Integer lastCount = null;
95  
96          /**
97           * Default constructor
98           */
99          public EventOrderCounter() {
100             super();
101         }
102 
103         public synchronized void setLastCount(Integer newCount) {
104             if (lastCount != null) {
105                 assertEquals(lastCount.intValue() + 1, newCount.intValue());
106             }
107 
108             lastCount = newCount;
109         }
110     }
111 
112     private static class EventOrderChecker implements NextFilter {
113         Throwable throwable;
114 
115         /**
116          * Default constructor
117          */
118         public EventOrderChecker() {
119             super();
120         }
121 
122         public void sessionOpened(IoSession session) {
123             // Do nothing
124         }
125 
126         public void sessionClosed(IoSession session) {
127             // Do nothing
128         }
129 
130         public void sessionIdle(IoSession session, IdleStatus status) {
131             // Do nothing
132         }
133 
134         public void exceptionCaught(IoSession session, Throwable cause) {
135             // Do nothing
136         }
137 
138         public void inputClosed(IoSession session) {
139             // Do nothing
140         }
141 
142         public void messageReceived(IoSession session, Object message) {
143             try {
144                 ((EventOrderCounter) session).setLastCount((Integer) message);
145             } catch (Exception e) {
146                 if (this.throwable == null) {
147                     this.throwable = e;
148                 }
149             }
150         }
151 
152         public void messageSent(IoSession session, WriteRequest writeRequest) {
153             // Do nothing
154         }
155 
156         public void filterWrite(IoSession session, WriteRequest writeRequest) {
157             // Do nothing
158         }
159 
160         public void filterClose(IoSession session) {
161             // Do nothing
162         }
163 
164         public void sessionCreated(IoSession session) {
165             // Do nothing
166         }
167         
168         public void event(IoSession session, FilterEvent event) {
169             // Do nothing
170         }
171     }
172 }