8194231: java/net/DatagramSocket/ReuseAddressTest.java failed with java.net.BindException: Address already in use: Cannot bind
authorazeller
Tue, 28 May 2019 20:12:16 +0200
changeset 55127 f0ef081cb15d
parent 55126 d2bc284803bc
child 55128 35192d9c2b76
8194231: java/net/DatagramSocket/ReuseAddressTest.java failed with java.net.BindException: Address already in use: Cannot bind Summary: Changed test to no longer use hardcoded ports. Reviewed-by: chegar, clanger
test/jdk/java/net/DatagramSocket/ReuseAddressTest.java
--- a/test/jdk/java/net/DatagramSocket/ReuseAddressTest.java	Fri May 31 15:02:45 2019 +0100
+++ b/test/jdk/java/net/DatagramSocket/ReuseAddressTest.java	Tue May 28 20:12:16 2019 +0200
@@ -1,4 +1,4 @@
-/* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+/* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -116,7 +116,7 @@
         MulticastSocket ms1 = null;
         MulticastSocket ms2 = null;
         try {
-            InetSocketAddress addr = createSocketAddress(5050);
+            InetSocketAddress addr = createSocketAddress(0);
 
             ms1 = new MulticastSocket(null);
             ms1.setReuseAddress(true);
@@ -132,6 +132,7 @@
                         + " unexpected " + e);
             }
 
+            addr = createSocketAddress(ms1.getLocalPort());
             ms2 = new MulticastSocket(null);
             ms2.setReuseAddress(true);
             if (!ms2.getReuseAddress()) {
@@ -186,7 +187,7 @@
         MulticastSocket ms1 = null;
         MulticastSocket ms2 = null;
         try {
-            InetSocketAddress addr = createSocketAddress(6060);
+            InetSocketAddress addr = createSocketAddress(0);
 
             ms1 = new MulticastSocket(null);
             try {
@@ -196,6 +197,7 @@
                         + " unexpected " + e);
             }
 
+            addr = createSocketAddress(ms1.getLocalPort());
             ms2 = new MulticastSocket(null);
             ms2.setReuseAddress(false);  // method under test
 
@@ -243,15 +245,13 @@
         DatagramSocket ds2 = null;
         try {
 
-            InetSocketAddress isa = createSocketAddress(7070);
-            InetAddress addr = isa.getAddress();
+            InetSocketAddress isa1 = createSocketAddress(0);
+            InetAddress addr = isa1.getAddress();
             InetAddress wildcard = InetAddress.getByName("0.0.0.0");
             if (addr.equals(wildcard) || addr.isLoopbackAddress()) {
                 System.out.println("Cannot check: addresses are equal");
             }
 
-            InetSocketAddress isa1 = new InetSocketAddress(addr, isa.getPort());
-            InetSocketAddress isa2 = new InetSocketAddress(wildcard, isa.getPort());
 
             ds1 = new DatagramSocket(null);
             ds1.setReuseAddress(true);    // method under test
@@ -261,6 +261,8 @@
             }
             ds1.bind(isa1);
 
+            InetSocketAddress isa2 = new InetSocketAddress(wildcard, ds1.getLocalPort());
+
             ds2 = new DatagramSocket(null);
             ds2.setReuseAddress(true);    // method under test
             if (!ds2.getReuseAddress()) {
@@ -275,8 +277,8 @@
                         + " unexpected " + e);
             }
 
-            if (ds1.getLocalPort() != isa.getPort() || !ds1.isBound()
-                    || ds2.getLocalPort() != isa.getPort() || !ds2.isBound()) {
+            if (ds1.getLocalPort() != ds2.getLocalPort() || !ds1.isBound()
+                    || !ds2.isBound()) {
                 System.out.println("bind() fails with: " + addr);
                 System.out.println("  ds1 [" + getInfo(ds1) + "]");
                 System.out.println("  ds2 [" + getInfo(ds2) + "]");