Repository: ignite
Updated Branches:
  refs/heads/master 6c3a486f0 -> 525a0636a


IGNITE-9589 Fixed flaky GridTcpCommunicationSpiConfigSelfTest (use available 
port for the run) - Fixes #4751.

Signed-off-by: Alexey Goncharuk <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/525a0636
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/525a0636
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/525a0636

Branch: refs/heads/master
Commit: 525a0636a10460c6db373ffb4badeff31a42471b
Parents: 6c3a486
Author: NSAmelchev <[email protected]>
Authored: Fri Sep 21 19:09:18 2018 +0300
Committer: Alexey Goncharuk <[email protected]>
Committed: Fri Sep 21 19:09:18 2018 +0300

----------------------------------------------------------------------
 .../GridTcpCommunicationSpiConfigSelfTest.java  | 26 +++++++++++---------
 .../ignite/testframework/GridTestUtils.java     | 18 ++++++++++++++
 2 files changed, 33 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/525a0636/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiConfigSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiConfigSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiConfigSelfTest.java
index 100811b..7bea716 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiConfigSelfTest.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridTcpCommunicationSpiConfigSelfTest.java
@@ -21,6 +21,8 @@ import org.apache.ignite.configuration.IgniteConfiguration;
 import org.apache.ignite.testframework.junits.spi.GridSpiAbstractConfigTest;
 import org.apache.ignite.testframework.junits.spi.GridSpiTest;
 
+import static org.apache.ignite.testframework.GridTestUtils.getFreeCommPort;
+
 /**
  * TCP communication SPI config test.
  */
@@ -54,18 +56,20 @@ public class GridTcpCommunicationSpiConfigSelfTest extends 
GridSpiAbstractConfig
      * @throws Exception If failed.
      */
     public void testLocalPortRange() throws Exception {
-        try {
-            IgniteConfiguration cfg = getConfiguration();
+        IgniteConfiguration cfg = getConfiguration();
+
+        TcpCommunicationSpi commSpi = new TcpCommunicationSpi();
 
-            TcpCommunicationSpi spi = new TcpCommunicationSpi();
+        commSpi.setLocalPortRange(0);
+        commSpi.setLocalPort(getFreeCommPort());
 
-            spi.setLocalPortRange(0);
-            cfg.setCommunicationSpi(spi);
+        cfg.setCommunicationSpi(commSpi);
+
+        startGrid(cfg);
+    }
 
-            startGrid(cfg.getIgniteInstanceName(), cfg);
-        }
-        finally {
-            stopAllGrids();
-        }
+    /** {@inheritDoc} */
+    @Override protected void afterTestsStopped() {
+        stopAllGrids();
     }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/525a0636/modules/core/src/test/java/org/apache/ignite/testframework/GridTestUtils.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/testframework/GridTestUtils.java 
b/modules/core/src/test/java/org/apache/ignite/testframework/GridTestUtils.java
index 77f5324..e4ea4a9 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/testframework/GridTestUtils.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/testframework/GridTestUtils.java
@@ -30,6 +30,7 @@ import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.net.InetAddress;
 import java.net.MulticastSocket;
+import java.net.ServerSocket;
 import java.nio.file.attribute.PosixFilePermission;
 import java.security.GeneralSecurityException;
 import java.security.KeyStore;
@@ -690,6 +691,23 @@ public final class GridTestUtils {
     }
 
     /**
+     * @return Free communication port number on localhost.
+     * @throws IOException If unable to find a free port.
+     */
+    public static int getFreeCommPort() throws IOException {
+        for (int port = default_comm_port; port < max_comm_port; port++) {
+            try (ServerSocket sock = new ServerSocket(port)) {
+                return sock.getLocalPort();
+            }
+            catch (IOException ignored) {
+                // No-op.
+            }
+        }
+
+        throw new IOException("Unable to find a free communication port.");
+    }
+
+    /**
      * Every invocation of this method will never return a
      * repeating multicast group for a different test case.
      *

Reply via email to