|
1 | 1 | /* |
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
24 | 24 | /* @test |
25 | 25 | * @bug 4111507 |
26 | 26 | * @summary retryServerSocket should not retry on BindException |
27 | | - * @author Ann Wollrath |
28 | 27 | * |
29 | 28 | * @run main/othervm AddrInUse |
30 | 29 | */ |
|
33 | 32 | import java.rmi.registry.LocateRegistry; |
34 | 33 | import java.rmi.server.ExportException; |
35 | 34 |
|
36 | | -public class AddrInUse implements Runnable { |
| 35 | +public class AddrInUse { |
37 | 36 |
|
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; |
63 | 38 |
|
64 | 39 | public static void main(String[] args) throws Exception { |
65 | | - System.err.println("\nRegression test for bug 4111507\n"); |
66 | | - |
67 | 40 | /* |
68 | 41 | * Bind a server socket to a port. |
69 | 42 | */ |
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 + "..."); |
83 | 46 |
|
84 | 47 | /* |
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. |
87 | 50 | */ |
88 | | - obj.wait(TIMEOUT); |
| 51 | + System.err.println("create a registry on the same port..."); |
| 52 | + System.err.println("(should cause an ExportException)"); |
89 | 53 |
|
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; |
101 | 63 | } |
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); |
104 | 81 | } |
| 82 | + System.err.println("TEST PASSED, received expected exception: " + registryExportFailure); |
105 | 83 | } |
106 | 84 | } |
107 | 85 | } |
0 commit comments