Browse Source

HADOOP-1812. Let OS choose ports for IPC and RPC unit tests.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@571331 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 18 năm trước cách đây
mục cha
commit
78e1d10e96

+ 2 - 0
CHANGES.txt

@@ -142,6 +142,8 @@ Trunk (unreleased changes)
     port.  Also switch default address for umbilical connections to
     loopback.  (cutting)
 
+    HADOOP-1812. Let OS choose ports for IPC and RPC unit tests. (cutting)
+
 
 Release 0.14.1 - 2007-09-04
 

+ 11 - 9
src/test/org/apache/hadoop/ipc/TestIPC.java

@@ -42,15 +42,14 @@ public class TestIPC extends TestCase {
 
   private static final Random RANDOM = new Random();
 
-  private static final int PORT = 1234;
   private static final String ADDRESS = "0.0.0.0";
 
   private static class TestServer extends Server {
     private boolean sleep;
 
-    public TestServer(String bindAddress, int port, int handlerCount, boolean sleep) 
+    public TestServer(int handlerCount, boolean sleep) 
       throws IOException {
-      super(bindAddress, port, LongWritable.class, handlerCount, conf);
+      super(ADDRESS, 0, LongWritable.class, handlerCount, conf);
       this.setTimeout(1000);
       this.sleep = sleep;
     }
@@ -67,11 +66,13 @@ public class TestIPC extends TestCase {
 
   private static class SerialCaller extends Thread {
     private Client client;
+    private InetSocketAddress server;
     private int count;
     private boolean failed;
 
-    public SerialCaller(Client client, int count) {
+    public SerialCaller(Client client, InetSocketAddress server, int count) {
       this.client = client;
+      this.server = server;
       this.count = count;
       client.setTimeout(1000);
     }
@@ -81,7 +82,7 @@ public class TestIPC extends TestCase {
         try {
           LongWritable param = new LongWritable(RANDOM.nextLong());
           LongWritable value =
-            (LongWritable)client.call(param, new InetSocketAddress(PORT));
+            (LongWritable)client.call(param, server);
           if (!param.equals(value)) {
             LOG.fatal("Call failed!");
             failed = true;
@@ -138,7 +139,8 @@ public class TestIPC extends TestCase {
   public void testSerial(int handlerCount, boolean handlerSleep, 
                          int clientCount, int callerCount, int callCount)
     throws Exception {
-    Server server = new TestServer(ADDRESS, PORT, handlerCount, handlerSleep);
+    Server server = new TestServer(handlerCount, handlerSleep);
+    InetSocketAddress addr = server.getListenerAddress();
     server.start();
 
     Client[] clients = new Client[clientCount];
@@ -148,7 +150,7 @@ public class TestIPC extends TestCase {
     
     SerialCaller[] callers = new SerialCaller[callerCount];
     for (int i = 0; i < callerCount; i++) {
-      callers[i] = new SerialCaller(clients[i%clientCount], callCount);
+      callers[i] = new SerialCaller(clients[i%clientCount], addr, callCount);
       callers[i].start();
     }
     for (int i = 0; i < callerCount; i++) {
@@ -171,13 +173,13 @@ public class TestIPC extends TestCase {
     throws Exception {
     Server[] servers = new Server[serverCount];
     for (int i = 0; i < serverCount; i++) {
-      servers[i] = new TestServer(ADDRESS, PORT+i+1, handlerCount, handlerSleep);
+      servers[i] = new TestServer(handlerCount, handlerSleep);
       servers[i].start();
     }
 
     InetSocketAddress[] addresses = new InetSocketAddress[addressCount];
     for (int i = 0; i < addressCount; i++) {
-      addresses[i] = new InetSocketAddress(PORT+1+(i%serverCount));
+      addresses[i] = servers[i%serverCount].getListenerAddress();
     }
 
     Client[] clients = new Client[clientCount];

+ 2 - 3
src/test/org/apache/hadoop/ipc/TestRPC.java

@@ -36,7 +36,6 @@ import org.apache.hadoop.ipc.VersionedProtocol;
 
 /** Unit tests for RPC. */
 public class TestRPC extends TestCase {
-  private static final int PORT = 1234;
   private static final String ADDRESS = "0.0.0.0";
 
   public static final Log LOG =
@@ -99,10 +98,10 @@ public class TestRPC extends TestCase {
   }
 
   public void testCalls() throws Exception {
-    Server server = RPC.getServer(new TestImpl(), ADDRESS, PORT, conf);
+    Server server = RPC.getServer(new TestImpl(), ADDRESS, 0, conf);
     server.start();
 
-    InetSocketAddress addr = new InetSocketAddress(PORT);
+    InetSocketAddress addr = server.getListenerAddress();
     TestProtocol proxy =
       (TestProtocol)RPC.getProxy(TestProtocol.class, TestProtocol.versionID, addr, conf);