From 77fdcf413d2b606554de2b4d18ddb93859d9fcd3 Mon Sep 17 00:00:00 2001 From: Daniel Fuchs Date: Fri, 14 Jun 2024 16:21:29 +0100 Subject: [PATCH] Improve test for better coverage --- .../nio/channels/SocketChannel/OpenLeak.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/test/jdk/java/nio/channels/SocketChannel/OpenLeak.java b/test/jdk/java/nio/channels/SocketChannel/OpenLeak.java index c3e30c4cd5bc3..8cc98dc801c49 100644 --- a/test/jdk/java/nio/channels/SocketChannel/OpenLeak.java +++ b/test/jdk/java/nio/channels/SocketChannel/OpenLeak.java @@ -32,13 +32,17 @@ import java.net.ConnectException; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.SocketAddress; import java.nio.channels.SocketChannel; +import java.nio.channels.UnresolvedAddressException; +import java.util.concurrent.CompletableFuture; import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; public class OpenLeak { + final CompletableFuture result = new CompletableFuture<>(); @Test public void test() throws Exception { InetAddress lo = InetAddress.getLoopbackAddress(); @@ -59,14 +63,35 @@ public void test() throws Exception { isa = new InetSocketAddress(lo, 61); try (SocketChannel sc3 = SocketChannel.open(isa)) {}; Assumptions.abort("Could not find a suitable port"); + return; } catch (ConnectException x) { } + SocketAddress sa = InetSocketAddress.createUnresolved(isa.getHostString(), isa.getPort()); System.out.println("Expecting Connection Refused for " + isa); - for (int i=0; i<100000; i++) { - try { - SocketChannel.open(isa); - throw new RuntimeException("This should not happen"); - } catch (ConnectException x) { } + System.out.println("Expecting UnresolvedAddressException for " + sa); + int i = 0; + try { + for (i = 0; i < 250000; i++) { + try { + SocketChannel.open(sa); + throw new RuntimeException("This should not happen"); + } catch (UnresolvedAddressException x) { + if (i > 250000 - 10) { + System.out.println(x); + } + } + try { + SocketChannel.open(isa); + throw new RuntimeException("This should not happen"); + } catch (ConnectException x) { + if (i > 250000 - 10) { + System.out.println(x); + } + } + } + } catch (Throwable t) { + System.out.println("Failed at " + i + " with " + t); + throw t; } }