-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1046 from onyn/feat/service-config
feat: apply grpc service config from consul (#1045)
- Loading branch information
Showing
5 changed files
with
224 additions
and
1 deletion.
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
119 changes: 119 additions & 0 deletions
119
...src/test/java/net/devh/boot/grpc/client/nameresolver/DiscoveryClientNameResolverTest.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,119 @@ | ||
/* | ||
* Copyright (c) 2016-2023 The gRPC-Spring Authors | ||
* | ||
* 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 net.devh.boot.grpc.client.nameresolver; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.cloud.client.DefaultServiceInstance; | ||
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClient; | ||
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties; | ||
|
||
import io.grpc.NameResolver; | ||
import io.grpc.Status; | ||
import io.grpc.SynchronizationContext; | ||
import io.grpc.internal.AutoConfiguredLoadBalancerFactory; | ||
import io.grpc.internal.GrpcUtil; | ||
import io.grpc.internal.ScParser; | ||
import net.devh.boot.grpc.common.util.GrpcUtils; | ||
|
||
/** | ||
* Test for {@link DiscoveryClientNameResolver}. | ||
*/ | ||
public class DiscoveryClientNameResolverTest { | ||
|
||
private final NameResolver.Args args = NameResolver.Args.newBuilder() | ||
.setDefaultPort(1212) | ||
.setProxyDetector(GrpcUtil.DEFAULT_PROXY_DETECTOR) | ||
.setSynchronizationContext( | ||
new SynchronizationContext((t, e) -> { | ||
throw new AssertionError(e); | ||
})) | ||
.setServiceConfigParser(new ScParser(true, 10, 10, new AutoConfiguredLoadBalancerFactory("pick_first"))) | ||
.setOffloadExecutor(Runnable::run) | ||
.build(); | ||
|
||
@Test | ||
void testValidServiceConfig() { | ||
String validServiceConfig = """ | ||
{ | ||
"loadBalancingConfig": [ | ||
{"round_robin": {}} | ||
], | ||
"methodConfig": [ | ||
{ | ||
"name": [{}], | ||
"retryPolicy": { | ||
"maxAttempts": 5, | ||
"initialBackoff": "0.05s", | ||
"maxBackoff": "1s", | ||
"backoffMultiplier": 2, | ||
"retryableStatusCodes": [ | ||
"UNAVAILABLE", | ||
"ABORTED", | ||
"DATA_LOSS", | ||
"INTERNAL", | ||
"DEADLINE_EXCEEDED" | ||
] | ||
}, | ||
"timeout": "5s" | ||
} | ||
] | ||
} | ||
"""; | ||
TestableListener listener = resolveServiceAndVerify("test1", validServiceConfig); | ||
NameResolver.ConfigOrError serviceConf = listener.getResult().getServiceConfig(); | ||
assertThat(serviceConf).isNotNull(); | ||
assertThat(serviceConf.getConfig()).isNotNull(); | ||
assertThat(serviceConf.getError()).isNull(); | ||
} | ||
|
||
@Test | ||
void testBrokenServiceConfig() { | ||
TestableListener listener = resolveServiceAndVerify("test2", "intentionally invalid service config"); | ||
NameResolver.ConfigOrError serviceConf = listener.getResult().getServiceConfig(); | ||
assertThat(serviceConf).isNotNull(); | ||
assertThat(serviceConf.getConfig()).isNull(); | ||
assertThat(serviceConf.getError()).extracting(Status::getCode).isEqualTo(Status.Code.UNKNOWN); | ||
} | ||
|
||
private TestableListener resolveServiceAndVerify(String serviceName, String serviceConfig) { | ||
SimpleDiscoveryProperties props = new SimpleDiscoveryProperties(); | ||
DefaultServiceInstance service = new DefaultServiceInstance( | ||
serviceName + "-1", serviceName, "127.0.0.1", 3322, false); | ||
Map<String, String> meta = service.getMetadata(); | ||
meta.put(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT, "6688"); | ||
meta.put(GrpcUtils.CLOUD_DISCOVERY_METADATA_SERVICE_CONFIG, serviceConfig); | ||
props.setInstances(Map.of(serviceName, List.of(service))); | ||
SimpleDiscoveryClient disco = new SimpleDiscoveryClient(props); | ||
DiscoveryClientNameResolver dcnr = new DiscoveryClientNameResolver(serviceName, disco, args, null, null); | ||
|
||
TestableListener listener = new TestableListener(); | ||
dcnr.start(listener); | ||
|
||
assertThat(listener.isErrorWasSet()).isFalse(); | ||
assertThat(listener.isResultWasSet()).isTrue(); | ||
InetSocketAddress addr = (InetSocketAddress) listener.getResult().getAddresses().get(0).getAddresses().get(0); | ||
assertThat(addr.getPort()).isEqualTo(6688); | ||
assertThat(addr.getHostString()).isEqualTo("127.0.0.1"); | ||
return listener; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...g-boot-starter/src/test/java/net/devh/boot/grpc/client/nameresolver/TestableListener.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,43 @@ | ||
/* | ||
* Copyright (c) 2016-2023 The gRPC-Spring Authors | ||
* | ||
* 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 net.devh.boot.grpc.client.nameresolver; | ||
|
||
import io.grpc.NameResolver; | ||
import io.grpc.Status; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class TestableListener extends NameResolver.Listener2 { | ||
|
||
private NameResolver.ResolutionResult result; | ||
private Status error; | ||
private boolean resultWasSet = false; | ||
private boolean errorWasSet = false; | ||
|
||
@Override | ||
public void onResult(NameResolver.ResolutionResult resolutionResult) { | ||
this.result = resolutionResult; | ||
resultWasSet = true; | ||
} | ||
|
||
@Override | ||
public void onError(Status error) { | ||
this.error = error; | ||
errorWasSet = true; | ||
} | ||
|
||
} |
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