Skip to content

Commit

Permalink
End-to-end test of GrpcServiceClient.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas committed Jun 24, 2024
1 parent dbf5abb commit f70b197
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,66 @@

package io.helidon.microprofile.grpc.client;

import io.helidon.microprofile.grpc.client.test.Echo;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import io.helidon.common.configurable.Resource;
import io.helidon.common.tls.Tls;
import io.helidon.microprofile.grpc.core.Grpc;
import io.helidon.microprofile.grpc.core.GrpcMarshaller;
import io.helidon.microprofile.grpc.core.Unary;
import io.helidon.microprofile.grpc.server.GrpcMpCdiExtension;
import io.helidon.microprofile.testing.junit5.AddBean;
import io.helidon.microprofile.testing.junit5.AddExtension;
import io.helidon.microprofile.testing.junit5.HelidonTest;
import io.helidon.webclient.grpc.GrpcClient;

import io.grpc.Channel;
import io.grpc.stub.StreamObserver;
import jakarta.inject.Inject;
import jakarta.ws.rs.client.WebTarget;
import org.junit.jupiter.api.Test;

import static io.helidon.grpc.core.ResponseHelper.complete;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

@HelidonTest
@AddBean(EchoServiceTest.EchoService.class)
@AddBean(JavaMarshaller.Supplier.class)
@AddExtension(GrpcMpCdiExtension.class)
class EchoServiceTest {

// @Test
void testEcho() {
@Inject
private WebTarget webTarget;

@Test
void testEcho() throws InterruptedException, ExecutionException, TimeoutException {
Tls clientTls = Tls.builder()
.trust(trust -> trust
.keystore(store -> store
.passphrase("password")
.trustStore(true)
.keystore(Resource.create("client.p12"))))
.build();
GrpcClient grpcClient = GrpcClient.builder()
.tls(clientTls)
.baseUri("https://localhost:" + webTarget.getUri().getPort())
.build();

ClientServiceDescriptor descriptor = ClientServiceDescriptor.builder(EchoService.class)
.name("EchoService")
.marshallerSupplier(new JavaMarshaller.Supplier())
.unary("Echo")
.build();

Channel channel = null; // TODO
GrpcServiceClient client = GrpcServiceClient.create(channel, descriptor);
StreamObserver<Echo.EchoResponse> observer = new StreamObserver<>() {
CompletableFuture<String> future = new CompletableFuture<>();
GrpcServiceClient client = GrpcServiceClient.create(grpcClient.channel(), descriptor);
StreamObserver<String> observer = new StreamObserver<>() {
@Override
public void onNext(Echo.EchoResponse value) {
// TODO check value
public void onNext(String value) {
future.complete(value);
}

@Override
Expand All @@ -56,11 +86,8 @@ public void onError(Throwable t) {
public void onCompleted() {
}
};
client.unary("Echo", fromString("Howdy"), observer);
}

private Echo.EchoRequest fromString(String value) {
return Echo.EchoRequest.newBuilder().setMessage(value).build();
client.unary("Echo", "Howdy", observer);
assertThat(future.get(5, TimeUnit.SECONDS), is("Howdy"));
}

@Grpc
Expand All @@ -73,11 +100,10 @@ public static class EchoService {
* @param observer the call response
*/
@Unary(name = "Echo")
public void echo(Echo.EchoRequest request, StreamObserver<Echo.EchoResponse> observer) {
@GrpcMarshaller("java")
public void echo(String request, StreamObserver<String> observer) {
try {
String message = request.getMessage();
Echo.EchoResponse response = Echo.EchoResponse.newBuilder().setMessage(message).build();
complete(observer, response);
complete(observer, request);
} catch (IllegalStateException e) {
observer.onError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import io.helidon.grpc.core.MarshallerSupplier;

import io.grpc.MethodDescriptor;
import jakarta.enterprise.context.Dependent;
import jakarta.inject.Named;

/**
* An implementation of a gRPC {@link io.grpc.MethodDescriptor.Marshaller} that
Expand Down Expand Up @@ -57,8 +59,10 @@ public T parse(InputStream in) {
* A {@link io.helidon.grpc.core.MarshallerSupplier} implementation that supplies
* instances of {@link io.helidon.microprofile.grpc.client.JavaMarshaller}.
*/
public static class Supplier
implements MarshallerSupplier {
@Dependent
@Named("java")
public static class Supplier implements MarshallerSupplier {

@Override
public <T> MethodDescriptor.Marshaller<T> get(Class<T> clazz) {
return new JavaMarshaller<>();
Expand Down
30 changes: 30 additions & 0 deletions microprofile/grpc/client/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# 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.
#

server:
port: 0
tls:
trust:
keystore:
passphrase: "password"
trust-store: true
resource:
resource-path: "server.p12"
private-key:
keystore:
passphrase: "password"
resource:
resource-path: "server.p12"
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# For more information see $JAVA_HOME/jre/lib/logging.properties

# Send messages to the console
handlers=io.helidon.common.HelidonConsoleHandler
handlers=io.helidon.logging.jul.HelidonConsoleHandler

# HelidonConsoleHandler uses a SimpleFormatter subclass that replaces "!thread!" with the current thread
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n
Expand Down
Binary file not shown.

0 comments on commit f70b197

Please sign in to comment.