Repeat DatagramSocket instance creation. 28/41128/2
authorJozef Gloncak <jgloncak@cisco.com>
Thu, 30 Jun 2016 10:02:56 +0000 (12:02 +0200)
committerJozef Gloncak <jgloncak@cisco.com>
Thu, 30 Jun 2016 12:52:47 +0000 (12:52 +0000)
If creation of instance of DatagramSocket class throws
SocketException then it is tried once again.

Change-Id: I78453883a50b770889dcc7d3da5baa69a42c6825
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
integrationtest/src/test/java/org/opendaylight/lispflowmapping/integrationtest/MappingServiceIntegrationTest.java

index f9de601ec56b4598d2a67c81d886caab157e300f..2a1213c8aacc4d3e69a64c38e1d949ae85ff3eff 100644 (file)
@@ -144,6 +144,11 @@ import org.slf4j.LoggerFactory;
 public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
     private static final Logger LOG = LoggerFactory.getLogger(MappingServiceIntegrationTest.class);
 
+    /**
+     * Defines how many attempt to create instance of DatagramSocket will be done before giving up.
+     */
+    private static final int NUM_OF_ATTEMPTS_TO_CREATE_SOCKET = 2;
+
     private byte[] mapRequestPacket;
     private byte[] mapRegisterPacketWithNotify;
     private byte[] mapRegisterPacketWithoutNotify;
@@ -2139,13 +2144,15 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
     }
 
     private DatagramSocket initSocket(DatagramSocket socket, int port) {
-        try {
-            socket = new DatagramSocket(new InetSocketAddress(ourAddress, port));
-        } catch (SocketException e) {
-            LOG.error("Can't initize socket for {}", ourAddress, e);
-            fail();
+        for (int i=0; i < NUM_OF_ATTEMPTS_TO_CREATE_SOCKET; i++) {
+            try {
+                return new DatagramSocket(new InetSocketAddress(ourAddress, port));
+            } catch (SocketException e) {
+                LOG.error("Can't initialize socket for {}", ourAddress, e);
+            }
         }
-        return socket;
+        fail();
+        return null;
     }
 
     private byte[] extractWSUdpByteArray(String wiresharkHex) {