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.lang.reflect.Method;
025: import java.nio.BufferUnderflowException;
026:
027: import org.junit.Assert;
028: import org.junit.Test;
029: import org.xsocket.QAUtil;
030: import org.xsocket.connection.BlockingConnection;
031: import org.xsocket.connection.IBlockingConnection;
032: import org.xsocket.connection.IConnectHandler;
033: import org.xsocket.connection.IDataHandler;
034: import org.xsocket.connection.INonBlockingConnection;
035: import org.xsocket.connection.IServer;
036: import org.xsocket.connection.NonBlockingConnection;
037: import org.xsocket.connection.Server;
038: import org.xsocket.connection.ConnectionUtils;
039: import org.xsocket.connection.spi.IIoHandler;
040:
041: /**
042: *
043: * @author grro@xsocket.org
044: */
045: public final class DecoupledReceiveAndConsumeTest {
046:
047: private static final String DELIMITER = "\n";
048: private static final int SLEEPTIME = 300;
049:
050: @Test
051: public void testSimple() throws Exception {
052: TestHandler hdl = new TestHandler();
053: IServer server = new Server(hdl);
054: ConnectionUtils.start(server);
055:
056: IBlockingConnection bc = new BlockingConnection("localhost",
057: server.getLocalPort());
058: bc.setAutoflush(false);
059:
060: // send first package & wait
061: bc.write(QAUtil.generateByteArray(600));
062: bc.write(DELIMITER);
063: bc.flush();
064:
065: QAUtil.sleep(SLEEPTIME);
066:
067: // send second one
068: bc.write(QAUtil.generateByteArray(1200));
069: bc.write(DELIMITER);
070: bc.flush();
071:
072: QAUtil.sleep(SLEEPTIME);
073:
074: // 600 byte (first call) should have been read by the server handler
075: Assert.assertTrue("consumed data size is " + hdl.readSize
076: + " but should be 600", hdl.readSize == 600);
077:
078: // and 1200 of the second call should already be in the receive queue
079: Assert.assertTrue("receivedAndonConsumend data size is "
080: + hdl.connection.available() + " but should be "
081: + (1200 + DELIMITER.length()), hdl.connection
082: .available() == (1200 + DELIMITER.length()));
083:
084: bc.close();
085: server.close();
086: }
087:
088: private static final class TestHandler implements IConnectHandler,
089: IDataHandler {
090:
091: private int readSize = 0;
092: private NonBlockingConnection connection = null;
093:
094: public boolean onConnect(INonBlockingConnection connection)
095: throws IOException {
096: this .connection = (NonBlockingConnection) connection;
097: return true;
098: }
099:
100: public boolean onData(INonBlockingConnection connection)
101: throws IOException, BufferUnderflowException {
102: byte[] bytes = connection.readBytesByDelimiter(DELIMITER);
103: readSize = bytes.length;
104:
105: // simulate blocking
106: try {
107: Thread.sleep(1000000);
108: } catch (InterruptedException ignore) {
109: }
110:
111: return true;
112: }
113: }
114: }
|