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