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.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  /**
37   * TODO Add documentation
38   * 
39   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
40   */
41  public class ExecutorFilterRegressionTest {
42      private ExecutorFilter filter;
43  
44      public ExecutorFilterRegressionTest() {
45          // Do nothing
46      }
47  
48      @Before
49      public void setUp() throws Exception {
50          filter = new ExecutorFilter(8);
51      }
52  
53      @After
54      public void tearDown() throws Exception {
55          ((ExecutorService) filter.getExecutor()).shutdown();
56          filter = null;
57      }
58  
59      @Test
60      public void testEventOrder() throws Throwable {
61          final EventOrderChecker nextFilter = new EventOrderChecker();
62          final EventOrderCounter[] sessions = new EventOrderCounter[] { new EventOrderCounter(),
63                  new EventOrderCounter(), new EventOrderCounter(), new EventOrderCounter(), new EventOrderCounter(),
64                  new EventOrderCounter(), new EventOrderCounter(), new EventOrderCounter(), new EventOrderCounter(),
65                  new EventOrderCounter(), };
66          final int loop = 1000000;
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              assertEquals(loop - 1, sessions[i].lastCount.intValue());
89          }
90      }
91  
92      private static class EventOrderCounter extends DummySession {
93          Integer lastCount = null;
94  
95          /**
96           * Default constructor
97           */
98          public EventOrderCounter() {
99              super();
100         }
101 
102         public synchronized void setLastCount(Integer newCount) {
103             if (lastCount != null) {
104                 assertEquals(lastCount.intValue() + 1, newCount.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 inputClosed(IoSession session) {
138             // Do nothing
139         }
140 
141         public void messageReceived(IoSession session, Object message) {
142             try {
143                 ((EventOrderCounter) session).setLastCount((Integer) message);
144             } catch (Exception e) {
145                 if (this.throwable == null) {
146                     this.throwable = e;
147                 }
148             }
149         }
150 
151         public void messageSent(IoSession session, WriteRequest writeRequest) {
152             // Do nothing
153         }
154 
155         public void filterWrite(IoSession session, WriteRequest writeRequest) {
156             // Do nothing
157         }
158 
159         public void filterClose(IoSession session) {
160             // Do nothing
161         }
162 
163         public void sessionCreated(IoSession session) {
164             // Do nothing
165         }
166     }
167 }