Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve available port discovery in tests #3555

Merged
merged 1 commit into from Feb 16, 2022

Conversation

jamesnetherton
Copy link
Contributor

No description provided.

private static String getCallerClassName() {
return StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
.walk(s -> s.map(StackWalker.StackFrame::getClassName)
.filter(className -> !className.equals(AvailablePortFinder.class.getName()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we make AvailablePortFinder class final, to better match the assumption made on the above line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shouldn't we make AvailablePortFinder class final

The class declaration is:

public final class AvailablePortFinder {
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Filtering on className should be enough then.

Copy link
Contributor

Choose a reason for hiding this comment

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

The class declaration is:

public final class AvailablePortFinder {
}

I am blind. Sorry for the noise.

LOGGER.info("getNextAvailable() -> {}", port);
return port;
String callerClassName = getCallerClassName();
String value = RESERVED_PORTS.putIfAbsent(port, callerClassName);
Copy link
Contributor

@ppalaga ppalaga Feb 16, 2022

Choose a reason for hiding this comment

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

Is this making some assumptions about whether this class can concurrently exist in multiple JVMs or multiple class loaders within a single maven invocation? IIRC surefire is forking a new JVM for each Maven module. So if building with mvnd, the AvailablePortFinder class (and thus also separate RESERVED_PORTS instances) may exist in several concurrent JVMs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you elaborate a bit more? It's not entirely clear to me why it matters in this context.

All it's doing is trying to track which class reserved a specific port. Arguably we don't need to do that. It just provides some nice logging output and a way to clear out stale entries from the RESERVED_PORTS map.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok - after re-reading. It's about separate RESERVED_PORTS instances.

In which case, yes, I suppose the map contents would be different between concurrent JVMs or class loaders.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for explaining. If it is just for logging, indeed, multiple RESERVED_PORTS instances in concurrent per-module JVMs do not matter.

@jamesnetherton jamesnetherton merged commit 39de94a into apache:main Feb 16, 2022
@jamesnetherton jamesnetherton deleted the test-ports branch February 16, 2022 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants