ayushtkn commented on a change in pull request #4028:
URL: https://github.com/apache/hadoop/pull/4028#discussion_r816927981



##########
File path: 
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/qjournal/TestMiniJournalCluster.java
##########
@@ -52,4 +59,82 @@ public void testStartStop() throws IOException {
       c.shutdown();
     }
   }
+
+  @Test
+  public void testStartStopWithPorts() throws Exception {
+    Configuration conf = new Configuration();
+
+    LambdaTestUtils.intercept(
+        IllegalArgumentException.class,
+        "Num of http ports (1) should match num of JournalNodes (3)",
+        "MiniJournalCluster port validation failed",
+        () -> {
+          new MiniJournalCluster.Builder(conf).setHttpPorts(8481).build();
+        });
+
+    LambdaTestUtils.intercept(
+        IllegalArgumentException.class,
+        "Num of rpc ports (2) should match num of JournalNodes (3)",
+        "MiniJournalCluster port validation failed",
+        () -> {
+          new MiniJournalCluster.Builder(conf).setRpcPorts(8481, 8482).build();
+        });
+
+    LambdaTestUtils.intercept(
+        IllegalArgumentException.class,
+        "Num of rpc ports (1) should match num of JournalNodes (3)",
+        "MiniJournalCluster port validation failed",
+        () -> {
+          new MiniJournalCluster.Builder(conf).setHttpPorts(800, 9000, 
10000).setRpcPorts(8481)
+              .build();
+        });
+
+    LambdaTestUtils.intercept(
+        IllegalArgumentException.class,
+        "Num of http ports (4) should match num of JournalNodes (3)",
+        "MiniJournalCluster port validation failed",
+        () -> {
+          new MiniJournalCluster.Builder(conf).setHttpPorts(800, 9000, 1000, 
2000)
+              .setRpcPorts(8481, 8482, 8483).build();
+        });
+
+    final int[] httpPorts = new int[] { NetUtils.getFreeSocketPort(), 
NetUtils.getFreeSocketPort(),
+        NetUtils.getFreeSocketPort() };
+    final int[] rpcPorts = new int[] { NetUtils.getFreeSocketPort(), 
NetUtils.getFreeSocketPort(),
+        NetUtils.getFreeSocketPort() };

Review comment:
       This won't work in all cases, the same port can be returned, say you are 
calling ``NetUtils.getFreeSocketPort()`` 6 times, it is possible it won't 
return 6 different ports, it can have dupes. 
   
   Can have a util method added in NetUtils, Something like this(Just for idea, 
can improve/fix)
   
   ```
     /**
      * Return a set of free ports. There is no guarantee it will remain free, 
so
      *    * it should be used immediately.
      * @param numPorts number of free ports required.
      * @return a set of unique ports.
      * @throws IOException in case unable to find the required number of free 
ports.
      */
     public static Set<Integer> getFreeSocketPorts(int numPorts)
         throws IOException {
       Set<Integer> ports = new HashSet<>();
       for (int numAttempt = 0;
            ports.size() != numPorts && numAttempt < 10 * numPorts; 
numAttempt++) {
         int port = getFreeSocketPort();
         if (port > 0) {
           ports.add(port);
         }
       }
       if (ports.size() != numPorts) {
         throw new IOException("Couldn't find " + numPorts + " free ports");
       }
       return ports;
     }
   ```
   And we can consume the output of this method.
   If possible, Please extend a test in ``TestNetUtils`` as well. A simple test 
should do:
   ```
   
     @Test 
     public void testGetFreePorts() throws IOException {
       assertEquals(6, NetUtils.getFreeSocketPorts(6).size());
     }
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to