diff --git a/cli/src/main/java/dev/enola/cli/CommandWithModel.java b/cli/src/main/java/dev/enola/cli/CommandWithModel.java index 16b93a08b..6c1abfaf1 100644 --- a/cli/src/main/java/dev/enola/cli/CommandWithModel.java +++ b/cli/src/main/java/dev/enola/cli/CommandWithModel.java @@ -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); } diff --git a/core/impl/src/main/java/dev/enola/core/grpc/EnolaGrpcClientProvider.java b/core/impl/src/main/java/dev/enola/core/grpc/EnolaGrpcClientProvider.java new file mode 100644 index 000000000..e360001a6 --- /dev/null +++ b/core/impl/src/main/java/dev/enola/core/grpc/EnolaGrpcClientProvider.java @@ -0,0 +1,47 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright 2023 The Enola 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 + + 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); + } +} diff --git a/core/impl/src/main/java/dev/enola/core/grpc/EnolaGrpcInProcess.java b/core/impl/src/main/java/dev/enola/core/grpc/EnolaGrpcInProcess.java index b91684149..9fd3e238e 100644 --- a/core/impl/src/main/java/dev/enola/core/grpc/EnolaGrpcInProcess.java +++ b/core/impl/src/main/java/dev/enola/core/grpc/EnolaGrpcInProcess.java @@ -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; @@ -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 private final EnolaService service; private final Server server; @@ -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; } diff --git a/core/impl/src/test/java/dev/enola/core/grpc/EnolaGrpcServerTest.java b/core/impl/src/test/java/dev/enola/core/grpc/EnolaGrpcServerTest.java index b2dc7b5da..b3ec46480 100644 --- a/core/impl/src/test/java/dev/enola/core/grpc/EnolaGrpcServerTest.java +++ b/core/impl/src/test/java/dev/enola/core/grpc/EnolaGrpcServerTest.java @@ -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; @@ -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 { @@ -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()); } } diff --git a/web/rest/src/test/java/dev/enola/web/rest/RestTest.java b/web/rest/src/test/java/dev/enola/web/rest/RestTest.java index a7ec346d9..46d7df572 100644 --- a/web/rest/src/test/java/dev/enola/web/rest/RestTest.java +++ b/web/rest/src/test/java/dev/enola/web/rest/RestTest.java @@ -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(); diff --git a/web/ui-soy/src/test/java/dev/enola/web/ui/UiTest.java b/web/ui-soy/src/test/java/dev/enola/web/ui/UiTest.java index 8570de9c3..39e73935d 100644 --- a/web/ui-soy/src/test/java/dev/enola/web/ui/UiTest.java +++ b/web/ui-soy/src/test/java/dev/enola/web/ui/UiTest.java @@ -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();