Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -43,6 +43,7 @@
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -110,17 +111,21 @@ public synchronized void start() throws Exception {
try {
AtomicBoolean error = new AtomicBoolean(false);
AtomicBoolean bindError = new AtomicBoolean(false);
// The predicate below tries to recognise failures. On a port clash, it sees e.g.
// Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 46481; nested exception is:
// ...and will never see "main enter" from TestApp.
p = ProcessTools.startProcess(
TEST_APP_NAME + "{" + name + "}",
pb,
(line) -> {
if (line.toLowerCase().contains("exception")
|| line.toLowerCase().contains("error")) {
if (line.contains("Exception")) {
error.set(true);
bindError.set(line.toLowerCase().contains("port already in use"));
return true; // On Exception, app will never start
}
bindError.set(line.toLowerCase().contains("bindexception"));
return true;
});
return line.contains("main enter");
},
60, TimeUnit.SECONDS);
if (bindError.get()) {
throw new BindException("Process could not be started");
} else if (error.get()) {
Expand Down Expand Up @@ -164,9 +169,25 @@ public void close() throws Exception {
}

private static final String TEST_APP_NAME = "TestApp";
private static final int FREE_PORT_ATTEMPTS = 10;

private static void testDefaultAgent(String propertyFile) throws Exception {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good refactoring change as it also removes some broken logic in the current retry strategy

int port = Utils.getFreePort();
for (int i = 1; i <= FREE_PORT_ATTEMPTS; i++) {
int port = Utils.getFreePort();
System.out.println("Attempting testDefaultAgent(" + propertyFile + ") with port: " + port);
try {
testDefaultAgent(propertyFile, port);
break; // return succesfully
} catch (BindException b) {
// Retry with new port. Throw if last iteration:
if (i == FREE_PORT_ATTEMPTS) {
throw(b);
}
}
}
}

private static void testDefaultAgent(String propertyFile, int port) throws Exception {
String propFile = System.getProperty("test.src") + File.separator + propertyFile;
List<String> pbArgs = new ArrayList<>(Arrays.asList(
"-cp",
Expand Down Expand Up @@ -235,53 +256,32 @@ private static JMXServiceURL testConnect(int port) throws Exception {
public static void main(String[] args) throws Exception {
System.out.println("---" + DefaultAgentFilterTest.class.getName() + "-main: starting ...");

boolean retry = false;
do {
try {
// filter DefaultAgentFilterTest$MyTestObject
testDefaultAgent("mgmt1.properties");
System.out.println("----\tTest FAILED !!");
throw new RuntimeException("---" + DefaultAgentFilterTest.class.getName() + " - No exception reported");
} catch (Exception ex) {
if (ex instanceof InvocationTargetException) {
if (ex.getCause() instanceof BindException
|| ex.getCause() instanceof java.rmi.ConnectException) {
System.out.println("Failed to allocate ports. Retrying ...");
retry = true;
}
} else if (ex instanceof InvalidClassException) {
System.out.println("----\tTest PASSED !!");
} else if (ex instanceof UnmarshalException
&& ((UnmarshalException) ex).getCause() instanceof InvalidClassException) {
System.out.println("----\tTest PASSED !!");
} else {
System.out.println(ex);
System.out.println("----\tTest FAILED !!");
throw ex;
}
}
} while (retry);
retry = false;
do {
try {
// filter non-existent class
testDefaultAgent("mgmt2.properties");
try {
// filter DefaultAgentFilterTest$MyTestObject
testDefaultAgent("mgmt1.properties");
System.out.println("----\tTest FAILED !!");
throw new RuntimeException("---" + DefaultAgentFilterTest.class.getName() + " - No exception reported");
} catch (Exception ex) {
if (ex instanceof InvalidClassException) {
System.out.println("----\tTest PASSED !!");
} catch (Exception ex) {
if (ex instanceof InvocationTargetException) {
if (ex.getCause() instanceof BindException
|| ex.getCause() instanceof java.rmi.ConnectException) {
System.out.println("Failed to allocate ports. Retrying ...");
retry = true;
}
} else {
System.out.println(ex);
System.out.println("----\tTest FAILED !!");
throw ex;
}
} else if (ex instanceof UnmarshalException
&& ((UnmarshalException) ex).getCause() instanceof InvalidClassException) {
System.out.println("----\tTest PASSED !!");
} else {
System.out.println(ex);
System.out.println("----\tTest FAILED !!");
throw ex;
}
} while (retry);

}
try {
// filter non-existent class
testDefaultAgent("mgmt2.properties");
System.out.println("----\tTest PASSED !!");
} catch (Exception ex) {
System.out.println(ex);
System.out.println("----\tTest FAILED !!");
throw ex;
}
System.out.println("---" + DefaultAgentFilterTest.class.getName() + "-main: finished ...");
}

Expand Down