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: import java.nio.ByteBuffer;
026: import java.nio.channels.ClosedChannelException;
027: import java.util.ArrayList;
028: import java.util.List;
029: import java.util.logging.Level;
030:
031: import org.junit.Assert;
032: import org.junit.Test;
033: import org.xsocket.MaxReadSizeExceededException;
034: import org.xsocket.QAUtil;
035: import org.xsocket.connection.BlockingConnection;
036: import org.xsocket.connection.IBlockingConnection;
037: import org.xsocket.connection.IConnectHandler;
038: import org.xsocket.connection.IDataHandler;
039: import org.xsocket.connection.INonBlockingConnection;
040: import org.xsocket.connection.IServer;
041: import org.xsocket.connection.Server;
042: import org.xsocket.connection.ConnectionUtils;
043: import org.xsocket.connection.IConnection.FlushMode;
044:
045: /**
046: *
047: * @author grro@xsocket.org
048: */
049: public final class ReadOnServerCloseTest {
050:
051: private static final int CHUNK_SIZE = 4000;
052: private static final int CHUNK_COUNT = 9;
053:
054: @Test
055: public void testReadOnClose() throws Exception {
056: IServer server = new Server(new Handler());
057: ConnectionUtils.start(server);
058:
059: IBlockingConnection connection = new BlockingConnection(
060: "localhost", server.getLocalPort());
061: connection.setAutoflush(false);
062:
063: connection.write(CHUNK_SIZE);
064: connection.write(CHUNK_COUNT);
065: connection.flush();
066:
067: byte[] data = connection.readBytesByLength(CHUNK_SIZE
068: * CHUNK_COUNT);
069: Assert.assertEquals(data.length, CHUNK_SIZE * CHUNK_COUNT);
070:
071: try {
072: connection.close();
073: } catch (ClosedChannelException ignore) {
074: }
075: server.close();
076: }
077:
078: private static final class Handler implements IConnectHandler,
079: IDataHandler {
080:
081: public boolean onConnect(INonBlockingConnection connection)
082: throws IOException {
083: connection.setAutoflush(false);
084: connection.setFlushmode(FlushMode.ASYNC);
085:
086: return true;
087: }
088:
089: public boolean onData(INonBlockingConnection connection)
090: throws IOException, BufferUnderflowException,
091: MaxReadSizeExceededException {
092: int length = connection.readInt();
093: int chunks = connection.readInt();
094:
095: for (int i = 0; i < chunks; i++) {
096: byte[] data = QAUtil.generateByteArray(length);
097: connection.write(data);
098: connection.flush();
099: }
100: connection.close();
101:
102: return true;
103: }
104: }
105: }
|