-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stronger safeguards against leaked config in internal Quarkus*Test ex…
…tensions If someone calls ConfigProvider.getConfig() out of the blue in a test that doesn't use any Quarkus*Test extension, this will call QuarkusConfigFactory and leak config in two ways: 1. In QuarkusConfigFactory#config 2. In SmallRyeConfigProviderResolver, registering config for the TCCL, which in such a case is most likely the system CL. A well-behaved test would call QuarkusConfigFactory.setConfig(null) to clean up all that, but it's easy to miss and there is potential for ConfigProvider.getConfig() being called indirectly, so there's no way we can guarantee all tests are well-behaved. This should at least guarantee that after a badly behaving test executes, the next test using a Quarkus*Test extension will clean up the mess.
- Loading branch information
Showing
4 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
test-framework/junit5-internal/src/main/java/io/quarkus/test/ConfigUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.test; | ||
|
||
import io.quarkus.runtime.configuration.QuarkusConfigFactory; | ||
|
||
public final class ConfigUtil { | ||
private ConfigUtil() { | ||
} | ||
|
||
/** | ||
* Clean up config left over from badly-behaving previous tests. | ||
* <p> | ||
* Tests may call ConfigProvider.getConfig() directly or indirectly, | ||
* triggering lazy initialization in QuarkusConfigFactory#getConfigFor, | ||
* and if they don't call QuarkusConfigFactory.setConfig(null) afterward, | ||
* they may leak that config. | ||
* As it's quite hard to guarantee that all tests clean up after them, | ||
* especially if they don't use any Quarkus*Test extensions, | ||
* we call this cleanup here in Quarkus*Test extensions as an additional safeguard. | ||
* <p> | ||
* Note this must be called quite early in extensions in order to be effective: | ||
* things like TestHTTPResourceManager#getUri make use of config and may be called very early, | ||
* upon test instantiation, so cleaning up in beforeEach() won't cut it for example. | ||
*/ | ||
public static void cleanUp() { | ||
try { | ||
QuarkusConfigFactory.setConfig(null); | ||
} catch (Throwable ignored) { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters