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.net.SocketTimeoutException;
025: import java.util.logging.Logger;
026:
027: import org.junit.Assert;
028: import org.junit.Test;
029: import org.xsocket.connection.BlockingConnection;
030: import org.xsocket.connection.IBlockingConnection;
031: import org.xsocket.connection.IDataHandler;
032: import org.xsocket.connection.INonBlockingConnection;
033: import org.xsocket.connection.IServer;
034: import org.xsocket.connection.Server;
035: import org.xsocket.connection.ConnectionUtils;
036:
037: /**
038: *
039: * @author grro@xsocket.org
040: */
041: public final class CloseTest {
042:
043: private static final Logger LOG = Logger.getLogger(CloseTest.class
044: .getName());
045:
046: private static final String DELIMITER = "x";
047:
048: private static final String QUIT_COMMAND = "QUIT";
049: private static final String OK_RESPONSE = "OK";
050: private static final String CLOSED_RESPONSE = "CLOSED";
051:
052: private int open = 0;
053:
054: private String error = null;
055:
056: @Test
057: public void testSimple() throws Exception {
058: IServer server = new Server(new ServerHandler());
059: ConnectionUtils.start(server);
060:
061: call("localhost", server.getLocalPort());
062:
063: server.close();
064: }
065:
066: @Test
067: public void testBulk() throws Exception {
068:
069: final IServer server = new Server(new ServerHandler());
070: ConnectionUtils.start(server);
071:
072: for (int i = 0; i < 5; i++) {
073: Thread t = new Thread(new Runnable() {
074: public void run() {
075: open++;
076: for (int j = 0; j < 3; j++) {
077: try {
078: call("localhost", server.getLocalPort());
079: System.out.print(".");
080: } catch (IOException ioe) {
081: System.out.println(ioe.toString());
082: error = ioe.toString();
083: }
084: }
085: open--;
086: }
087: });
088:
089: t.start();
090: }
091:
092: do {
093: try {
094: Thread.sleep(500);
095: } catch (InterruptedException igonre) {
096: }
097: } while (open > 0);
098:
099: Assert.assertNull(error, error);
100:
101: server.close();
102: }
103:
104: private void call(String srvAddress, int srvPort)
105: throws IOException {
106: IBlockingConnection connection = new BlockingConnection(
107: srvAddress, srvPort);
108: connection.setAutoflush(true);
109: connection.setReceiveTimeoutMillis(1000);
110:
111: connection.write("hello" + DELIMITER);
112: Assert.assertTrue(connection.readStringByDelimiter(DELIMITER,
113: Integer.MAX_VALUE).equals(OK_RESPONSE));
114:
115: connection.write("some command" + DELIMITER);
116: Assert.assertTrue(connection.readStringByDelimiter(DELIMITER,
117: Integer.MAX_VALUE).equals(OK_RESPONSE));
118:
119: connection.write(QUIT_COMMAND + DELIMITER);
120: try {
121: Assert.assertTrue(connection.readStringByDelimiter(
122: DELIMITER, Integer.MAX_VALUE).equals(
123: CLOSED_RESPONSE));
124: } catch (SocketTimeoutException stoe) {
125: System.out
126: .println("socket time out reached by waiting for quit response");
127: throw stoe;
128: }
129:
130: try {
131: connection.close();
132: } catch (IOException ioe) {
133: // ioe should have been thrown, because the connection is closed by the server
134: }
135: }
136:
137: private static class ServerHandler implements IDataHandler {
138:
139: public boolean onData(INonBlockingConnection connection)
140: throws IOException {
141: String request = connection.readStringByDelimiter(
142: DELIMITER, Integer.MAX_VALUE);
143:
144: if (request.equals(QUIT_COMMAND)) {
145: connection.write(CLOSED_RESPONSE + DELIMITER);
146: LOG.fine("[" + connection.getId()
147: + "] server-side closing of connection");
148: connection.close();
149:
150: } else {
151: connection.write(OK_RESPONSE + DELIMITER);
152: }
153: return true;
154: }
155:
156: @Override
157: public Object clone() throws CloneNotSupportedException {
158: return super.clone();
159: }
160: }
161: }
|