diff options
Diffstat (limited to 'jetty-io/src/test/java/org/eclipse/jetty/io/nio/SelectChannelEndPointTest.java')
-rw-r--r-- | jetty-io/src/test/java/org/eclipse/jetty/io/nio/SelectChannelEndPointTest.java | 84 |
1 files changed, 70 insertions, 14 deletions
diff --git a/jetty-io/src/test/java/org/eclipse/jetty/io/nio/SelectChannelEndPointTest.java b/jetty-io/src/test/java/org/eclipse/jetty/io/nio/SelectChannelEndPointTest.java index d35d62d843..5190935ea3 100644 --- a/jetty-io/src/test/java/org/eclipse/jetty/io/nio/SelectChannelEndPointTest.java +++ b/jetty-io/src/test/java/org/eclipse/jetty/io/nio/SelectChannelEndPointTest.java @@ -1,5 +1,10 @@ package org.eclipse.jetty.io.nio; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; @@ -22,12 +27,9 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import static org.hamcrest.Matchers.greaterThanOrEqualTo; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - public class SelectChannelEndPointTest { + protected SelectChannelEndPoint _lastEndp; protected ServerSocketChannel _connector; protected QueuedThreadPool _threadPool = new QueuedThreadPool(); protected SelectorManager _manager = new SelectorManager() @@ -64,6 +66,7 @@ public class SelectChannelEndPointTest { SelectChannelEndPoint endp = new SelectChannelEndPoint(channel,selectSet,key,2000); endp.setConnection(selectSet.getManager().newConnection(channel,endp, key.attachment())); + _lastEndp=endp; return endp; } }; @@ -116,10 +119,7 @@ public class SelectChannelEndPointTest progress=false; _in.compact(); if (_in.space()>0 && _endp.fill(_in)>0) - { progress=true; - ((AsyncEndPoint)_endp).cancelIdle(); - } while (_blockAt>0 && _in.length()>0 && _in.length()<_blockAt) { @@ -210,7 +210,7 @@ public class SelectChannelEndPointTest assertEquals(c,(char)b); } client.close(); - + int i=0; while (server.isOpen()) { @@ -325,6 +325,54 @@ public class SelectChannelEndPointTest assertEquals(c,(char)b); } } + + @Test + public void testIdle() throws Exception + { + Socket client = newClient(); + + client.setSoTimeout(3000); + + SocketChannel server = _connector.accept(); + server.configureBlocking(false); + + _manager.register(server); + + // Write client to server + client.getOutputStream().write("HelloWorld".getBytes("UTF-8")); + + // Verify echo server to client + for (char c : "HelloWorld".toCharArray()) + { + int b = client.getInputStream().read(); + assertTrue(b>0); + assertEquals(c,(char)b); + } + + // Set Max idle + _lastEndp.setMaxIdleTime(500); + + // read until idle shutdown received + long start=System.currentTimeMillis(); + int b=client.getInputStream().read(); + assertEquals(-1,b); + long idle=System.currentTimeMillis()-start; + assertTrue(idle>400); + assertTrue(idle<2000); + + // But endpoint is still open. + assertTrue(_lastEndp.isOpen()); + + + // Wait for another idle callback + Thread.sleep(1000); + // endpoint is closed. + + assertFalse(_lastEndp.isOpen()); + + } + + @Test public void testStress() throws Exception @@ -338,8 +386,12 @@ public class SelectChannelEndPointTest _manager.register(server); int writes = 100000; + final byte[] bytes="HelloWorld".getBytes("UTF-8"); final CountDownLatch latch = new CountDownLatch(writes); final InputStream in = new BufferedInputStream(client.getInputStream()); + final long start = System.currentTimeMillis(); + client.getOutputStream().write(bytes); + client.getOutputStream().flush(); new Thread() { @@ -350,29 +402,33 @@ public class SelectChannelEndPointTest while (latch.getCount()>0) { // Verify echo server to client - for (char c : "HelloWorld".toCharArray()) + for (byte b0 : bytes) { int b = in.read(); assertTrue(b>0); - assertEquals(c,(char)b); + assertEquals(0xff&b0,b); } latch.countDown(); } } - catch(Exception e) + catch(Throwable e) { + System.err.println("latch="+latch.getCount()); + System.err.println("time="+(System.currentTimeMillis()-start)); e.printStackTrace(); } } }.start(); - byte[] bytes="HelloWorld".getBytes("UTF-8"); // Write client to server - for (int i=0;i<writes;i++) + for (int i=1;i<writes;i++) + { client.getOutputStream().write(bytes); + Thread.yield(); + } + client.getOutputStream().flush(); assertTrue(latch.await(100,TimeUnit.SECONDS)); - } } |