-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[4.x] LazyConfigSource is now queried when an unknown node is request…
…ed (#8707) * LazyConfigSource is now queried when an unknown node is requested by the user, instead of being ignored. * Removed unneeded synchronized and use locks instead of ConcurrentHashMap, as computeIfAbsent should not be used for possibly blocking operations (which we need).
- Loading branch information
1 parent
85f95d6
commit fd27d4a
Showing
6 changed files
with
197 additions
and
9 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2024 Oracle and/or its affiliates. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.helidon.config.tests</groupId> | ||
<artifactId>helidon-config-tests-project</artifactId> | ||
<version>4.0.0-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>helidon-config-tests-test-lazy-source</artifactId> | ||
<name>Helidon Config Tests Lazy Source</name> | ||
|
||
<description> | ||
Integration tests of lazy config source. | ||
</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.helidon.config</groupId> | ||
<artifactId>helidon-config</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-all</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
85 changes: 85 additions & 0 deletions
85
...ts/test-lazy-source/src/test/java/io/helidon/config/tests/lazy/source/LazySourceTest.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,85 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.config.tests.lazy.source; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import io.helidon.config.Config; | ||
import io.helidon.config.ConfigSources; | ||
import io.helidon.config.spi.ConfigNode; | ||
import io.helidon.config.spi.ConfigSource; | ||
import io.helidon.config.spi.LazyConfigSource; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsInAnyOrder; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class LazySourceTest { | ||
@Test | ||
void testLazySource() { | ||
Map<String, String> map = new HashMap<>(); | ||
map.put("tree.node2", "value2"); | ||
|
||
TestLazySource testLazySource = new TestLazySource(map); | ||
|
||
Config config = Config.builder() | ||
.addSource(testLazySource) | ||
.addSource(ConfigSources.create(Map.of("tree.node1", "value1"))) | ||
.disableEnvironmentVariablesSource() | ||
.disableSystemPropertiesSource() | ||
.build(); | ||
|
||
assertThat(config.get("tree.node1").as(String.class).get(), is("value1")); | ||
|
||
// when using lazy config source, we defer the loading of the key until it is actually requested | ||
assertThat("tree.node2 should exist", config.get("tree.node2").exists(), is(true)); | ||
assertThat(config.get("tree.node2").as(String.class).get(), is("value2")); | ||
assertThat("tree.node3 should not exist", config.get("tree.node3").exists(), is(false)); | ||
|
||
// config tree is immutable once created - so we ignore values that appear in the source later in time, | ||
// as we have already resolved that the node is not present | ||
map.put("tree.node3", "value3"); | ||
assertThat("tree.node3 should not exist, as it was already cached as not existing", | ||
config.get("tree.node3").exists(), | ||
is(false)); | ||
|
||
// each node should have been requested from the config source, starting from root | ||
assertThat(testLazySource.requestedNodes, containsInAnyOrder("", "tree", "tree.node1", "tree.node2", "tree.node3")); | ||
} | ||
|
||
private static class TestLazySource implements LazyConfigSource, ConfigSource { | ||
private final List<String> requestedNodes = new ArrayList<>(); | ||
private final Map<String, String> values; | ||
|
||
private TestLazySource(Map<String, String> values) { | ||
this.values = values; | ||
} | ||
|
||
@Override | ||
public Optional<ConfigNode> node(String key) { | ||
requestedNodes.add(key); | ||
return Optional.ofNullable(values.get(key)) | ||
.map(ConfigNode.ValueNode::create); | ||
} | ||
} | ||
} |