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

Introduce LauncherInterceptor API #3091

Merged
merged 7 commits into from
Jan 31, 2023
Merged

Conversation

marcphilipp
Copy link
Member

@marcphilipp marcphilipp commented Nov 14, 2022

Overview

In order to support use cases where test discovery and execution needs to be wrapped by third-party code, e.g. for using the Eclipse OSGi runtime this PR introduces LauncherInterceptor which can be registered using Java's ServiceLoader mechanism. The feature is opt-in via a new configuration parameter than can be set as a system property in order to avoid accidental usage via transitive dependencies.


I hereby agree to the terms of the JUnit Contributor License Agreement.


Definition of Done

@sormuras
Copy link
Member

Nice work, Marc! Will take a closer look later this week.

I already see a way to supersed this project, or at least make sormuras/junit-platform-isolator use this new feature. 🤔

@marcphilipp marcphilipp force-pushed the marc/launcher-interceptor branch from ea04536 to 5614e89 Compare November 14, 2022 08:07
@jbduncan
Copy link
Contributor

Interesting! I'm guessing this feature could also be used to run logic that needs to run only once per test suite invocation?

@marcphilipp
Copy link
Member Author

Yes, but LauncherSessionListener might be a better fit for that.

@marcphilipp marcphilipp force-pushed the marc/launcher-interceptor branch from d7c2980 to 95f3925 Compare January 10, 2023 14:26
@marcphilipp marcphilipp force-pushed the marc/launcher-interceptor branch from 95f3925 to 880fc6f Compare January 23, 2023 18:56
@marcphilipp marcphilipp force-pushed the marc/launcher-interceptor branch from 880fc6f to 4af316e Compare January 31, 2023 13:05
So they can influence the class loader used to load test engines etc.
- Avoid early static initialization during native image compilation
  where possible
- Use and document `--initialize-at-build-time` build argument
@marcphilipp marcphilipp force-pushed the marc/launcher-interceptor branch from 4af316e to da8e76f Compare January 31, 2023 14:33
@holly-cummins
Copy link

holly-cummins commented Jul 10, 2023

Hi @marcphilipp, thanks for this. I'm having a play with the new capability for the Quarkus use case (as discussed in #2579 and #3028). Using sophisticated debug (system out printlns), I can see that the launcher interceptor is being invoked, and is calling currentThread.setContextClassLoader in the intercept method. The launcher interceptor is running nice and early, before any of my tests or test extensions.

I have an extension which implements InvocationInterceptor. In its interceptBeforeAllMethod, I check the classloader of the test class with extensionContext.getRequiredTestClass().getClassLoader(). I would expect it to be the classloader I set on the TCCL in the LauncherInterceptor, but it's not. Similarly, if I check invocationContext.getExecutable().getParameters() and look at the classloader of the parameters, they're the same as they would be if my interceptor wasn't there.

Am I doing something wrong, or was I just over-optimistic about how much would be changed by doing an interception? I do hope it's "I am doing something wrong," because we've been quite excited about in the Quarkus team about how this would allow us to eliminate some classloader hacks.

(We're already able to run the test class itself from a different classloader with the existing interceptors, just by making a new instance with Class.forName() on the new classloader, but the arguments we have to pass in to the 'new' class are loaded by the 'old' classloader, and we get argument type mismatches. )

@holly-cummins
Copy link

Actually, it's an interceptTestMethod override where I'm checking the test class's classloader.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants