Skip to content

Commit 02ba9af

Browse files
author
duke
committed
Backport 04c0f8d359a3f450ac2070c6d41834145d9c75f7
1 parent 70ba5d4 commit 02ba9af

File tree

1 file changed

+37
-59
lines changed

1 file changed

+37
-59
lines changed
Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,7 +24,6 @@
2424
/* @test
2525
* @bug 4111507
2626
* @summary retryServerSocket should not retry on BindException
27-
* @author Ann Wollrath
2827
*
2928
* @run main/othervm AddrInUse
3029
*/
@@ -33,75 +32,54 @@
3332
import java.rmi.registry.LocateRegistry;
3433
import java.rmi.server.ExportException;
3534

36-
public class AddrInUse implements Runnable {
35+
public class AddrInUse {
3736

38-
private static int port = -1;
39-
private static final long TIMEOUT = 10000;
40-
41-
private boolean exportSucceeded = false;
42-
private Throwable exportException = null;
43-
44-
public void run() {
45-
46-
/*
47-
* Attempt to create (i.e. export) a registry on the port that
48-
* has already been bound, and record the result.
49-
*/
50-
try {
51-
LocateRegistry.createRegistry(port);
52-
synchronized (this) {
53-
exportSucceeded = true;
54-
notifyAll();
55-
}
56-
} catch (Throwable t) {
57-
synchronized (this) {
58-
exportException = t;
59-
notifyAll();
60-
}
61-
}
62-
}
37+
private static volatile Throwable registryExportFailure = null;
6338

6439
public static void main(String[] args) throws Exception {
65-
System.err.println("\nRegression test for bug 4111507\n");
66-
6740
/*
6841
* Bind a server socket to a port.
6942
*/
70-
ServerSocket server = new ServerSocket(0);
71-
port = server.getLocalPort();
72-
System.err.println("Created a ServerSocket on port " + port + "...");
73-
74-
/*
75-
* Start a thread that creates a registry on the same port,
76-
* and analyze the result.
77-
*/
78-
System.err.println("create a registry on the same port...");
79-
System.err.println("(should cause an ExportException)");
80-
AddrInUse obj = new AddrInUse();
81-
synchronized (obj) {
82-
(new Thread(obj, "AddrInUse")).start();
43+
try (ServerSocket server = new ServerSocket(0)) {
44+
int port = server.getLocalPort();
45+
System.err.println("Created a ServerSocket on port " + port + "...");
8346

8447
/*
85-
* Don't wait forever (original bug is that the export
86-
* hangs).
48+
* Start a thread that creates a registry on the same port,
49+
* and analyze the result.
8750
*/
88-
obj.wait(TIMEOUT);
51+
System.err.println("create a registry on the same port...");
52+
System.err.println("(should cause an ExportException)");
8953

90-
if (obj.exportSucceeded) {
91-
throw new RuntimeException(
92-
"TEST FAILED: export on already-bound port succeeded");
93-
} else if (obj.exportException != null) {
94-
obj.exportException.printStackTrace();
95-
if (obj.exportException instanceof ExportException) {
96-
System.err.println("TEST PASSED");
97-
} else {
98-
throw new RuntimeException(
99-
"TEST FAILED: unexpected exception occurred",
100-
obj.exportException);
54+
Thread exportRegistryThread = new Thread(() -> {
55+
/*
56+
* Attempt to create (i.e. export) a registry on the port that
57+
* has already been bound, and record the result.
58+
*/
59+
try {
60+
LocateRegistry.createRegistry(port);
61+
} catch (Throwable t) {
62+
registryExportFailure = t;
10163
}
102-
} else {
103-
throw new RuntimeException("TEST FAILED: export timed out");
64+
}, "ExportRegistry-Thread");
65+
66+
exportRegistryThread.start();
67+
68+
/*
69+
* Wait for the LocateRegistry.createRegistry() call to complete or
70+
* if it blocks forever (due to the original bug), then let jtreg fail
71+
* the test with a timeout
72+
*/
73+
exportRegistryThread.join();
74+
if (registryExportFailure == null) {
75+
throw new RuntimeException(
76+
"TEST FAILED: export on already-bound port succeeded");
77+
}
78+
if (!(registryExportFailure instanceof ExportException)) {
79+
throw new RuntimeException(
80+
"TEST FAILED: unexpected exception occurred", registryExportFailure);
10481
}
82+
System.err.println("TEST PASSED, received expected exception: " + registryExportFailure);
10583
}
10684
}
10785
}

0 commit comments

Comments
 (0)