Add more diagnostics to SocketConnectionAttemptTest to fix the flaky test / Cleanup

This commit is contained in:
Trustin Lee 2013-12-08 13:02:37 +09:00
parent 3812b897e6
commit dc9537e672

View File

@ -22,7 +22,6 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.util.internal.SystemPropertyUtil; import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Assume;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
@ -32,6 +31,7 @@ import java.net.Socket;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.junit.Assume.*;
public class SocketConnectionAttemptTest extends AbstractClientSocketTest { public class SocketConnectionAttemptTest extends AbstractClientSocketTest {
@ -48,8 +48,7 @@ public class SocketConnectionAttemptTest extends AbstractClientSocketTest {
} }
public void testConnectTimeout(Bootstrap cb) throws Throwable { public void testConnectTimeout(Bootstrap cb) throws Throwable {
TestHandler h = new TestHandler(); cb.handler(new TestHandler()).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000);
cb.handler(h).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000);
ChannelFuture future = cb.connect(BAD_HOST, 8080); ChannelFuture future = cb.connect(BAD_HOST, 8080);
try { try {
assertThat(future.await(3000), is(true)); assertThat(future.await(3000), is(true));
@ -62,12 +61,12 @@ public class SocketConnectionAttemptTest extends AbstractClientSocketTest {
public void testConnectCancellation() throws Throwable { public void testConnectCancellation() throws Throwable {
// Check if the test can be executed or should be skipped because of no network/internet connection // 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 // See https://github.com/netty/netty/issues/1474
boolean noRoute = false; boolean badHostTimedOut = true;
Socket socket = new Socket(); Socket socket = new Socket();
try { try {
socket.connect(new InetSocketAddress(BAD_HOST, 8080), 10); socket.connect(new InetSocketAddress(BAD_HOST, 8080), 10);
} catch (ConnectException e) { } catch (ConnectException e) {
noRoute = true; badHostTimedOut = false;
// is thrown for no route to host when using Socket connect // is thrown for no route to host when using Socket connect
} catch (Exception e) { } catch (Exception e) {
// ignore // ignore
@ -79,17 +78,23 @@ public class SocketConnectionAttemptTest extends AbstractClientSocketTest {
} }
} }
Assume.assumeFalse( assumeThat("The connection attempt to " + BAD_HOST + " does not time out.", badHostTimedOut, is(true));
"No route to host, so skip the test as it may be because of no network connection ", noRoute);
run(); run();
} }
public void testConnectCancellation(Bootstrap cb) throws Throwable { public void testConnectCancellation(Bootstrap cb) throws Throwable {
TestHandler h = new TestHandler(); cb.handler(new TestHandler()).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000);
cb.handler(h).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000);
ChannelFuture future = cb.connect(BAD_HOST, 8080); ChannelFuture future = cb.connect(BAD_HOST, 8080);
try { 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)) { if (future.cancel(true)) {
assertThat(future.channel().closeFuture().await(500), is(true)); assertThat(future.channel().closeFuture().await(500), is(true));
assertThat(future.isCancelled(), is(true)); assertThat(future.isCancelled(), is(true));
@ -104,7 +109,8 @@ public class SocketConnectionAttemptTest extends AbstractClientSocketTest {
private static class TestHandler extends ChannelInboundHandlerAdapter { private static class TestHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace(); InternalLoggerFactory.getInstance(
SocketConnectionAttemptTest.class).warn("Unexpected exception:", cause);
} }
} }
} }