Reduced the number of InetAddress.getLocalHost() calls in test cases

This commit is contained in:
Trustin Lee 2009-03-12 00:14:17 +00:00
parent 0edc9abb0f
commit ebdfec8ec0
11 changed files with 48 additions and 23 deletions

View File

@ -26,7 +26,6 @@ import static org.easymock.EasyMock.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel; import java.nio.channels.ServerSocketChannel;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -39,6 +38,7 @@ import org.jboss.netty.channel.ChannelPipelineException;
import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.util.DummyHandler; import org.jboss.netty.util.DummyHandler;
import org.jboss.netty.util.ExecutorUtil; import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -93,7 +93,7 @@ public abstract class AbstractSocketClientBootstrapTest {
bootstrap.setOption( bootstrap.setOption(
"remoteAddress", "remoteAddress",
new InetSocketAddress( new InetSocketAddress(
InetAddress.getLocalHost(), TestOptions.getLocalHost(),
serverSocket.socket().getLocalPort())); serverSocket.socket().getLocalPort()));
ChannelFuture future = bootstrap.connect(); ChannelFuture future = bootstrap.connect();
@ -130,7 +130,7 @@ public abstract class AbstractSocketClientBootstrapTest {
bootstrap.setOption( bootstrap.setOption(
"remoteAddress", "remoteAddress",
new InetSocketAddress( new InetSocketAddress(
InetAddress.getLocalHost(), TestOptions.getLocalHost(),
serverSocket.socket().getLocalPort())); serverSocket.socket().getLocalPort()));
bootstrap.setOption("localAddress", new InetSocketAddress(0)); bootstrap.setOption("localAddress", new InetSocketAddress(0));
@ -162,7 +162,7 @@ public abstract class AbstractSocketClientBootstrapTest {
expect(pipelineFactory.getPipeline()).andThrow(new ChannelPipelineException()); expect(pipelineFactory.getPipeline()).andThrow(new ChannelPipelineException());
replay(pipelineFactory); replay(pipelineFactory);
bootstrap.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1)); bootstrap.connect(new InetSocketAddress(TestOptions.getLocalHost(), 1));
} }
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)

View File

@ -26,7 +26,6 @@ import static org.easymock.EasyMock.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -46,6 +45,7 @@ import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.SocketChannelConfig; import org.jboss.netty.channel.socket.SocketChannelConfig;
import org.jboss.netty.util.DummyHandler; import org.jboss.netty.util.DummyHandler;
import org.jboss.netty.util.ExecutorUtil; import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -132,7 +132,7 @@ public abstract class AbstractSocketServerBootstrapTest {
Socket socket = null; Socket socket = null;
try { try {
socket = new Socket( socket = new Socket(
InetAddress.getLocalHost(), TestOptions.getLocalHost(),
((InetSocketAddress) channel.getLocalAddress()).getPort()); ((InetSocketAddress) channel.getLocalAddress()).getPort());
// Wait until the connection is open in the server side. // Wait until the connection is open in the server side.
@ -188,7 +188,7 @@ public abstract class AbstractSocketServerBootstrapTest {
expect(pipelineFactory.getPipeline()).andThrow(new ChannelPipelineException()); expect(pipelineFactory.getPipeline()).andThrow(new ChannelPipelineException());
replay(pipelineFactory); replay(pipelineFactory);
bootstrap.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1)); bootstrap.connect(new InetSocketAddress(TestOptions.getLocalHost(), 1));
} }
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)

View File

@ -25,7 +25,6 @@ package org.jboss.netty.channel.socket;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Random; import java.util.Random;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -47,6 +46,7 @@ import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.util.ExecutorUtil; import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -97,7 +97,7 @@ public abstract class AbstractSocketEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0)); Channel sc = sb.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress) sc.getLocalAddress()).getPort(); int port = ((InetSocketAddress) sc.getLocalAddress()).getPort();
ChannelFuture ccf = cb.connect(new InetSocketAddress(InetAddress.getLocalHost(), port)); ChannelFuture ccf = cb.connect(new InetSocketAddress(TestOptions.getLocalHost(), port));
assertTrue(ccf.awaitUninterruptibly().isSuccess()); assertTrue(ccf.awaitUninterruptibly().isSuccess());
Channel cc = ccf.getChannel(); Channel cc = ccf.getChannel();

View File

@ -25,7 +25,6 @@ package org.jboss.netty.channel.socket;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel; import java.nio.channels.ServerSocketChannel;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -69,7 +68,7 @@ public class NioClientSocketShutdownTimeTest {
serverSocket.configureBlocking(false); serverSocket.configureBlocking(false);
ChannelFuture f = b.connect(new InetSocketAddress( ChannelFuture f = b.connect(new InetSocketAddress(
InetAddress.getLocalHost(), TestOptions.getLocalHost(),
serverSocket.socket().getLocalPort())); serverSocket.socket().getLocalPort()));
serverSocket.accept(); serverSocket.accept();

View File

@ -25,7 +25,6 @@ package org.jboss.netty.channel.socket;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -75,7 +74,7 @@ public class NioServerSocketShutdownTimeTest {
Socket socket = null; Socket socket = null;
try { try {
socket = new Socket( socket = new Socket(
InetAddress.getLocalHost(), TestOptions.getLocalHost(),
((InetSocketAddress) channel.getLocalAddress()).getPort()); ((InetSocketAddress) channel.getLocalAddress()).getPort());
while (!handler.connected) { while (!handler.connected) {

View File

@ -25,7 +25,6 @@ package org.jboss.netty.handler.codec.frame;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Random; import java.util.Random;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -47,6 +46,7 @@ import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.util.ExecutorUtil; import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -99,7 +99,7 @@ public abstract class AbstractSocketFixedLengthEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0)); Channel sc = sb.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress) sc.getLocalAddress()).getPort(); int port = ((InetSocketAddress) sc.getLocalAddress()).getPort();
ChannelFuture ccf = cb.connect(new InetSocketAddress(InetAddress.getLocalHost(), port)); ChannelFuture ccf = cb.connect(new InetSocketAddress(TestOptions.getLocalHost(), port));
assertTrue(ccf.awaitUninterruptibly().isSuccess()); assertTrue(ccf.awaitUninterruptibly().isSuccess());
Channel cc = ccf.getChannel(); Channel cc = ccf.getChannel();

View File

@ -25,7 +25,6 @@ package org.jboss.netty.handler.codec.serialization;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Random; import java.util.Random;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -45,6 +44,7 @@ import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.util.ExecutorUtil; import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -108,7 +108,7 @@ public abstract class AbstractSocketCompatibleObjectStreamEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0)); Channel sc = sb.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress) sc.getLocalAddress()).getPort(); int port = ((InetSocketAddress) sc.getLocalAddress()).getPort();
ChannelFuture ccf = cb.connect(new InetSocketAddress(InetAddress.getLocalHost(), port)); ChannelFuture ccf = cb.connect(new InetSocketAddress(TestOptions.getLocalHost(), port));
assertTrue(ccf.awaitUninterruptibly().isSuccess()); assertTrue(ccf.awaitUninterruptibly().isSuccess());
Channel cc = ccf.getChannel(); Channel cc = ccf.getChannel();

View File

@ -25,7 +25,6 @@ package org.jboss.netty.handler.codec.serialization;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Random; import java.util.Random;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -45,6 +44,7 @@ import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.util.ExecutorUtil; import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -108,7 +108,7 @@ public abstract class AbstractSocketObjectStreamEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0)); Channel sc = sb.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress) sc.getLocalAddress()).getPort(); int port = ((InetSocketAddress) sc.getLocalAddress()).getPort();
ChannelFuture ccf = cb.connect(new InetSocketAddress(InetAddress.getLocalHost(), port)); ChannelFuture ccf = cb.connect(new InetSocketAddress(TestOptions.getLocalHost(), port));
assertTrue(ccf.awaitUninterruptibly().isSuccess()); assertTrue(ccf.awaitUninterruptibly().isSuccess());
Channel cc = ccf.getChannel(); Channel cc = ccf.getChannel();

View File

@ -25,7 +25,6 @@ package org.jboss.netty.handler.codec.string;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Random; import java.util.Random;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -47,6 +46,7 @@ import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder; import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters; import org.jboss.netty.handler.codec.frame.Delimiters;
import org.jboss.netty.util.ExecutorUtil; import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -112,7 +112,7 @@ public abstract class AbstractSocketStringEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0)); Channel sc = sb.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress) sc.getLocalAddress()).getPort(); int port = ((InetSocketAddress) sc.getLocalAddress()).getPort();
ChannelFuture ccf = cb.connect(new InetSocketAddress(InetAddress.getLocalHost(), port)); ChannelFuture ccf = cb.connect(new InetSocketAddress(TestOptions.getLocalHost(), port));
assertTrue(ccf.awaitUninterruptibly().isSuccess()); assertTrue(ccf.awaitUninterruptibly().isSuccess());
Channel cc = ccf.getChannel(); Channel cc = ccf.getChannel();

View File

@ -25,7 +25,6 @@ package org.jboss.netty.handler.ssl;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Random; import java.util.Random;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -54,6 +53,7 @@ import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory; import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.util.ExecutorUtil; import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -128,7 +128,7 @@ public abstract class AbstractSocketSslEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0)); Channel sc = sb.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress) sc.getLocalAddress()).getPort(); int port = ((InetSocketAddress) sc.getLocalAddress()).getPort();
ChannelFuture ccf = cb.connect(new InetSocketAddress(InetAddress.getLocalHost(), port)); ChannelFuture ccf = cb.connect(new InetSocketAddress(TestOptions.getLocalHost(), port));
ccf.awaitUninterruptibly(); ccf.awaitUninterruptibly();
if (!ccf.isSuccess()) { if (!ccf.isSuccess()) {
logger.error("Connection attempt failed", ccf.getCause()); logger.error("Connection attempt failed", ccf.getCause());

View File

@ -22,6 +22,9 @@
*/ */
package org.jboss.netty.util; package org.jboss.netty.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
/** /**
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
@ -30,7 +33,9 @@ package org.jboss.netty.util;
*/ */
@org.junit.Ignore @org.junit.Ignore
public final class TestOptions { public final class TestOptions {
private static final boolean ENABLED; private static final boolean ENABLED;
private static final InetAddress LOCALHOST;
static { static {
String value = System.getProperty("exclude-timing-tests", "false").trim(); String value = System.getProperty("exclude-timing-tests", "false").trim();
@ -42,12 +47,34 @@ public final class TestOptions {
if (!ENABLED) { if (!ENABLED) {
System.err.println("Timing tests will be disabled as requested."); System.err.println("Timing tests will be disabled as requested.");
} }
InetAddress localhost = null;
try {
localhost = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
try {
localhost = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
} catch (UnknownHostException e1) {
try {
localhost = InetAddress.getByAddress(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 });
} catch (UnknownHostException e2) {
System.err.println("Failed to get the localhost.");
e2.printStackTrace();
}
}
}
LOCALHOST = localhost;
} }
public static boolean isTimingTestEnabled() { public static boolean isTimingTestEnabled() {
return ENABLED; return ENABLED;
} }
public static InetAddress getLocalHost() {
return LOCALHOST;
}
private TestOptions() { private TestOptions() {
// Unused // Unused
} }