# HG changeset patch # User alanb # Date 1554626399 -3600 # Node ID dbd1beb994ab49f3c760691bdb8c14aee36e4a73 # Parent f22d38b23756eff8fea48ddccf122036916eb2c2 Extend test coverage of UDP sockets diff -r f22d38b23756 -r dbd1beb994ab test/jdk/java/net/Socket/Timeouts.java --- a/test/jdk/java/net/Socket/Timeouts.java Sat Apr 06 16:36:24 2019 +0100 +++ b/test/jdk/java/net/Socket/Timeouts.java Sun Apr 07 09:39:59 2019 +0100 @@ -494,8 +494,8 @@ Future result1 = pool.submit(ss::accept); Future result2 = pool.submit(ss::accept); - // establish connection after 3 seconds - scheduleConnect(ss.getLocalSocketAddress(), 3000); + // establish connection after 2 seconds + scheduleConnect(ss.getLocalSocketAddress(), 2000); // one task should have accepted the connection, the other should // have completed with SocketTimeoutException @@ -515,7 +515,7 @@ } assertTrue((s1 != null) ^ (s2 != null)); - // should get here in 4 seconds, not 7 seconds + // should get here in 4 seconds, not 8 seconds int timeout = ss.getSoTimeout(); checkDuration(start, timeout-100, timeout+2000); } finally { diff -r f22d38b23756 -r dbd1beb994ab test/jdk/java/net/Socket/UdpSocket.java --- a/test/jdk/java/net/Socket/UdpSocket.java Sat Apr 06 16:36:24 2019 +0100 +++ b/test/jdk/java/net/Socket/UdpSocket.java Sun Apr 07 09:39:59 2019 +0100 @@ -28,6 +28,7 @@ */ import java.io.IOException; +import java.lang.ref.WeakReference; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; @@ -37,7 +38,8 @@ import java.security.Permission; import java.util.Arrays; import java.util.ArrayList; -import java.util.List; +import java.util.ArrayDeque; +import java.util.Deque; import org.testng.annotations.Test; import static org.testng.Assert.*; @@ -84,7 +86,7 @@ * Test that the number of UDP sockets is limited when running with a * security manager. */ - public void testMaxSockets() throws IOException { + public void testMaxSockets() throws Exception { int limit = Integer.getInteger("sun.net.maxDatagramSockets"); // security manager grants all permissions @@ -93,14 +95,14 @@ }; System.setSecurityManager(securityManager); - List sockets = new ArrayList<>(); + Deque sockets = new ArrayDeque<>(); try { // create the maximum number of sockets for (int i=0; i(s); + s = null; + while (ref.get() != null) { + System.gc(); + Thread.sleep(100); + } + + // try to create another socket - should succeed + s = newUdpSocket(); s.close(); } finally { closeAll(sockets); @@ -123,9 +136,9 @@ return new Socket(InetAddress.getLoopbackAddress(), 8000, false); } - private void closeAll(List sockets) throws IOException { - for (Socket s : sockets) { - s.close(); - } + private static void closeAll(Deque sockets) throws IOException { + sockets.forEach(s -> { + try { s.close(); } catch (IOException ignore) { } + }); } }