001: /*
002: * Copyright (c) xsocket.org, 2006-2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection;
022:
023: import java.io.IOException;
024: import java.nio.BufferUnderflowException;
025:
026: import org.junit.Assert;
027: import org.junit.Test;
028: import org.xsocket.Execution;
029: import org.xsocket.MaxReadSizeExceededException;
030: import org.xsocket.QAUtil;
031: import org.xsocket.connection.IConnectHandler;
032: import org.xsocket.connection.IDataHandler;
033: import org.xsocket.connection.INonBlockingConnection;
034: import org.xsocket.connection.IServer;
035: import org.xsocket.connection.NonBlockingConnection;
036: import org.xsocket.connection.Server;
037: import org.xsocket.connection.ConnectionUtils;
038:
039: /**
040: *
041: * @author grro@xsocket.org
042: */
043: public final class NonBlockingConnnectionResetTest {
044:
045: @Test
046: public void testIdleTimout() throws Exception {
047:
048: IServer server = new Server(new EchoHandler());
049: ConnectionUtils.start(server);
050:
051: Handler hdl = new Handler();
052: NonBlockingConnection con = new NonBlockingConnection(
053: "localhost", server.getLocalPort(), hdl);
054:
055: con.setConnectionTimeoutMillis(60 * 60 * 1000);
056: con.setIdleTimeoutMillis(2 * 1000);
057:
058: con.write("test" + EchoHandler.DELIMITER);
059: QAUtil.sleep(300);
060:
061: Assert.assertEquals("test", hdl.data);
062: Assert.assertFalse(hdl.connectionTimeoutOccurred);
063: Assert.assertFalse(hdl.idleTimeoutOccurred);
064: hdl.data = null;
065:
066: con.reset();
067:
068: con.setHandler(hdl);
069:
070: con.write("test2" + EchoHandler.DELIMITER);
071: QAUtil.sleep(300);
072:
073: Assert.assertEquals("test2", hdl.data);
074: Assert.assertFalse(hdl.connectionTimeoutOccurred);
075: Assert.assertFalse(hdl.idleTimeoutOccurred);
076: hdl.data = null;
077:
078: QAUtil.sleep(2000);
079:
080: Assert.assertFalse(hdl.connectionTimeoutOccurred);
081: Assert.assertTrue(hdl.idleTimeoutOccurred);
082: QAUtil.assertTimeout(hdl.elapsed, 2000, 1800, 3000);
083:
084: con.close();
085: server.close();
086: }
087:
088: @Test
089: public void testConnectionTimeout() throws Exception {
090:
091: IServer server = new Server(new EchoHandler());
092: ConnectionUtils.start(server);
093:
094: Handler hdl = new Handler();
095: NonBlockingConnection con = new NonBlockingConnection(
096: "localhost", server.getLocalPort(), hdl);
097:
098: con.setConnectionTimeoutMillis(2 * 1000);
099: con.setIdleTimeoutMillis(60 * 60 * 1000);
100:
101: con.write("test" + EchoHandler.DELIMITER);
102: QAUtil.sleep(300);
103:
104: Assert.assertEquals("test", hdl.data);
105: Assert.assertFalse(hdl.connectionTimeoutOccurred);
106: Assert.assertFalse(hdl.idleTimeoutOccurred);
107: hdl.data = null;
108:
109: con.reset();
110:
111: con.setHandler(hdl);
112:
113: con.write("test2" + EchoHandler.DELIMITER);
114: QAUtil.sleep(300);
115:
116: Assert.assertEquals("test2", hdl.data);
117: Assert.assertFalse(hdl.connectionTimeoutOccurred);
118: Assert.assertFalse(hdl.idleTimeoutOccurred);
119: hdl.data = null;
120:
121: QAUtil.sleep(2000);
122:
123: Assert.assertTrue(hdl.connectionTimeoutOccurred);
124: Assert.assertFalse(hdl.idleTimeoutOccurred);
125: QAUtil.assertTimeout(hdl.elapsed, 2000, 1800, 3000);
126:
127: con.close();
128: server.close();
129: }
130:
131: @Execution(Execution.NONTHREADED)
132: private static final class Handler implements IConnectHandler,
133: IDataHandler, IIdleTimeoutHandler,
134: IConnectionTimeoutHandler {
135:
136: private String data = null;
137:
138: private boolean connectionTimeoutOccurred = false;
139: private boolean idleTimeoutOccurred = false;
140:
141: private long elapsed = 0;
142:
143: public boolean onConnect(INonBlockingConnection connection)
144: throws IOException, BufferUnderflowException,
145: MaxReadSizeExceededException {
146: elapsed = System.currentTimeMillis();
147: return true;
148: }
149:
150: public boolean onData(INonBlockingConnection connection)
151: throws IOException, BufferUnderflowException,
152: MaxReadSizeExceededException {
153: data = connection
154: .readStringByDelimiter(EchoHandler.DELIMITER);
155: return true;
156: }
157:
158: public boolean onConnectionTimeout(
159: INonBlockingConnection connection) throws IOException {
160: elapsed = System.currentTimeMillis() - elapsed;
161: connectionTimeoutOccurred = true;
162: return true;
163: }
164:
165: public boolean onIdleTimeout(INonBlockingConnection connection)
166: throws IOException {
167: elapsed = System.currentTimeMillis() - elapsed;
168: idleTimeoutOccurred = true;
169: return true;
170: }
171:
172: }
173: }
|