Skip to content

Commit

Permalink
feat (core): Introduce EnolaGrpcClientProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Sep 30, 2023
1 parent 208eabd commit 884ab9a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cli/src/main/java/dev/enola/cli/CommandWithModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final void run() throws Exception {
var ekr = new EntityKindRepository();
ekr.load(modelResource);
esp = new EnolaServiceProvider();
service = new EnolaGrpcInProcess(esp.get(ekr)).getClient();
service = new EnolaGrpcInProcess(esp.get(ekr)).get();

run(ekr, service);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2023 The Enola <https://enola.dev> 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
*
* https://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 dev.enola.core.grpc;

import static java.util.concurrent.TimeUnit.SECONDS;

import dev.enola.core.proto.EnolaServiceGrpc;

import io.grpc.Grpc;
import io.grpc.InsecureChannelCredentials;
import io.grpc.ManagedChannel;

public class EnolaGrpcClientProvider
implements AutoCloseable { // javax.inject.Provider<EnolaServiceGrpc.EnolaServiceBlockingStub>

private final EnolaServiceGrpc.EnolaServiceBlockingStub client;
private final ManagedChannel channel;

public EnolaGrpcClientProvider(String endpoint) {
var credz = InsecureChannelCredentials.create();
channel = Grpc.newChannelBuilder(endpoint, credz).build();
client = EnolaServiceGrpc.newBlockingStub(channel).withDeadlineAfter(3, SECONDS);
}

public EnolaServiceGrpc.EnolaServiceBlockingStub get() {
return client;
}

public void close() throws Exception {
channel.shutdownNow().awaitTermination(3, SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package dev.enola.core.grpc;

import static java.util.concurrent.TimeUnit.SECONDS;

import dev.enola.core.EnolaService;
import dev.enola.core.proto.EnolaServiceGrpc;

Expand All @@ -28,7 +30,8 @@
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class EnolaGrpcInProcess implements AutoCloseable {
public class EnolaGrpcInProcess
implements AutoCloseable { // javax.inject.Provider<EnolaServiceGrpc.EnolaServiceBlockingStub>

private final EnolaService service;
private final Server server;
Expand All @@ -46,10 +49,10 @@ public EnolaGrpcInProcess(EnolaService service) throws IOException {
channelBuilder.directExecutor(); // as above
channel = channelBuilder.build();

client = EnolaServiceGrpc.newBlockingStub(channel);
client = EnolaServiceGrpc.newBlockingStub(channel).withDeadlineAfter(3, SECONDS);
}

public EnolaServiceGrpc.EnolaServiceBlockingStub getClient() {
public EnolaServiceGrpc.EnolaServiceBlockingStub get() {
return client;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import static com.google.common.truth.Truth.assertThat;

import static java.util.concurrent.TimeUnit.SECONDS;

import dev.enola.common.io.resource.ClasspathResource;
import dev.enola.core.EnolaService;
import dev.enola.core.EnolaServiceProvider;
Expand All @@ -29,10 +27,6 @@
import dev.enola.core.proto.GetEntityRequest;
import dev.enola.core.proto.ID;

import io.grpc.Grpc;
import io.grpc.InsecureChannelCredentials;
import io.grpc.ManagedChannel;

import org.junit.Test;

public class EnolaGrpcServerTest {
Expand All @@ -43,17 +37,16 @@ public void remoting() throws Exception {
// similarly in dev.enola.demo.ServerTest
var port = enolaServer.getPort();
var endpoint = "localhost:" + port;
var credz = InsecureChannelCredentials.create();
ManagedChannel channel = Grpc.newChannelBuilder(endpoint, credz).build();
check(EnolaServiceGrpc.newBlockingStub(channel).withDeadlineAfter(3, SECONDS));
channel.shutdownNow().awaitTermination(3, SECONDS);
try (var enolaClient = new EnolaGrpcClientProvider(endpoint)) {
check(enolaClient.get());
}
}
}

@Test
public void inProcess() throws Exception {
try (var enolaServer = new EnolaGrpcInProcess(service())) {
check(enolaServer.getClient());
check(enolaServer.get());
}
}

Expand Down
2 changes: 1 addition & 1 deletion web/rest/src/test/java/dev/enola/web/rest/RestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void getAndList() throws IOException {
var addr = new InetSocketAddress(0);
try (var server = new SunServer(addr)) {
// Setup
var testGrpcService = new EnolaGrpcInProcess(new TestService()).getClient();
var testGrpcService = new EnolaGrpcInProcess(new TestService()).get();
new RestAPI(testGrpcService).register(server);
server.start();
var rp = new ResourceProviders();
Expand Down
2 changes: 1 addition & 1 deletion web/ui-soy/src/test/java/dev/enola/web/ui/UiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class UiTest {
public void testUi() throws IOException {
var addr = new InetSocketAddress(0);
try (var server = new SunServer(addr)) {
var testGrpcService = new EnolaGrpcInProcess(new TestService()).getClient();
var testGrpcService = new EnolaGrpcInProcess(new TestService()).get();
new UI(testGrpcService).register(server);
server.start();
var rp = new ResourceProviders();
Expand Down

0 comments on commit 884ab9a

Please sign in to comment.