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.session.TrafficMask;
33  import org.apache.mina.core.write.WriteRequest;
34  
35  /**
36   * TODO Add documentation
37   * 
38   * @author The Apache MINA Project (dev@mina.apache.org)
39   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
40   */
41  public class ExecutorFilterRegressionTest extends TestCase {
42      private ExecutorFilter filter;
43  
44      public ExecutorFilterRegressionTest() {
45      }
46  
47      @Override
48      public void setUp() throws Exception {
49          filter = new ExecutorFilter(8);
50      }
51  
52      @Override
53      public void tearDown() throws Exception {
54          ((ExecutorService) filter.getExecutor()).shutdown();
55          filter = null;
56      }
57  
58      public void testEventOrder() throws Throwable {
59          final EventOrderChecker nextFilter = new EventOrderChecker();
60          final EventOrderCounter[] sessions = new EventOrderCounter[] {
61                  new EventOrderCounter(), new EventOrderCounter(),
62                  new EventOrderCounter(), new EventOrderCounter(),
63                  new EventOrderCounter(), new EventOrderCounter(),
64                  new EventOrderCounter(), new EventOrderCounter(),
65                  new EventOrderCounter(), new EventOrderCounter(), };
66          final int loop = 10000000;
67          final int end = sessions.length - 1;
68          final ExecutorFilter filter = this.filter;
69          ExecutorService executor = (ExecutorService) filter.getExecutor();
70          //executor.setKeepAliveTime(3, TimeUnit.SECONDS);
71  
72          for (int i = 0; i < loop; i++) {
73              Integer objI = new Integer(i);
74  
75              for (int j = end; j >= 0; j--) {
76                  filter.messageReceived(nextFilter, sessions[j], objI);
77              }
78  
79              if (nextFilter.throwable != null) {
80                  throw nextFilter.throwable;
81              }
82          }
83  
84          executor.shutdown();
85          executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
86  
87          for (int i = end; i >= 0; i--) {
88              Assert.assertEquals(loop - 1, sessions[i].lastCount.intValue());
89          }
90      }
91  
92      private static class EventOrderCounter extends DummySession {
93          private Integer lastCount = null;
94  
95          public synchronized void setLastCount(Integer newCount) {
96              if (lastCount != null) {
97                  Assert.assertEquals(lastCount.intValue() + 1, newCount
98                          .intValue());
99              }
100 
101             lastCount = newCount;
102         }
103     }
104 
105     private static class EventOrderChecker implements NextFilter {
106         private Throwable throwable;
107 
108         public void sessionOpened(IoSession session) {
109         }
110 
111         public void sessionClosed(IoSession session) {
112         }
113 
114         public void sessionIdle(IoSession session, IdleStatus status) {
115         }
116 
117         public void exceptionCaught(IoSession session, Throwable cause) {
118         }
119 
120         public void messageReceived(IoSession session, Object message) {
121             try {
122                 ((EventOrderCounter) session).setLastCount((Integer) message);
123             } catch (Throwable t) {
124                 if (this.throwable == null) {
125                     this.throwable = t;
126                 }
127             }
128         }
129 
130         public void messageSent(IoSession session, WriteRequest writeRequest) {
131         }
132 
133         public void filterWrite(IoSession session, WriteRequest writeRequest) {
134         }
135 
136         public void filterClose(IoSession session) {
137         }
138 
139         public void sessionCreated(IoSession session) {
140         }
141 
142         public void filterSetTrafficMask(IoSession session, TrafficMask trafficMask) {
143         }
144     }
145 
146     public static void main(String[] args) {
147         junit.textui.TestRunner.run(ExecutorFilterRegressionTest.class);
148     }
149 }