diff --git a/sdk/src/test/java/io/dapr/client/GrpcChannelFacadeTest.java b/sdk/src/test/java/io/dapr/client/GrpcChannelFacadeTest.java index c775934d2..206c0bc5d 100644 --- a/sdk/src/test/java/io/dapr/client/GrpcChannelFacadeTest.java +++ b/sdk/src/test/java/io/dapr/client/GrpcChannelFacadeTest.java @@ -22,12 +22,14 @@ import io.grpc.ServerBuilder; import io.grpc.inprocess.InProcessChannelBuilder; import io.grpc.inprocess.InProcessServerBuilder; +import io.grpc.testing.GrpcCleanupRule; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.ResponseBody; import okhttp3.mock.Behavior; import okhttp3.mock.MockInterceptor; import org.junit.Before; +import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -37,6 +39,7 @@ import java.io.IOException; import java.time.Duration; +import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import static io.dapr.utils.TestUtils.findFreePort; @@ -54,6 +57,9 @@ public class GrpcChannelFacadeTest { private static DaprHttp daprHttp; + @Rule + public static final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); + /** * Enable the waitForSidecar to allow the gRPC to check the http endpoint for the health check */ @@ -66,11 +72,12 @@ public void setUp() { @BeforeAll public static void setup() throws IOException { port = findFreePort(); - server = ServerBuilder.forPort(port) + + // Create a server, add service, start, and register for automatic graceful shutdown. + grpcCleanup.register(ServerBuilder.forPort(port) .addService(new DaprGrpc.DaprImplBase() { }) - .build(); - server.start(); + .build().start()); } @AfterAll @@ -78,8 +85,6 @@ public static void teardown() throws InterruptedException { if (daprHttp != null) { daprHttp.close(); } - server.shutdown(); - server.awaitTermination(); } @Test @@ -88,13 +93,14 @@ public void waitForSidecarTimeoutHealthCheck() throws Exception { DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient); ManagedChannel channel = InProcessChannelBuilder.forName("waitForSidecarTimeoutHealthCheck").build(); + grpcCleanup.register(channel); final GrpcChannelFacade channelFacade = new GrpcChannelFacade(channel, daprHttp); - mockInterceptor.addRule() - .get() - .path("/v1.0/healthz/outbound") - .times(6) - .respond(404, ResponseBody.create("Not Found", MediaType.get("application/json"))); + mockInterceptor.addRule() + .get() + .path("/v1.0/healthz/outbound") + .times(6) + .respond(404, ResponseBody.create("Not Found", MediaType.get("application/json"))); StepVerifier.create(channelFacade.waitForChannelReady(1000)) .expectSubscription() @@ -110,6 +116,9 @@ public void waitForSidecarOK() { ManagedChannel channel = ManagedChannelBuilder.forAddress("127.0.0.1", port) .usePlaintext().build(); + + grpcCleanup.register(channel); + final GrpcChannelFacade channelFacade = new GrpcChannelFacade(channel, daprHttp); // added since this is doing a check against the http health check endpoint