001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.filter.executor;
021:
022: import java.util.concurrent.ExecutorService;
023: import java.util.concurrent.TimeUnit;
024:
025: import junit.framework.Assert;
026: import junit.framework.TestCase;
027:
028: import org.apache.mina.common.DummySession;
029: import org.apache.mina.common.IdleStatus;
030: import org.apache.mina.common.IoSession;
031: import org.apache.mina.common.TrafficMask;
032: import org.apache.mina.common.WriteRequest;
033: import org.apache.mina.common.IoFilter.NextFilter;
034:
035: public class ExecutorFilterRegressionTest extends TestCase {
036: private ExecutorFilter filter;
037:
038: public ExecutorFilterRegressionTest() {
039: }
040:
041: @Override
042: public void setUp() throws Exception {
043: filter = new ExecutorFilter(8);
044: }
045:
046: @Override
047: public void tearDown() throws Exception {
048: ((ExecutorService) filter.getExecutor()).shutdown();
049: filter = null;
050: }
051:
052: public void testEventOrder() throws Throwable {
053: final EventOrderChecker nextFilter = new EventOrderChecker();
054: final EventOrderCounter[] sessions = new EventOrderCounter[] {
055: new EventOrderCounter(), new EventOrderCounter(),
056: new EventOrderCounter(), new EventOrderCounter(),
057: new EventOrderCounter(), new EventOrderCounter(),
058: new EventOrderCounter(), new EventOrderCounter(),
059: new EventOrderCounter(), new EventOrderCounter(), };
060: final int loop = 10000000;
061: final int end = sessions.length - 1;
062: final ExecutorFilter filter = this .filter;
063: ExecutorService executor = (ExecutorService) filter
064: .getExecutor();
065: //executor.setKeepAliveTime(3, TimeUnit.SECONDS);
066:
067: for (int i = 0; i < loop; i++) {
068: Integer objI = new Integer(i);
069:
070: for (int j = end; j >= 0; j--) {
071: filter.messageReceived(nextFilter, sessions[j], objI);
072: }
073:
074: if (nextFilter.throwable != null) {
075: throw nextFilter.throwable;
076: }
077: }
078:
079: executor.shutdown();
080: executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
081:
082: for (int i = end; i >= 0; i--) {
083: Assert.assertEquals(loop - 1, sessions[i].lastCount
084: .intValue());
085: }
086: }
087:
088: private static class EventOrderCounter extends DummySession {
089: private Integer lastCount = null;
090:
091: public synchronized void setLastCount(Integer newCount) {
092: if (lastCount != null) {
093: Assert.assertEquals(lastCount.intValue() + 1, newCount
094: .intValue());
095: }
096:
097: lastCount = newCount;
098: }
099: }
100:
101: private static class EventOrderChecker implements NextFilter {
102: private Throwable throwable;
103:
104: public void sessionOpened(IoSession session) {
105: }
106:
107: public void sessionClosed(IoSession session) {
108: }
109:
110: public void sessionIdle(IoSession session, IdleStatus status) {
111: }
112:
113: public void exceptionCaught(IoSession session, Throwable cause) {
114: }
115:
116: public void messageReceived(IoSession session, Object message) {
117: try {
118: ((EventOrderCounter) session)
119: .setLastCount((Integer) message);
120: } catch (Throwable t) {
121: if (this .throwable == null) {
122: this .throwable = t;
123: }
124: }
125: }
126:
127: public void messageSent(IoSession session,
128: WriteRequest writeRequest) {
129: }
130:
131: public void filterWrite(IoSession session,
132: WriteRequest writeRequest) {
133: }
134:
135: public void filterClose(IoSession session) {
136: }
137:
138: public void sessionCreated(IoSession session) {
139: }
140:
141: public void filterSetTrafficMask(IoSession session,
142: TrafficMask trafficMask) {
143: }
144: }
145:
146: public static void main(String[] args) {
147: junit.textui.TestRunner.run(ExecutorFilterRegressionTest.class);
148: }
149: }
|