Skip to content

Commit

Permalink
Improve test for better coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuch committed Jun 14, 2024
1 parent 79ca2ef commit 77fdcf4
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions test/jdk/java/nio/channels/SocketChannel/OpenLeak.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> result = new CompletableFuture<>();
@Test
public void test() throws Exception {
InetAddress lo = InetAddress.getLoopbackAddress();
Expand All @@ -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;
}
}

Expand Down

0 comments on commit 77fdcf4

Please sign in to comment.