Prevent duplicate entries in channels map
Prevents duplicate entries from being added to the named channels map in client properties. Resolves #67
This commit is contained in:
@@ -86,7 +86,7 @@ public class GrpcClientAutoConfiguration {
|
||||
@Override
|
||||
public String getTarget(String authority) {
|
||||
NamedChannel channel = this.channels.getChannel(authority);
|
||||
return this.channels.getTarget(channel.getAddress());
|
||||
return this.channels.getTarget(channel);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -83,6 +83,15 @@ public class GrpcClientProperties implements EnvironmentAware {
|
||||
return address;
|
||||
}
|
||||
|
||||
public String getTarget(NamedChannel channel) {
|
||||
String address = channel.getAddress();
|
||||
if (address.startsWith("static:") || address.startsWith("tcp:")) {
|
||||
address = address.substring(address.indexOf(":") + 1).replaceFirst("/*", "");
|
||||
}
|
||||
address = this.environment.resolvePlaceholders(address);
|
||||
return address;
|
||||
}
|
||||
|
||||
public static class NamedChannel {
|
||||
|
||||
/**
|
||||
|
||||
@@ -200,6 +200,19 @@ class GrpcClientPropertiesTests {
|
||||
assertThat(properties.getChannels()).containsOnlyKeys("custom");
|
||||
}
|
||||
|
||||
@Test
|
||||
void withCustomChannelReturnsDuplicateEntryMap() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("spring.grpc.client.channels.custom.address", "static://my-server:8888");
|
||||
GrpcClientProperties properties = bindProperties(map);
|
||||
GrpcClientAutoConfiguration.NamedChannelVirtualTargets virtualTargets = new GrpcClientAutoConfiguration.NamedChannelVirtualTargets(
|
||||
properties);
|
||||
var address = virtualTargets.getTarget("custom");
|
||||
assertThat(address).isEqualTo("my-server:8888");
|
||||
assertThat(properties.getTarget("custom")).isEqualTo("my-server:8888");
|
||||
assertThat(properties.getChannels()).containsOnlyKeys("custom");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user