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.transport;
021:
022: import java.net.InetSocketAddress;
023: import java.util.concurrent.CountDownLatch;
024: import java.util.concurrent.TimeUnit;
025:
026: import junit.framework.Assert;
027: import junit.framework.TestCase;
028:
029: import org.apache.mina.common.ConnectFuture;
030: import org.apache.mina.common.IoAcceptor;
031: import org.apache.mina.common.IoConnector;
032: import org.apache.mina.common.IoHandlerAdapter;
033: import org.apache.mina.common.IoSession;
034: import org.apache.mina.common.IoSessionInitializer;
035: import org.apache.mina.common.RuntimeIoException;
036: import org.apache.mina.util.AvailablePortFinder;
037:
038: /**
039: * Tests a generic {@link IoConnector}.
040: *
041: * @author The Apache MINA Project (dev@mina.apache.org)
042: * @version $Rev: 607102 $, $Date: 2007-12-27 11:09:45 -0700 (Thu, 27 Dec 2007) $
043: */
044: public abstract class AbstractConnectorTest extends TestCase {
045:
046: protected abstract IoAcceptor createAcceptor();
047:
048: protected abstract IoConnector createConnector();
049:
050: public void testConnectFutureSuccessTiming() throws Exception {
051: int port = AvailablePortFinder.getNextAvailable(1025);
052: IoAcceptor acceptor = createAcceptor();
053: acceptor.setHandler(new IoHandlerAdapter());
054: acceptor.bind(new InetSocketAddress(port));
055:
056: try {
057: final StringBuffer buf = new StringBuffer();
058: IoConnector connector = createConnector();
059: connector.setHandler(new IoHandlerAdapter() {
060: @Override
061: public void sessionCreated(IoSession session) {
062: buf.append("1");
063: }
064:
065: @Override
066: public void sessionOpened(IoSession session) {
067: buf.append("2");
068: }
069:
070: @Override
071: public void exceptionCaught(IoSession session,
072: Throwable cause) {
073: buf.append("X");
074: }
075: });
076: ConnectFuture future = connector
077: .connect(new InetSocketAddress("localhost", port));
078: future.awaitUninterruptibly();
079: buf.append("3");
080: future.getSession().close();
081: Assert.assertEquals("123", buf.toString());
082: } finally {
083: acceptor.dispose();
084: }
085: }
086:
087: public void testConnectFutureFailureTiming() throws Exception {
088: int port = AvailablePortFinder.getNextAvailable(1025);
089: final StringBuffer buf = new StringBuffer();
090:
091: IoConnector connector = createConnector();
092: connector.setHandler(new IoHandlerAdapter() {
093: @Override
094: public void sessionCreated(IoSession session) {
095: buf.append("X");
096: }
097:
098: @Override
099: public void sessionOpened(IoSession session) {
100: buf.append("Y");
101: }
102:
103: @Override
104: public void exceptionCaught(IoSession session,
105: Throwable cause) {
106: buf.append("Z");
107: }
108: });
109:
110: try {
111: ConnectFuture future = connector
112: .connect(new InetSocketAddress("localhost", port));
113: future.awaitUninterruptibly();
114: buf.append("1");
115: try {
116: future.getSession().close();
117: fail();
118: } catch (RuntimeIoException e) {
119: // OK.
120: }
121: Assert.assertEquals("1", buf.toString());
122: } finally {
123: connector.dispose();
124: }
125: }
126:
127: /**
128: * Test to make sure the SessionCallback gets invoked before IoHandler.sessionCreated.
129: */
130: public void testSessionCallbackInvocation() throws Exception {
131: final int callbackInvoked = 0;
132: final int sessionCreatedInvoked = 1;
133: final int sessionCreatedInvokedBeforeCallback = 2;
134: final boolean[] assertions = { false, false, false };
135: final CountDownLatch latch = new CountDownLatch(2);
136: final ConnectFuture[] callbackFuture = new ConnectFuture[1];
137:
138: int port = AvailablePortFinder.getNextAvailable(1025);
139: IoAcceptor acceptor = createAcceptor();
140: acceptor.setHandler(new IoHandlerAdapter());
141: InetSocketAddress address = new InetSocketAddress(port);
142: acceptor.bind(address);
143:
144: IoConnector connector = createConnector();
145: connector.setHandler(new IoHandlerAdapter() {
146: @Override
147: public void sessionCreated(IoSession session)
148: throws Exception {
149: assertions[sessionCreatedInvoked] = true;
150: assertions[sessionCreatedInvokedBeforeCallback] = !assertions[callbackInvoked];
151: latch.countDown();
152: }
153: });
154:
155: ConnectFuture future = connector.connect(address,
156: new IoSessionInitializer<ConnectFuture>() {
157: public void initializeSession(IoSession session,
158: ConnectFuture future) {
159: assertions[callbackInvoked] = true;
160: callbackFuture[0] = future;
161: latch.countDown();
162: }
163: });
164:
165: assertTrue(
166: "Timed out waiting for callback and IoHandler.sessionCreated to be invoked",
167: latch.await(5, TimeUnit.SECONDS));
168: assertTrue("Callback was not invoked",
169: assertions[callbackInvoked]);
170: assertTrue("IoHandler.sessionCreated was not invoked",
171: assertions[sessionCreatedInvoked]);
172: assertFalse(
173: "IoHandler.sessionCreated was invoked before session callback",
174: assertions[sessionCreatedInvokedBeforeCallback]);
175: assertSame(
176: "Callback future should have been same future as returned by connect",
177: future, callbackFuture[0]);
178: }
179: }
|