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[] {
63                  new EventOrderCounter(), new EventOrderCounter(),
64                  new EventOrderCounter(), new EventOrderCounter(),
65                  new EventOrderCounter(), new EventOrderCounter(),
66                  new EventOrderCounter(), new EventOrderCounter(),
67                  new EventOrderCounter(), new EventOrderCounter(), };
68          final int loop = 1000000;
69          final int end = sessions.length - 1;
70          final ExecutorFilter filter = this.filter;
71          ExecutorService executor = (ExecutorService) filter.getExecutor();
72          //executor.setKeepAliveTime(3, TimeUnit.SECONDS);
73  
74          for (int i = 0; i < loop; i++) {
75              Integer objI = new Integer(i);
76  
77              for (int j = end; j >= 0; j--) {
78                  filter.messageReceived(nextFilter, sessions[j], objI);
79              }
80  
81              if (nextFilter.throwable != null) {
82                  throw nextFilter.throwable;
83              }
84          }
85  
86          executor.shutdown();
87          executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
88  
89          for (int i = end; i >= 0; i--) {
90              assertEquals(loop - 1, sessions[i].lastCount.intValue());
91          }
92      }
93  
94      private static class EventOrderCounter extends DummySession {
95          Integer lastCount = null;
96  
97          /**
98           * Default constructor
99           */
100         public EventOrderCounter() {
101             super();
102         }
103         
104         public synchronized void setLastCount(Integer newCount) {
105             if (lastCount != null) {
106                 assertEquals(lastCount.intValue() + 1, newCount
107                         .intValue());
108             }
109 
110             lastCount = newCount;
111         }
112     }
113 
114     private static class EventOrderChecker implements NextFilter {
115         Throwable throwable;
116 
117         /**
118          * Default constructor
119          */
120         public EventOrderChecker() {
121             super();
122         }
123         
124         public void sessionOpened(IoSession session) {
125             // Do nothing
126         }
127 
128         public void sessionClosed(IoSession session) {
129             // Do nothing
130         }
131 
132         public void sessionIdle(IoSession session, IdleStatus status) {
133             // Do nothing
134         }
135 
136         public void exceptionCaught(IoSession session, Throwable cause) {
137             // Do nothing
138         }
139 
140         public void messageReceived(IoSession session, Object message) {
141             try {
142                 ((EventOrderCounter) session).setLastCount((Integer) message);
143             } catch (Throwable t) {
144                 if (this.throwable == null) {
145                     this.throwable = t;
146                 }
147             }
148         }
149 
150         public void messageSent(IoSession session, WriteRequest writeRequest) {
151             // Do nothing
152         }
153 
154         public void filterWrite(IoSession session, WriteRequest writeRequest) {
155             // Do nothing
156         }
157 
158         public void filterClose(IoSession session) {
159             // Do nothing
160         }
161 
162         public void sessionCreated(IoSession session) {
163             // Do nothing
164         }
165     }
166 }