From ab298e511eda62667012ef11dbcb71da594b4b28 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 23 Jul 2012 17:11:59 +1000 Subject: [PATCH] jetty-9 unit test cleanups --- .../org/eclipse/jetty/http/HttpParser.java | 14 +++++- .../jetty/http/HttpGeneratorServerTest.java | 1 - .../org/eclipse/jetty/io/ChannelEndPoint.java | 35 +++++++++------ .../org/eclipse/jetty/server/HttpChannel.java | 17 +++++--- .../jetty/server/HttpConnectionTest.java | 43 ++++++------------- .../jetty/server/ssl/SSLCloseTest.java | 9 ++-- .../SSLSelectChannelConnectorLoadTest.java | 14 +++--- .../java/org/eclipse/jetty/util/MultiMap.java | 6 +-- 8 files changed, 73 insertions(+), 66 deletions(-) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java index 8dc0a08369d..18466b03426 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import org.eclipse.jetty.http.HttpTokens.EndOfContent; +import org.eclipse.jetty.io.EofException; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Utf8StringBuilder; import org.eclipse.jetty.util.log.Log; @@ -898,7 +899,7 @@ public class HttpParser buffer.position(buffer.position()-1); String chars = BufferUtil.toDetailString(buffer); BufferUtil.clear(buffer); - throw new IOException(this+" Extra data after oshut: "+chars); + throw new EofException(this+" Extra data after oshut: "+chars); } } return false; @@ -1075,16 +1076,25 @@ public class HttpParser return false; } + catch(EofException e) + { + LOG.debug(e); + BufferUtil.clear(buffer); + return false; + } catch(Exception e) { + BufferUtil.clear(buffer); if (isClosed()) { LOG.debug(e); throw new IllegalStateException(e); } - LOG.warn(e); + LOG.warn("badMessage: "+e.toString()+" for "+_handler); + LOG.debug(e); _handler.badMessage(400, e.toString()); + BufferUtil.clear(buffer); return true; } } diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorServerTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorServerTest.java index 25827581835..cd074b79831 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorServerTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorServerTest.java @@ -413,7 +413,6 @@ public class HttpGeneratorServerTest result=gen.generate(info,header,null,null,null,null); assertEquals(HttpGenerator.Result.FLUSH,result); String head = BufferUtil.toString(header); - System.out.println(head); BufferUtil.clear(header); result=gen.generate(info,null,null,null,null,null); diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ChannelEndPoint.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ChannelEndPoint.java index b5ac8d46a21..94a4c6f2db0 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ChannelEndPoint.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ChannelEndPoint.java @@ -13,13 +13,17 @@ package org.eclipse.jetty.io; +import java.io.EOFException; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; +import java.net.SocketException; import java.nio.ByteBuffer; import java.nio.channels.ByteChannel; +import java.nio.channels.ClosedChannelException; import java.nio.channels.GatheringByteChannel; import java.nio.channels.SocketChannel; +import java.util.concurrent.BrokenBarrierException; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.log.Log; @@ -146,24 +150,31 @@ public class ChannelEndPoint extends AbstractEndPoint public int flush(ByteBuffer... buffers) throws IOException { int flushed=0; - if (buffers.length==1) - flushed=_channel.write(buffers[0]); - else if (buffers.length>1 && _channel instanceof GatheringByteChannel) - flushed= (int)((GatheringByteChannel)_channel).write(buffers,0,buffers.length); - else + try { - for (ByteBuffer b : buffers) + if (buffers.length==1) + flushed=_channel.write(buffers[0]); + else if (buffers.length>1 && _channel instanceof GatheringByteChannel) + flushed= (int)((GatheringByteChannel)_channel).write(buffers,0,buffers.length); + else { - if (b.hasRemaining()) + for (ByteBuffer b : buffers) { - int l=_channel.write(b); - if (l>0) - flushed+=l; - else - break; + if (b.hasRemaining()) + { + int l=_channel.write(b); + if (l>0) + flushed+=l; + else + break; + } } } } + catch (ClosedChannelException | EOFException | SocketException e) + { + throw new EofException(e); + } if (flushed>0) notIdle(); return flushed; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java index 9a58cd891dd..4df499fe9c4 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java @@ -53,8 +53,7 @@ import org.eclipse.jetty.util.resource.Resource; */ public abstract class HttpChannel { - private static final Logger LOG = Log.getLogger(HttpChannel.class); - + static final Logger LOG = Log.getLogger(HttpChannel.class); private static final ThreadLocal __currentChannel = new ThreadLocal(); @@ -355,8 +354,7 @@ public abstract class HttpChannel } catch (ServletException e) { - LOG.warn(String.valueOf(_uri),e.toString()); - LOG.debug(String.valueOf(_uri),e); + LOG.warn(String.valueOf(_uri),e); _state.error(e); _request.setHandled(true); commitError(500, null, e.toString()); @@ -406,7 +404,10 @@ public abstract class HttpChannel // Complete reading the request _in.consumeAll(); - + } + catch(EofException e) + { + LOG.debug(e); } catch(Exception e) { @@ -703,6 +704,12 @@ public abstract class HttpChannel } return _response.commit(); } + + @Override + public String toString() + { + return "CEH:"+HttpChannel.this.getConnection().toString(); + } } diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpConnectionTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpConnectionTest.java index d83bc604342..0e2229f237d 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpConnectionTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpConnectionTest.java @@ -157,43 +157,23 @@ public class HttpConnectionTest @Test public void testBad() throws Exception { - try - { - ((StdErrLog)Log.getLogger(HttpParser.class)).setHideStacks(true); + HttpParser.LOG.info("badMessage: 3 bad messages expected ..."); + String response; - String response; - - response=connector.getResponses("GET http://localhost:WRONG/ HTTP/1.1\n"+ - "Host: localhost\n"+ + response=connector.getResponses("GET http://localhost:EXPECTED_NUMBER_FORMAT_EXCEPTION/ HTTP/1.1\n"+ + "Host: localhost\n"+ "\015\012"); - checkContains(response,0,"HTTP/1.1 400"); + checkContains(response,0,"HTTP/1.1 400"); - response=connector.getResponses("GET /bad/encoding%1 HTTP/1.1\n"+ - "Host: localhost\n"+ + response=connector.getResponses("GET /bad/encoding%1 HTTP/1.1\n"+ + "Host: localhost\n"+ "\015\012"); - checkContains(response,0,"HTTP/1.1 400"); + checkContains(response,0,"HTTP/1.1 400"); - /* - response=connector.getResponses("GET /foo/bar%c0%00 HTTP/1.1\n"+ - "Host: localhost\n"+ + response=connector.getResponses("GET % HTTP/1.1\n"+ + "Host: localhost\n"+ "\015\012"); - checkContains(response,0,"HTTP/1.1 400"); - - response=connector.getResponses("GET /bad/utf8%c1 HTTP/1.1\n"+ - "Host: localhost\n"+ - "\015\012"); - checkContains(response,0,"HTTP/1.1 400"); - */ - - response=connector.getResponses("GET % HTTP/1.1\n"+ - "Host: localhost\n"+ - "\015\012"); - checkContains(response,0,"HTTP/1.1 400"); - } - finally - { - ((StdErrLog)Log.getLogger(HttpParser.class)).setHideStacks(false); - } + checkContains(response,0,"HTTP/1.1 400"); } @Test @@ -340,6 +320,7 @@ public class HttpConnectionTest try { + HttpChannel.LOG.info("EXPECTING: java.lang.IllegalStateException..."); ((StdErrLog)Log.getLogger(HttpChannel.class)).setHideStacks(true); response=connector.getResponses(requests); offset = checkContains(response,offset,"HTTP/1.1 500"); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLCloseTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLCloseTest.java index 3be494987b2..37ffd0d6bef 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLCloseTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLCloseTest.java @@ -110,16 +110,17 @@ public class SSLCloseTest extends TestCase String line; while ((line=in.readLine())!=null) { - System.err.println(line); + // System.err.println(line); if (line.trim().length()==0) break; } Thread.sleep(2000); - System.err.println(__endp); + // System.err.println(__endp); while ((line=in.readLine())!=null) - System.err.println(line); + //System.err.println(line); + Thread.yield(); } @@ -147,7 +148,7 @@ public class SSLCloseTest extends TestCase for (int i=0;i<2;i++) { - System.err.println("Write "+i+" "+bytes.length); + // System.err.println("Write "+i+" "+bytes.length); out.write(bytes); } } diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLSelectChannelConnectorLoadTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLSelectChannelConnectorLoadTest.java index 4ba49fae262..131e7c04cc7 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLSelectChannelConnectorLoadTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ssl/SSLSelectChannelConnectorLoadTest.java @@ -84,7 +84,6 @@ public class SSLSelectChannelConnectorLoadTest } @Test - @Ignore public void testLongLivedConnections() throws Exception { Worker.totalIterations.set(0); @@ -110,13 +109,13 @@ public class SSLSelectChannelConnectorLoadTest boolean done = true; for (Future task : tasks) done &= task.isDone(); - System.err.print("\rIterations: " + Worker.totalIterations.get() + "/" + clients * iterations); + //System.err.print("\rIterations: " + Worker.totalIterations.get() + "/" + clients * iterations); if (done) break; } long end = System.currentTimeMillis(); - System.err.println(); - System.err.println("Elapsed time: " + TimeUnit.MILLISECONDS.toSeconds(end - start) + "s"); + //System.err.println(); + //System.err.println("Elapsed time: " + TimeUnit.MILLISECONDS.toSeconds(end - start) + "s"); for (Worker worker : workers) worker.close(); @@ -132,7 +131,6 @@ public class SSLSelectChannelConnectorLoadTest } @Test - @Ignore public void testShortLivedConnections() throws Exception { Worker.totalIterations.set(0); @@ -157,13 +155,13 @@ public class SSLSelectChannelConnectorLoadTest boolean done = true; for (Future task : tasks) done &= task.isDone(); - System.err.print("\rIterations: " + Worker.totalIterations.get() + "/" + clients * iterations); + // System.err.print("\rIterations: " + Worker.totalIterations.get() + "/" + clients * iterations); if (done) break; } long end = System.currentTimeMillis(); - System.err.println(); - System.err.println("Elapsed time: " + TimeUnit.MILLISECONDS.toSeconds(end - start) + "s"); + // System.err.println(); + // System.err.println("Elapsed time: " + TimeUnit.MILLISECONDS.toSeconds(end - start) + "s"); threadPool.shutdown(); diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiMap.java b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiMap.java index b6d19dfdb6e..471fc06929f 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiMap.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiMap.java @@ -50,7 +50,7 @@ public class MultiMap implements Map * @param name The entry key. * @return Unmodifieable List of values. */ - public List getValues(Object name) + public List getValues(String name) { return LazyList.getList(_map.get(name),true); } @@ -63,7 +63,7 @@ public class MultiMap implements Map * @param i Index of element to get. * @return Unmodifieable List of values. */ - public Object getValue(Object name,int i) + public Object getValue(String name,int i) { Object l=_map.get(name); if (i==0 && LazyList.size(l)==0) @@ -80,7 +80,7 @@ public class MultiMap implements Map * @param name The entry key. * @return String value. */ - public String getString(Object name) + public String getString(String name) { Object l=_map.get(name); switch(LazyList.size(l))