test clean ups

This commit is contained in:
Greg Wilkins 2011-11-16 12:26:28 +11:00
parent e3bdf43112
commit 0fdc5c5d2e
5 changed files with 107 additions and 19 deletions

View File

@ -111,24 +111,27 @@ public class ChannelEndPoint implements EndPoint
LOG.debug("ishut {}",this);
if (_channel.isOpen())
{
if (_channel instanceof SocketChannel)
if (_socket!=null)
{
Socket socket= ((SocketChannel)_channel).socket();
try
{
if (!socket.isInputShutdown())
socket.shutdownInput();
if (!_socket.isInputShutdown())
{
// System.err.println("ISHUT "+_socket);
_socket.shutdownInput();
}
}
catch(SocketException e)
{
{
// System.err.println(e);
LOG.debug(e.toString());
LOG.ignore(e);
if (!socket.isClosed())
if (!_socket.isClosed())
close();
}
finally
{
if(socket.isOutputShutdown() && !socket.isClosed())
if(_socket.isOutputShutdown() && !_socket.isClosed())
close();
}
}
@ -150,24 +153,26 @@ public class ChannelEndPoint implements EndPoint
LOG.debug("oshut {}",this);
if (_channel.isOpen())
{
if (_channel instanceof SocketChannel)
if (_socket!=null)
{
Socket socket= ((SocketChannel)_channel).socket();
try
{
if (!socket.isOutputShutdown())
socket.shutdownOutput();
if (!_socket.isOutputShutdown())
{
// System.err.println("OSHUT "+_socket);
_socket.shutdownOutput();
}
}
catch(SocketException e)
{
LOG.debug(e.toString());
LOG.ignore(e);
if (!socket.isClosed())
if (!_socket.isClosed())
close();
}
finally
{
if (socket.isInputShutdown() && !socket.isClosed())
if (_socket.isInputShutdown() && !_socket.isClosed())
close();
}
}
@ -200,6 +205,8 @@ public class ChannelEndPoint implements EndPoint
public void close() throws IOException
{
LOG.debug("close {}",this);
// System.err.println("CLOSE "+_socket);
_channel.close();
}

View File

@ -24,11 +24,15 @@ public abstract class EndPointTest<T extends EndPoint>
EndPointPair<T> c = newConnection();
Buffer buffer = new IndirectNIOBuffer(4096);
// Client sends a request
c.client.flush(new ByteArrayBuffer("request"));
// Server receives the request
int len = c.server.fill(buffer);
assertEquals(7,len);
assertEquals("request",buffer.toString());
// Client and server are open
assertTrue(c.client.isOpen());
assertFalse(c.client.isInputShutdown());
assertFalse(c.client.isOutputShutdown());
@ -36,9 +40,11 @@ public abstract class EndPointTest<T extends EndPoint>
assertFalse(c.server.isInputShutdown());
assertFalse(c.server.isOutputShutdown());
// Server sends response and closes output
c.server.flush(new ByteArrayBuffer("response"));
c.server.shutdownOutput();
// client server are open, server is oshut
assertTrue(c.client.isOpen());
assertFalse(c.client.isInputShutdown());
assertFalse(c.client.isOutputShutdown());
@ -46,11 +52,13 @@ public abstract class EndPointTest<T extends EndPoint>
assertFalse(c.server.isInputShutdown());
assertTrue(c.server.isOutputShutdown());
// Client reads response
buffer.clear();
len = c.client.fill(buffer);
assertEquals(8,len);
assertEquals("response",buffer.toString());
// Client and server are open, server is oshut
assertTrue(c.client.isOpen());
assertFalse(c.client.isInputShutdown());
assertFalse(c.client.isOutputShutdown());
@ -58,10 +66,12 @@ public abstract class EndPointTest<T extends EndPoint>
assertFalse(c.server.isInputShutdown());
assertTrue(c.server.isOutputShutdown());
// Client reads -1
buffer.clear();
len = c.client.fill(buffer);
assertEquals(-1,len);
// Client and server are open, server is oshut, client is ishut
assertTrue(c.client.isOpen());
assertTrue(c.client.isInputShutdown());
assertFalse(c.client.isOutputShutdown());
@ -69,19 +79,23 @@ public abstract class EndPointTest<T extends EndPoint>
assertFalse(c.server.isInputShutdown());
assertTrue(c.server.isOutputShutdown());
// Client shutsdown output, which is a close because already ishut
c.client.shutdownOutput();
// Client is closed. Server is open and oshut
assertFalse(c.client.isOpen());
assertTrue(c.client.isInputShutdown());
assertTrue(c.client.isOutputShutdown());
assertTrue(c.server.isOpen());
assertFalse(c.server.isInputShutdown());
assertTrue(c.server.isOutputShutdown());
// Server reads close
buffer.clear();
len = c.server.fill(buffer);
assertEquals(-1,len);
// Client and Server are closed
assertFalse(c.client.isOpen());
assertTrue(c.client.isInputShutdown());
assertTrue(c.client.isOutputShutdown());

View File

@ -23,6 +23,8 @@ import java.io.ByteArrayOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import org.eclipse.jetty.util.IO;
import org.junit.Assert;
@ -51,14 +53,12 @@ public class IOTest
}
@Test
public void testHalfCloses() throws Exception
public void testHalfClose() throws Exception
{
ServerSocket connector = new ServerSocket(0);
Socket client = new Socket("localhost",connector.getLocalPort());
System.err.println(client);
Socket server = connector.accept();
System.err.println(server);
// we can write both ways
client.getOutputStream().write(1);
@ -129,8 +129,64 @@ public class IOTest
// which has to be closed explictly
server.close();
assertTrue(server.isClosed());
}
@Test
public void testHalfCloseClientServer() throws Exception
{
ServerSocketChannel connector = ServerSocketChannel.open();
connector.socket().bind(null);
Socket client = new Socket("localhost",connector.socket().getLocalPort());
client.setSoTimeout(1000);
client.setSoLinger(false,-1);
Socket server = connector.accept().socket();
server.setSoTimeout(1000);
server.setSoLinger(false,-1);
// Write from client to server
client.getOutputStream().write(1);
// Server reads
assertEquals(1,server.getInputStream().read());
// Write from server to client with oshut
server.getOutputStream().write(1);
System.err.println("OSHUT "+server);
server.shutdownOutput();
// Client reads response
assertEquals(1,client.getInputStream().read());
// Client reads -1 and does ishut
assertEquals(-1,client.getInputStream().read());
assertFalse(client.isInputShutdown());
System.err.println("ISHUT "+client);
client.shutdownInput();
// Client ???
System.err.println("OSHUT "+client);
client.shutdownOutput();
System.err.println("CLOSE "+client);
client.close();
// Server reads -1, does ishut and then close
assertEquals(-1,server.getInputStream().read());
assertFalse(server.isInputShutdown());
System.err.println("ISHUT "+server);
try
{
server.shutdownInput();
}
catch(SocketException e)
{
System.err.println(e);
}
System.err.println("CLOSE "+server);
server.close();
}

View File

@ -36,4 +36,9 @@ public class ChannelEndPointTest extends EndPointTest<ChannelEndPoint>
return c;
}
@Override
public void testClientServerExchange() throws Exception
{
super.testClientServerExchange();
}
}

View File

@ -209,8 +209,14 @@ public class SelectChannelEndPointTest
assertTrue(b>0);
assertEquals(c,(char)b);
}
client.close();
int i=0;
while (server.isOpen())
{
assert(i++<10);
Thread.sleep(10);
}
}
@ -324,7 +330,7 @@ public class SelectChannelEndPointTest
public void testStress() throws Exception
{
Socket client = newClient();
client.setSoTimeout(10000);
client.setSoTimeout(30000);
SocketChannel server = _connector.accept();
server.configureBlocking(false);