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.InputStreamReader;
024: import java.io.LineNumberReader;
025: import java.net.Socket;
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: import org.junit.Assert;
030: import org.junit.Test;
031: import org.xsocket.QAUtil;
032: import org.xsocket.connection.BlockingConnection;
033: import org.xsocket.connection.IBlockingConnection;
034:
035: /**
036: *
037: * @author grro@xsocket.org
038: */
039: public final class EchoServerTest {
040:
041: private int running = 0;
042: private final List<String> errors = new ArrayList<String>();
043:
044: @Test
045: public void testXSocketServerSide() throws Exception {
046: EchoServer server = new EchoServer(9322);
047:
048: Socket client = new Socket("localhost", 9322);
049: client.getOutputStream().write("test\r\n".getBytes());
050: LineNumberReader lnr = new LineNumberReader(
051: new InputStreamReader(client.getInputStream()));
052: String response = lnr.readLine();
053:
054: Assert.assertEquals("test", response);
055:
056: lnr.close();
057: client.close();
058:
059: server.close();
060: }
061:
062: @Test
063: public void testXSocketBothSide() throws Exception {
064:
065: EchoServer server = new EchoServer(9312);
066:
067: for (int i = 0; i < 10; i++) {
068: Thread t = new Thread() {
069: @Override
070: public void run() {
071: running++;
072:
073: try {
074: for (int j = 0; j < 5; j++) {
075: IBlockingConnection connection = new BlockingConnection(
076: "127.0.0.1", 9312);
077:
078: connection.write("hello\r\n");
079: String response = connection
080: .readStringByDelimiter("\r\n");
081: if (!response.equals("hello")) {
082: errors.add("got " + response
083: + " instead of hello");
084: }
085:
086: connection.write("you\r\n");
087: response = connection
088: .readStringByDelimiter("\r\n");
089: if (!response.equals("you")) {
090: errors.add("got " + response
091: + " instead of you");
092: }
093:
094: connection.close();
095: }
096:
097: } catch (Exception e) {
098: errors.add(e.toString());
099: }
100:
101: running--;
102: }
103: };
104: t.start();
105: }
106:
107: do {
108: QAUtil.sleep(100);
109: } while (running > 0);
110:
111: server.close();
112:
113: Assert.assertTrue(errors.isEmpty());
114: }
115: }
|