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

Fix config sources ordering #171

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
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
Expand Up @@ -45,22 +45,25 @@ Iterable<ConfigSource> getConfigSources(ConsulConfig config, ConsulConfigGateway
return Collections.emptyList();
}

List<ConfigSource> result = new ArrayList<>(keys.size());
Map<String, ConfigSource> results = new LinkedHashMap<>(keys.size());

List<Uni<?>> allUnis = new ArrayList<>();

for (Map.Entry<String, ValueType> entry : keys.entrySet()) {
String fullKey = config.prefix().isPresent() ? config.prefix().get() + "/" + entry.getKey() : entry.getKey();
results.put(fullKey, null);
allUnis.add(consulConfigGateway.getValue(fullKey).invoke(new Consumer<Response>() {
@Override
public void accept(Response response) {
if (response != null) {
result.add(ResponseConfigSourceUtil.toConfigSource(response, entry.getValue(), config.prefix()));
results.put(response.getKey(),
ResponseConfigSourceUtil.toConfigSource(response, entry.getValue(), config.prefix()));
} else {
String message = "Key '" + fullKey + "' not found in Consul.";
if (config.failOnMissingKey()) {
throw new RuntimeException(message);
} else {
results.remove(fullKey);
log.info(message);
}
}
Expand All @@ -81,6 +84,6 @@ public void accept(Response response) {
consulConfigGateway.close();
}

return result;
return results.values();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.eclipse.microprofile.config.spi.ConfigSource;
Expand Down Expand Up @@ -207,6 +208,35 @@ void testPropertiesKeysWithPrefix() {
verify(mockGateway, times(1)).getValue("config/second");
}

@Test
void testConfigSourcesOrdered() {
ConsulConfig config = mock(ConsulConfig.class);
// We want this order: first > second > third (first overrides second which overrides third)
when(config.rawValueKeys()).thenReturn(keyValues("some/first", "some/second", "some/third"));
AgentConfig agentConfig = mock(AgentConfig.class);
when(config.agent()).thenReturn(agentConfig);

ConsulConfigGateway mockGateway = mock(ConsulConfigGateway.class);
// make sure the third property is received first
when(mockGateway.getValue("some/first")).thenReturn(validResponse("some/third", "third"));
// make sure the first property is received in second
when(mockGateway.getValue("some/second")).thenReturn(validResponse("some/first", "first"));
// make sure the second property is received in third
when(mockGateway.getValue("some/third")).thenReturn(validResponse("some/second", "second"));

Iterable<ConfigSource> configSources = new ConsulConfigSourceFactory().getConfigSources(config, mockGateway);
assertThat(configSources).hasSize(3);
assertThat(configSources).extracting(ConfigSource::getProperties).containsExactly(
Map.of("some.first", "first"),
Map.of("some.second", "second"),
Map.of("some.third", "third"));

//all keys should have been resolved because we resolve keys in the order they were given by the user
verify(mockGateway, times(1)).getValue("some/first");
verify(mockGateway, times(1)).getValue("some/second");
verify(mockGateway, times(1)).getValue("some/third");
}

private Optional<List<String>> keyValues(String... keys) {
return Optional.of(Arrays.asList(keys));
}
Expand Down
Loading