From 26979f89011d007cf1a7ddab1296b4524b1a61f1 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Sun, 8 Dec 2013 13:02:37 +0900 Subject: [PATCH] Add more diagnostics to SocketConnectionAttemptTest to fix the flaky test / Cleanup --- .../socket/SocketConnectionAttemptTest.java | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/testsuite/src/test/java/io/netty/testsuite/transport/socket/SocketConnectionAttemptTest.java b/testsuite/src/test/java/io/netty/testsuite/transport/socket/SocketConnectionAttemptTest.java index 68df394102..8ff49c4f7c 100644 --- a/testsuite/src/test/java/io/netty/testsuite/transport/socket/SocketConnectionAttemptTest.java +++ b/testsuite/src/test/java/io/netty/testsuite/transport/socket/SocketConnectionAttemptTest.java @@ -18,11 +18,9 @@ package io.netty.testsuite.transport.socket; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerAdapter; -import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOption; import io.netty.util.internal.SystemPropertyUtil; import io.netty.util.internal.logging.InternalLoggerFactory; -import org.junit.Assume; import org.junit.Test; import java.io.IOException; @@ -32,6 +30,7 @@ import java.net.Socket; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import static org.junit.Assume.*; public class SocketConnectionAttemptTest extends AbstractClientSocketTest { @@ -48,8 +47,7 @@ public class SocketConnectionAttemptTest extends AbstractClientSocketTest { } public void testConnectTimeout(Bootstrap cb) throws Throwable { - TestHandler h = new TestHandler(); - cb.handler(h).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000); + cb.handler(new ChannelHandlerAdapter()).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000); ChannelFuture future = cb.connect(BAD_HOST, 8080); try { assertThat(future.await(3000), is(true)); @@ -62,12 +60,12 @@ public class SocketConnectionAttemptTest extends AbstractClientSocketTest { public void testConnectCancellation() throws Throwable { // Check if the test can be executed or should be skipped because of no network/internet connection // See https://github.com/netty/netty/issues/1474 - boolean noRoute = false; + boolean badHostTimedOut = true; Socket socket = new Socket(); try { socket.connect(new InetSocketAddress(BAD_HOST, 8080), 10); } catch (ConnectException e) { - noRoute = true; + badHostTimedOut = false; // is thrown for no route to host when using Socket connect } catch (Exception e) { // ignore @@ -79,17 +77,23 @@ public class SocketConnectionAttemptTest extends AbstractClientSocketTest { } } - Assume.assumeFalse( - "No route to host, so skip the test as it may be because of no network connection ", noRoute); + assumeThat("The connection attempt to " + BAD_HOST + " does not time out.", badHostTimedOut, is(true)); + run(); } public void testConnectCancellation(Bootstrap cb) throws Throwable { - TestHandler h = new TestHandler(); - cb.handler(h).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); + cb.handler(new ChannelHandlerAdapter()).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000); ChannelFuture future = cb.connect(BAD_HOST, 8080); try { - assertThat(future.await(1000), is(false)); + if (future.await(1000)) { + if (future.isSuccess()) { + fail("A connection attempt to " + BAD_HOST + " must not succeed."); + } else { + throw future.cause(); + } + } + if (future.cancel(true)) { assertThat(future.channel().closeFuture().await(500), is(true)); assertThat(future.isCancelled(), is(true)); @@ -100,11 +104,4 @@ public class SocketConnectionAttemptTest extends AbstractClientSocketTest { future.channel().close(); } } - - private static class TestHandler extends ChannelHandlerAdapter { - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - cause.printStackTrace(); - } - } }