Issue #2226 - SelectChannelEndPoint test cleanup
+ Renamed SelectChannelEndPointInterestsTest to SocketChannelEndPointInterestsTest + Merged EndPointTest abstract class into SocketChannelEndPointTest as it's only used by that testcase Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
parent
13224e4ee8
commit
7c5b49fb7f
|
@ -1,157 +0,0 @@
|
||||||
//
|
|
||||||
// ========================================================================
|
|
||||||
// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
// All rights reserved. This program and the accompanying materials
|
|
||||||
// are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
// and Apache License v2.0 which accompanies this distribution.
|
|
||||||
//
|
|
||||||
// The Eclipse Public License is available at
|
|
||||||
// http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
//
|
|
||||||
// The Apache License v2.0 is available at
|
|
||||||
// http://www.opensource.org/licenses/apache2.0.php
|
|
||||||
//
|
|
||||||
// You may elect to redistribute this code under either of these licenses.
|
|
||||||
// ========================================================================
|
|
||||||
//
|
|
||||||
|
|
||||||
package org.eclipse.jetty.io;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
import org.eclipse.jetty.util.BufferUtil;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public abstract class EndPointTest<T extends EndPoint>
|
|
||||||
{
|
|
||||||
public static class EndPointPair<T>
|
|
||||||
{
|
|
||||||
public T client;
|
|
||||||
public T server;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract EndPointPair<T> newConnection() throws Exception;
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testClientServerExchange() throws Exception
|
|
||||||
{
|
|
||||||
EndPointPair<T> c = newConnection();
|
|
||||||
ByteBuffer buffer = BufferUtil.allocate(4096);
|
|
||||||
|
|
||||||
// Client sends a request
|
|
||||||
c.client.flush(BufferUtil.toBuffer("request"));
|
|
||||||
|
|
||||||
// Server receives the request
|
|
||||||
int len = c.server.fill(buffer);
|
|
||||||
assertEquals(7,len);
|
|
||||||
assertEquals("request",BufferUtil.toString(buffer));
|
|
||||||
|
|
||||||
// Client and server are open
|
|
||||||
assertTrue(c.client.isOpen());
|
|
||||||
assertFalse(c.client.isOutputShutdown());
|
|
||||||
assertTrue(c.server.isOpen());
|
|
||||||
assertFalse(c.server.isOutputShutdown());
|
|
||||||
|
|
||||||
// Server sends response and closes output
|
|
||||||
c.server.flush(BufferUtil.toBuffer("response"));
|
|
||||||
c.server.shutdownOutput();
|
|
||||||
|
|
||||||
// client server are open, server is oshut
|
|
||||||
assertTrue(c.client.isOpen());
|
|
||||||
assertFalse(c.client.isOutputShutdown());
|
|
||||||
assertTrue(c.server.isOpen());
|
|
||||||
assertTrue(c.server.isOutputShutdown());
|
|
||||||
|
|
||||||
// Client reads response
|
|
||||||
BufferUtil.clear(buffer);
|
|
||||||
len = c.client.fill(buffer);
|
|
||||||
assertEquals(8,len);
|
|
||||||
assertEquals("response",BufferUtil.toString(buffer));
|
|
||||||
|
|
||||||
// Client and server are open, server is oshut
|
|
||||||
assertTrue(c.client.isOpen());
|
|
||||||
assertFalse(c.client.isOutputShutdown());
|
|
||||||
assertTrue(c.server.isOpen());
|
|
||||||
assertTrue(c.server.isOutputShutdown());
|
|
||||||
|
|
||||||
// Client reads -1
|
|
||||||
BufferUtil.clear(buffer);
|
|
||||||
len = c.client.fill(buffer);
|
|
||||||
assertEquals(-1,len);
|
|
||||||
|
|
||||||
// Client and server are open, server is oshut, client is ishut
|
|
||||||
assertTrue(c.client.isOpen());
|
|
||||||
assertFalse(c.client.isOutputShutdown());
|
|
||||||
assertTrue(c.server.isOpen());
|
|
||||||
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.isOutputShutdown());
|
|
||||||
assertTrue(c.server.isOpen());
|
|
||||||
assertTrue(c.server.isOutputShutdown());
|
|
||||||
|
|
||||||
// Server reads close
|
|
||||||
BufferUtil.clear(buffer);
|
|
||||||
len = c.server.fill(buffer);
|
|
||||||
assertEquals(-1,len);
|
|
||||||
|
|
||||||
// Client and Server are closed
|
|
||||||
assertFalse(c.client.isOpen());
|
|
||||||
assertTrue(c.client.isOutputShutdown());
|
|
||||||
assertFalse(c.server.isOpen());
|
|
||||||
assertTrue(c.server.isOutputShutdown());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testClientClose() throws Exception
|
|
||||||
{
|
|
||||||
EndPointPair<T> c = newConnection();
|
|
||||||
ByteBuffer buffer = BufferUtil.allocate(4096);
|
|
||||||
|
|
||||||
c.client.flush(BufferUtil.toBuffer("request"));
|
|
||||||
int len = c.server.fill(buffer);
|
|
||||||
assertEquals(7,len);
|
|
||||||
assertEquals("request",BufferUtil.toString(buffer));
|
|
||||||
|
|
||||||
assertTrue(c.client.isOpen());
|
|
||||||
assertFalse(c.client.isOutputShutdown());
|
|
||||||
assertTrue(c.server.isOpen());
|
|
||||||
assertFalse(c.server.isOutputShutdown());
|
|
||||||
|
|
||||||
c.client.close();
|
|
||||||
|
|
||||||
assertFalse(c.client.isOpen());
|
|
||||||
assertTrue(c.client.isOutputShutdown());
|
|
||||||
assertTrue(c.server.isOpen());
|
|
||||||
assertFalse(c.server.isOutputShutdown());
|
|
||||||
|
|
||||||
len = c.server.fill(buffer);
|
|
||||||
assertEquals(-1,len);
|
|
||||||
|
|
||||||
assertFalse(c.client.isOpen());
|
|
||||||
assertTrue(c.client.isOutputShutdown());
|
|
||||||
assertTrue(c.server.isOpen());
|
|
||||||
assertFalse(c.server.isOutputShutdown());
|
|
||||||
|
|
||||||
c.server.shutdownOutput();
|
|
||||||
|
|
||||||
assertFalse(c.client.isOpen());
|
|
||||||
assertTrue(c.client.isOutputShutdown());
|
|
||||||
assertFalse(c.server.isOpen());
|
|
||||||
assertTrue(c.server.isOutputShutdown());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -43,7 +43,7 @@ import org.junit.After;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class SelectChannelEndPointInterestsTest
|
public class SocketChannelEndPointInterestsTest
|
||||||
{
|
{
|
||||||
private QueuedThreadPool threadPool;
|
private QueuedThreadPool threadPool;
|
||||||
private Scheduler scheduler;
|
private Scheduler scheduler;
|
|
@ -18,14 +18,27 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.io;
|
package org.eclipse.jetty.io;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.ServerSocketChannel;
|
import java.nio.channels.ServerSocketChannel;
|
||||||
import java.nio.channels.SocketChannel;
|
import java.nio.channels.SocketChannel;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
public class SocketChannelEndPointTest extends EndPointTest<SocketChannelEndPoint>
|
public class SocketChannelEndPointTest
|
||||||
{
|
{
|
||||||
|
public static class EndPointPair
|
||||||
|
{
|
||||||
|
public SocketChannelEndPoint client;
|
||||||
|
public SocketChannelEndPoint server;
|
||||||
|
}
|
||||||
|
|
||||||
static ServerSocketChannel connector;
|
static ServerSocketChannel connector;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
|
@ -42,25 +55,126 @@ public class SocketChannelEndPointTest extends EndPointTest<SocketChannelEndPoin
|
||||||
connector=null;
|
connector=null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private EndPointPair newConnection() throws Exception
|
||||||
protected EndPointPair<SocketChannelEndPoint> newConnection() throws Exception
|
|
||||||
{
|
{
|
||||||
EndPointPair<SocketChannelEndPoint> c = new EndPointPair<>();
|
EndPointPair c = new EndPointPair();
|
||||||
|
|
||||||
c.client=new SocketChannelEndPoint(SocketChannel.open(connector.socket().getLocalSocketAddress()),null,null,null);
|
c.client=new SocketChannelEndPoint(SocketChannel.open(connector.socket().getLocalSocketAddress()),null,null,null);
|
||||||
c.server=new SocketChannelEndPoint(connector.accept(),null,null,null);
|
c.server=new SocketChannelEndPoint(connector.accept(),null,null,null);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Test
|
||||||
public void testClientClose() throws Exception
|
|
||||||
{
|
|
||||||
super.testClientClose();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void testClientServerExchange() throws Exception
|
public void testClientServerExchange() throws Exception
|
||||||
{
|
{
|
||||||
super.testClientServerExchange();
|
EndPointPair c = newConnection();
|
||||||
|
ByteBuffer buffer = BufferUtil.allocate(4096);
|
||||||
|
|
||||||
|
// Client sends a request
|
||||||
|
c.client.flush(BufferUtil.toBuffer("request"));
|
||||||
|
|
||||||
|
// Server receives the request
|
||||||
|
int len = c.server.fill(buffer);
|
||||||
|
assertEquals(7,len);
|
||||||
|
assertEquals("request",BufferUtil.toString(buffer));
|
||||||
|
|
||||||
|
// Client and server are open
|
||||||
|
assertTrue(c.client.isOpen());
|
||||||
|
assertFalse(c.client.isOutputShutdown());
|
||||||
|
assertTrue(c.server.isOpen());
|
||||||
|
assertFalse(c.server.isOutputShutdown());
|
||||||
|
|
||||||
|
// Server sends response and closes output
|
||||||
|
c.server.flush(BufferUtil.toBuffer("response"));
|
||||||
|
c.server.shutdownOutput();
|
||||||
|
|
||||||
|
// client server are open, server is oshut
|
||||||
|
assertTrue(c.client.isOpen());
|
||||||
|
assertFalse(c.client.isOutputShutdown());
|
||||||
|
assertTrue(c.server.isOpen());
|
||||||
|
assertTrue(c.server.isOutputShutdown());
|
||||||
|
|
||||||
|
// Client reads response
|
||||||
|
BufferUtil.clear(buffer);
|
||||||
|
len = c.client.fill(buffer);
|
||||||
|
assertEquals(8,len);
|
||||||
|
assertEquals("response",BufferUtil.toString(buffer));
|
||||||
|
|
||||||
|
// Client and server are open, server is oshut
|
||||||
|
assertTrue(c.client.isOpen());
|
||||||
|
assertFalse(c.client.isOutputShutdown());
|
||||||
|
assertTrue(c.server.isOpen());
|
||||||
|
assertTrue(c.server.isOutputShutdown());
|
||||||
|
|
||||||
|
// Client reads -1
|
||||||
|
BufferUtil.clear(buffer);
|
||||||
|
len = c.client.fill(buffer);
|
||||||
|
assertEquals(-1,len);
|
||||||
|
|
||||||
|
// Client and server are open, server is oshut, client is ishut
|
||||||
|
assertTrue(c.client.isOpen());
|
||||||
|
assertFalse(c.client.isOutputShutdown());
|
||||||
|
assertTrue(c.server.isOpen());
|
||||||
|
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.isOutputShutdown());
|
||||||
|
assertTrue(c.server.isOpen());
|
||||||
|
assertTrue(c.server.isOutputShutdown());
|
||||||
|
|
||||||
|
// Server reads close
|
||||||
|
BufferUtil.clear(buffer);
|
||||||
|
len = c.server.fill(buffer);
|
||||||
|
assertEquals(-1,len);
|
||||||
|
|
||||||
|
// Client and Server are closed
|
||||||
|
assertFalse(c.client.isOpen());
|
||||||
|
assertTrue(c.client.isOutputShutdown());
|
||||||
|
assertFalse(c.server.isOpen());
|
||||||
|
assertTrue(c.server.isOutputShutdown());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testClientClose() throws Exception
|
||||||
|
{
|
||||||
|
EndPointPair c = newConnection();
|
||||||
|
ByteBuffer buffer = BufferUtil.allocate(4096);
|
||||||
|
|
||||||
|
c.client.flush(BufferUtil.toBuffer("request"));
|
||||||
|
int len = c.server.fill(buffer);
|
||||||
|
assertEquals(7,len);
|
||||||
|
assertEquals("request",BufferUtil.toString(buffer));
|
||||||
|
|
||||||
|
assertTrue(c.client.isOpen());
|
||||||
|
assertFalse(c.client.isOutputShutdown());
|
||||||
|
assertTrue(c.server.isOpen());
|
||||||
|
assertFalse(c.server.isOutputShutdown());
|
||||||
|
|
||||||
|
c.client.close();
|
||||||
|
|
||||||
|
assertFalse(c.client.isOpen());
|
||||||
|
assertTrue(c.client.isOutputShutdown());
|
||||||
|
assertTrue(c.server.isOpen());
|
||||||
|
assertFalse(c.server.isOutputShutdown());
|
||||||
|
|
||||||
|
len = c.server.fill(buffer);
|
||||||
|
assertEquals(-1,len);
|
||||||
|
|
||||||
|
assertFalse(c.client.isOpen());
|
||||||
|
assertTrue(c.client.isOutputShutdown());
|
||||||
|
assertTrue(c.server.isOpen());
|
||||||
|
assertFalse(c.server.isOutputShutdown());
|
||||||
|
|
||||||
|
c.server.shutdownOutput();
|
||||||
|
|
||||||
|
assertFalse(c.client.isOpen());
|
||||||
|
assertTrue(c.client.isOutputShutdown());
|
||||||
|
assertFalse(c.server.isOpen());
|
||||||
|
assertTrue(c.server.isOutputShutdown());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue