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 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   * TODO Add documentation
36   * 
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   */
39  public class ExecutorFilterRegressionTest extends TestCase {
40      private ExecutorFilter filter;
41  
42      public ExecutorFilterRegressionTest() {
43          // Do nothing
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 = 1000000;
66          final int end = sessions.length - 1;
67          final ExecutorFilter filter = this.filter;
68          ExecutorService executor = (ExecutorService) filter.getExecutor();
69          //executor.setKeepAliveTime(3, TimeUnit.SECONDS);
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          Integer lastCount = null;
93  
94          /**
95           * Default constructor
96           */
97          public EventOrderCounter() {
98              super();
99          }
100         
101         public synchronized void setLastCount(Integer newCount) {
102             if (lastCount != null) {
103                 Assert.assertEquals(lastCount.intValue() + 1, newCount
104                         .intValue());
105             }
106 
107             lastCount = newCount;
108         }
109     }
110 
111     private static class EventOrderChecker implements NextFilter {
112         Throwable throwable;
113 
114         /**
115          * Default constructor
116          */
117         public EventOrderChecker() {
118             super();
119         }
120         
121         public void sessionOpened(IoSession session) {
122             // Do nothing
123         }
124 
125         public void sessionClosed(IoSession session) {
126             // Do nothing
127         }
128 
129         public void sessionIdle(IoSession session, IdleStatus status) {
130             // Do nothing
131         }
132 
133         public void exceptionCaught(IoSession session, Throwable cause) {
134             // Do nothing
135         }
136 
137         public void messageReceived(IoSession session, Object message) {
138             try {
139                 ((EventOrderCounter) session).setLastCount((Integer) message);
140             } catch (Throwable t) {
141                 if (this.throwable == null) {
142                     this.throwable = t;
143                 }
144             }
145         }
146 
147         public void messageSent(IoSession session, WriteRequest writeRequest) {
148             // Do nothing
149         }
150 
151         public void filterWrite(IoSession session, WriteRequest writeRequest) {
152             // Do nothing
153         }
154 
155         public void filterClose(IoSession session) {
156             // Do nothing
157         }
158 
159         public void sessionCreated(IoSession session) {
160             // Do nothing
161         }
162     }
163 
164     public static void main(String[] args) {
165         junit.textui.TestRunner.run(ExecutorFilterRegressionTest.class);
166     }
167 }