Skip to content

Commit

Permalink
Moves ClientUriSupplier as a top-level class. Adds new test showing h…
Browse files Browse the repository at this point in the history
…ow to retry and advance to next URI using a supplier.
  • Loading branch information
spericas committed Sep 25, 2024
1 parent d6c18a0 commit afc7eb7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.webclient.grpc;

import java.util.Iterator;

import io.helidon.webclient.api.ClientUri;

/**
* Interface implemented by all client URI suppliers.
*/
public interface ClientUriSupplier extends Iterator<ClientUri>, Iterable<ClientUri> {

@Override
default Iterator<ClientUri> iterator() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@
*/
public class ClientUriSuppliers {

/**
* Interfaces implemented by all client URI suppliers.
*/
public interface ClientUriSupplier extends Iterator<ClientUri> {
}

/**
* Supplies an iterator that returns URIs chosen in order from
* first to last.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ abstract class GrpcBaseClientCall<ReqT, ResT> extends ClientCall<ReqT, ResT> {
private final Duration pollWaitTime;
private final boolean abortPollTimeExpired;
private final Duration heartbeatPeriod;
private final ClientUriSuppliers.ClientUriSupplier clientUriSupplier;
private final ClientUriSupplier clientUriSupplier;

private final MethodDescriptor.Marshaller<ReqT> requestMarshaller;
private final MethodDescriptor.Marshaller<ResT> responseMarshaller;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ interface GrpcClientConfigBlueprint extends HttpClientConfig, Prototype.Factory<
GrpcClientProtocolConfig protocolConfig();

/**
* A {@link io.helidon.webclient.grpc.ClientUriSuppliers.ClientUriSupplier} that
* can dynamically provide zero or more {@link io.helidon.webclient.api.ClientUri}s
* to connect to.
* A {@link io.helidon.webclient.grpc.ClientUriSupplier} that can dynamically
* provide zero or more {@link io.helidon.webclient.api.ClientUri}s to connect.
*
* @return a supplier for zero or more client URIs
*/
Optional<ClientUriSuppliers.ClientUriSupplier> clientUriSupplier();
Optional<ClientUriSupplier> clientUriSupplier();
}

5 changes: 5 additions & 0 deletions webclient/tests/grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
<artifactId>helidon-webserver-testing-junit5-grpc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.fault-tolerance</groupId>
<artifactId>helidon-fault-tolerance</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import io.helidon.common.configurable.Resource;
import io.helidon.common.tls.Tls;
import io.helidon.faulttolerance.Retry;
import io.helidon.webclient.api.ClientUri;
import io.helidon.webclient.grpc.ClientUriSuppliers;
import io.helidon.webclient.grpc.GrpcClient;
Expand Down Expand Up @@ -52,8 +53,11 @@ class GrpcClientUriTest extends GrpcBaseTest {
.build();
}

/**
* Shows how each gRPC call advances the iterator in the {@code ClientUriSupplier}.
*/
@Test
void testClientUris() {
void testSupplier() {
CountDownLatch latch = new CountDownLatch(2);
ClientUri clientUri = ClientUri.create(URI.create("https://localhost:" + server.port()));
GrpcClient grpcClient = GrpcClient.builder()
Expand All @@ -66,7 +70,26 @@ void testClientUris() {
assertThat(res1.getText(), is("HELLO"));
Strings.StringMessage res2 = service.upper(newStringMessage("hello"));
assertThat(res2.getText(), is("HELLO"));
assertThat(latch.getCount(), is(0L));
}

/**
* Should fail to connect to first URI but succeed with second after retrying.
*/
@Test
void testSupplierWithRetries() {
CountDownLatch latch = new CountDownLatch(2);
ClientUri badUri = ClientUri.create(URI.create("https://foo:8000"));
ClientUri goodUri = ClientUri.create(URI.create("https://localhost:" + server.port()));
GrpcClient grpcClient = GrpcClient.builder()
.tls(clientTls)
.clientUriSupplier(new ClientUriSupplierTest(latch, badUri, goodUri))
.build();
StringServiceGrpc.StringServiceBlockingStub service = StringServiceGrpc.newBlockingStub(grpcClient.channel());

Retry retry = Retry.builder().calls(2).build();
Strings.StringMessage res = retry.invoke(() -> service.upper(newStringMessage("hello")));
assertThat(res.getText(), is("HELLO"));
assertThat(latch.getCount(), is(0L));
}

Expand Down

0 comments on commit afc7eb7

Please sign in to comment.