Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuch committed Jun 14, 2024
1 parent 77fdcf4 commit 21b0641
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions test/jdk/java/nio/channels/SocketChannel/OpenLeak.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,20 @@
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<>();
private static final int MAX_LOOP = 250000;

@Test
public void test() throws Exception {
InetAddress lo = InetAddress.getLoopbackAddress();

// Try to find a suitable port that will cause a
// Connection Rejected exception

// port 47 is reserved - there should be nothing there...
InetSocketAddress isa = new InetSocketAddress(lo, 47);
try (SocketChannel sc1 = SocketChannel.open(isa)) {
Expand All @@ -66,25 +65,30 @@ public void test() throws Exception {
return;
} catch (ConnectException x) { }

// create an unresolved address to test another path
// where close should be called
SocketAddress sa = InetSocketAddress.createUnresolved(isa.getHostString(), isa.getPort());

System.out.println("Expecting Connection Refused for " + isa);
System.out.println("Expecting UnresolvedAddressException for " + sa);
int i = 0;
try {
for (i = 0; i < 250000; i++) {
for (i = 0; i < MAX_LOOP; i++) {
try {
SocketChannel.open(sa);
throw new RuntimeException("This should not happen");
} catch (UnresolvedAddressException x) {
if (i > 250000 - 10) {
if (i < 5 || i >= MAX_LOOP - 5) {
// print a message for the first five and last 5 exceptions
System.out.println(x);
}
}
try {
SocketChannel.open(isa);
throw new RuntimeException("This should not happen");
} catch (ConnectException x) {
if (i > 250000 - 10) {
if (i < 5 || i >= MAX_LOOP - 5) {
// print a message for the first five and last 5 exceptions
System.out.println(x);
}
}
Expand Down

0 comments on commit 21b0641

Please sign in to comment.