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 The Apache MINA Project (dev@mina.apache.org)
38   * @version $Rev: 713957 $, $Date: 2008-11-14 10:27:16 +0100 (Fri, 14 Nov 2008) $
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          //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          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 }