Skip to content

Commit 1b1500a

Browse files
committed
8206187: javax/management/remote/mandatory/connection/DefaultAgentFilterTest.java fails with Port already in use
Backport-of: cd36be42c2eb3eacdb3625e87510eb15acac3230
1 parent 0e6959d commit 1b1500a

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

test/jdk/javax/management/remote/mandatory/connection/DefaultAgentFilterTest.java

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2022, 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
@@ -43,6 +43,7 @@
4343
import java.rmi.registry.Registry;
4444
import java.util.ArrayList;
4545
import java.util.Arrays;
46+
import java.util.concurrent.TimeUnit;
4647
import java.util.HashMap;
4748
import java.util.HashSet;
4849
import java.util.List;
@@ -110,17 +111,21 @@ public synchronized void start() throws Exception {
110111
try {
111112
AtomicBoolean error = new AtomicBoolean(false);
112113
AtomicBoolean bindError = new AtomicBoolean(false);
114+
// The predicate below tries to recognise failures. On a port clash, it sees e.g.
115+
// Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 46481; nested exception is:
116+
// ...and will never see "main enter" from TestApp.
113117
p = ProcessTools.startProcess(
114118
TEST_APP_NAME + "{" + name + "}",
115119
pb,
116120
(line) -> {
117-
if (line.toLowerCase().contains("exception")
118-
|| line.toLowerCase().contains("error")) {
121+
if (line.contains("Exception")) {
119122
error.set(true);
123+
bindError.set(line.toLowerCase().contains("port already in use"));
124+
return true; // On Exception, app will never start
120125
}
121-
bindError.set(line.toLowerCase().contains("bindexception"));
122-
return true;
123-
});
126+
return line.contains("main enter");
127+
},
128+
60, TimeUnit.SECONDS);
124129
if (bindError.get()) {
125130
throw new BindException("Process could not be started");
126131
} else if (error.get()) {
@@ -164,9 +169,25 @@ public void close() throws Exception {
164169
}
165170

166171
private static final String TEST_APP_NAME = "TestApp";
172+
private static final int FREE_PORT_ATTEMPTS = 10;
167173

168174
private static void testDefaultAgent(String propertyFile) throws Exception {
169-
int port = Utils.getFreePort();
175+
for (int i = 1; i <= FREE_PORT_ATTEMPTS; i++) {
176+
int port = Utils.getFreePort();
177+
System.out.println("Attempting testDefaultAgent(" + propertyFile + ") with port: " + port);
178+
try {
179+
testDefaultAgent(propertyFile, port);
180+
break; // return succesfully
181+
} catch (BindException b) {
182+
// Retry with new port. Throw if last iteration:
183+
if (i == FREE_PORT_ATTEMPTS) {
184+
throw(b);
185+
}
186+
}
187+
}
188+
}
189+
190+
private static void testDefaultAgent(String propertyFile, int port) throws Exception {
170191
String propFile = System.getProperty("test.src") + File.separator + propertyFile;
171192
List<String> pbArgs = new ArrayList<>(Arrays.asList(
172193
"-cp",
@@ -235,53 +256,32 @@ private static JMXServiceURL testConnect(int port) throws Exception {
235256
public static void main(String[] args) throws Exception {
236257
System.out.println("---" + DefaultAgentFilterTest.class.getName() + "-main: starting ...");
237258

238-
boolean retry = false;
239-
do {
240-
try {
241-
// filter DefaultAgentFilterTest$MyTestObject
242-
testDefaultAgent("mgmt1.properties");
243-
System.out.println("----\tTest FAILED !!");
244-
throw new RuntimeException("---" + DefaultAgentFilterTest.class.getName() + " - No exception reported");
245-
} catch (Exception ex) {
246-
if (ex instanceof InvocationTargetException) {
247-
if (ex.getCause() instanceof BindException
248-
|| ex.getCause() instanceof java.rmi.ConnectException) {
249-
System.out.println("Failed to allocate ports. Retrying ...");
250-
retry = true;
251-
}
252-
} else if (ex instanceof InvalidClassException) {
253-
System.out.println("----\tTest PASSED !!");
254-
} else if (ex instanceof UnmarshalException
255-
&& ((UnmarshalException) ex).getCause() instanceof InvalidClassException) {
256-
System.out.println("----\tTest PASSED !!");
257-
} else {
258-
System.out.println(ex);
259-
System.out.println("----\tTest FAILED !!");
260-
throw ex;
261-
}
262-
}
263-
} while (retry);
264-
retry = false;
265-
do {
266-
try {
267-
// filter non-existent class
268-
testDefaultAgent("mgmt2.properties");
259+
try {
260+
// filter DefaultAgentFilterTest$MyTestObject
261+
testDefaultAgent("mgmt1.properties");
262+
System.out.println("----\tTest FAILED !!");
263+
throw new RuntimeException("---" + DefaultAgentFilterTest.class.getName() + " - No exception reported");
264+
} catch (Exception ex) {
265+
if (ex instanceof InvalidClassException) {
269266
System.out.println("----\tTest PASSED !!");
270-
} catch (Exception ex) {
271-
if (ex instanceof InvocationTargetException) {
272-
if (ex.getCause() instanceof BindException
273-
|| ex.getCause() instanceof java.rmi.ConnectException) {
274-
System.out.println("Failed to allocate ports. Retrying ...");
275-
retry = true;
276-
}
277-
} else {
278-
System.out.println(ex);
279-
System.out.println("----\tTest FAILED !!");
280-
throw ex;
281-
}
267+
} else if (ex instanceof UnmarshalException
268+
&& ((UnmarshalException) ex).getCause() instanceof InvalidClassException) {
269+
System.out.println("----\tTest PASSED !!");
270+
} else {
271+
System.out.println(ex);
272+
System.out.println("----\tTest FAILED !!");
273+
throw ex;
282274
}
283-
} while (retry);
284-
275+
}
276+
try {
277+
// filter non-existent class
278+
testDefaultAgent("mgmt2.properties");
279+
System.out.println("----\tTest PASSED !!");
280+
} catch (Exception ex) {
281+
System.out.println(ex);
282+
System.out.println("----\tTest FAILED !!");
283+
throw ex;
284+
}
285285
System.out.println("---" + DefaultAgentFilterTest.class.getName() + "-main: finished ...");
286286
}
287287

0 commit comments

Comments
 (0)