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 java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
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.util.DummyHandler;
import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@ -93,7 +93,7 @@ public abstract class AbstractSocketClientBootstrapTest {
bootstrap.setOption(
"remoteAddress",
new InetSocketAddress(
InetAddress.getLocalHost(),
TestOptions.getLocalHost(),
serverSocket.socket().getLocalPort()));
ChannelFuture future = bootstrap.connect();
@ -130,7 +130,7 @@ public abstract class AbstractSocketClientBootstrapTest {
bootstrap.setOption(
"remoteAddress",
new InetSocketAddress(
InetAddress.getLocalHost(),
TestOptions.getLocalHost(),
serverSocket.socket().getLocalPort()));
bootstrap.setOption("localAddress", new InetSocketAddress(0));
@ -162,7 +162,7 @@ public abstract class AbstractSocketClientBootstrapTest {
expect(pipelineFactory.getPipeline()).andThrow(new ChannelPipelineException());
replay(pipelineFactory);
bootstrap.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1));
bootstrap.connect(new InetSocketAddress(TestOptions.getLocalHost(), 1));
}
@Test(expected = IllegalStateException.class)

View File

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

View File

@ -25,7 +25,6 @@ package org.jboss.netty.channel.socket;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Random;
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.SimpleChannelHandler;
import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@ -97,7 +97,7 @@ public abstract class AbstractSocketEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0));
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());
Channel cc = ccf.getChannel();

View File

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

View File

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

View File

@ -25,7 +25,6 @@ package org.jboss.netty.handler.codec.frame;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Random;
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.SimpleChannelHandler;
import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@ -99,7 +99,7 @@ public abstract class AbstractSocketFixedLengthEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0));
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());
Channel cc = ccf.getChannel();

View File

@ -25,7 +25,6 @@ package org.jboss.netty.handler.codec.serialization;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Random;
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.SimpleChannelHandler;
import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@ -108,7 +108,7 @@ public abstract class AbstractSocketCompatibleObjectStreamEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0));
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());
Channel cc = ccf.getChannel();

View File

@ -25,7 +25,6 @@ package org.jboss.netty.handler.codec.serialization;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Random;
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.SimpleChannelHandler;
import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@ -108,7 +108,7 @@ public abstract class AbstractSocketObjectStreamEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0));
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());
Channel cc = ccf.getChannel();

View File

@ -25,7 +25,6 @@ package org.jboss.netty.handler.codec.string;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Random;
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.Delimiters;
import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@ -112,7 +112,7 @@ public abstract class AbstractSocketStringEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0));
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());
Channel cc = ccf.getChannel();

View File

@ -25,7 +25,6 @@ package org.jboss.netty.handler.ssl;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Random;
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.InternalLoggerFactory;
import org.jboss.netty.util.ExecutorUtil;
import org.jboss.netty.util.TestOptions;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@ -128,7 +128,7 @@ public abstract class AbstractSocketSslEchoTest {
Channel sc = sb.bind(new InetSocketAddress(0));
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();
if (!ccf.isSuccess()) {
logger.error("Connection attempt failed", ccf.getCause());

View File

@ -22,6 +22,9 @@
*/
package org.jboss.netty.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author The Netty Project (netty-dev@lists.jboss.org)
@ -30,7 +33,9 @@ package org.jboss.netty.util;
*/
@org.junit.Ignore
public final class TestOptions {
private static final boolean ENABLED;
private static final InetAddress LOCALHOST;
static {
String value = System.getProperty("exclude-timing-tests", "false").trim();
@ -42,12 +47,34 @@ public final class TestOptions {
if (!ENABLED) {
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() {
return ENABLED;
}
public static InetAddress getLocalHost() {
return LOCALHOST;
}
private TestOptions() {
// Unused
}