2222import io .grpc .ServerBuilder ;
2323import io .grpc .inprocess .InProcessChannelBuilder ;
2424import io .grpc .inprocess .InProcessServerBuilder ;
25+ import io .grpc .testing .GrpcCleanupRule ;
2526import okhttp3 .MediaType ;
2627import okhttp3 .OkHttpClient ;
2728import okhttp3 .ResponseBody ;
2829import okhttp3 .mock .Behavior ;
2930import okhttp3 .mock .MockInterceptor ;
3031import org .junit .Before ;
32+ import org .junit .Rule ;
3133import org .junit .jupiter .api .AfterAll ;
3234import org .junit .jupiter .api .BeforeAll ;
3335import org .junit .jupiter .api .BeforeEach ;
3739
3840import java .io .IOException ;
3941import java .time .Duration ;
42+ import java .util .concurrent .TimeUnit ;
4043import java .util .concurrent .TimeoutException ;
4144
4245import static io .dapr .utils .TestUtils .findFreePort ;
@@ -54,6 +57,9 @@ public class GrpcChannelFacadeTest {
5457
5558 private static DaprHttp daprHttp ;
5659
60+ @ Rule
61+ public static final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule ();
62+
5763 /**
5864 * Enable the waitForSidecar to allow the gRPC to check the http endpoint for the health check
5965 */
@@ -66,20 +72,19 @@ public void setUp() {
6672 @ BeforeAll
6773 public static void setup () throws IOException {
6874 port = findFreePort ();
69- server = ServerBuilder .forPort (port )
75+
76+ // Create a server, add service, start, and register for automatic graceful shutdown.
77+ grpcCleanup .register (ServerBuilder .forPort (port )
7078 .addService (new DaprGrpc .DaprImplBase () {
7179 })
72- .build ();
73- server .start ();
80+ .build ().start ());
7481 }
7582
7683 @ AfterAll
7784 public static void teardown () throws InterruptedException {
7885 if (daprHttp != null ) {
7986 daprHttp .close ();
8087 }
81- server .shutdown ();
82- server .awaitTermination ();
8388 }
8489
8590 @ Test
@@ -88,13 +93,14 @@ public void waitForSidecarTimeoutHealthCheck() throws Exception {
8893 DaprHttp daprHttp = new DaprHttp (Properties .SIDECAR_IP .get (), 3500 , okHttpClient );
8994
9095 ManagedChannel channel = InProcessChannelBuilder .forName ("waitForSidecarTimeoutHealthCheck" ).build ();
96+ grpcCleanup .register (channel );
9197 final GrpcChannelFacade channelFacade = new GrpcChannelFacade (channel , daprHttp );
9298
93- mockInterceptor .addRule ()
94- .get ()
95- .path ("/v1.0/healthz/outbound" )
96- .times (6 )
97- .respond (404 , ResponseBody .create ("Not Found" , MediaType .get ("application/json" )));
99+ mockInterceptor .addRule ()
100+ .get ()
101+ .path ("/v1.0/healthz/outbound" )
102+ .times (6 )
103+ .respond (404 , ResponseBody .create ("Not Found" , MediaType .get ("application/json" )));
98104
99105 StepVerifier .create (channelFacade .waitForChannelReady (1000 ))
100106 .expectSubscription ()
@@ -110,6 +116,9 @@ public void waitForSidecarOK() {
110116
111117 ManagedChannel channel = ManagedChannelBuilder .forAddress ("127.0.0.1" , port )
112118 .usePlaintext ().build ();
119+
120+ grpcCleanup .register (channel );
121+
113122 final GrpcChannelFacade channelFacade = new GrpcChannelFacade (channel , daprHttp );
114123
115124 // added since this is doing a check against the http health check endpoint
0 commit comments