forked from grpc-ecosystem/grpc-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: apply grpc service config from consul (grpc-ecosystem#1045)
- Loading branch information
onyn
committed
Feb 2, 2024
1 parent
a2e9520
commit dd71c00
Showing
4 changed files
with
190 additions
and
3 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
105 changes: 105 additions & 0 deletions
105
...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,105 @@ | ||
package net.devh.boot.grpc.client.nameresolver; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
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 java.net.InetSocketAddress; | ||
import java.util.List; | ||
import java.util.Map; | ||
import net.devh.boot.grpc.common.util.GrpcUtils; | ||
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; | ||
|
||
/** | ||
* 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() { | ||
var 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" | ||
} | ||
] | ||
} | ||
"""; | ||
var listener = resolveServiceAndVerify("test1", validServiceConfig); | ||
var serviceConf = listener.getResult().getServiceConfig(); | ||
assertThat(serviceConf).isNotNull(); | ||
assertThat(serviceConf.getConfig()).isNotNull(); | ||
assertThat(serviceConf.getError()).isNull(); | ||
} | ||
|
||
@Test | ||
void testBrokenServiceConfig() { | ||
var listener = resolveServiceAndVerify("test2", "intentionally invalid service config"); | ||
var 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) { | ||
var props = new SimpleDiscoveryProperties(); | ||
var service = new DefaultServiceInstance(serviceName + "-1", serviceName, "127.0.0.1", 3322, false); | ||
var 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))); | ||
var disco = new SimpleDiscoveryClient(props); | ||
var dcnr = new DiscoveryClientNameResolver(serviceName, disco, args, null, null); | ||
|
||
var listener = new TestableListener(); | ||
dcnr.start(listener); | ||
|
||
assertThat(listener.isErrorWasSet()).isFalse(); | ||
assertThat(listener.isResultWasSet()).isTrue(); | ||
var 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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...-autoconfigure/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,30 @@ | ||
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