Back to Repositories

Testing gRPC Health Service Implementation in Conductor OSS

This test suite evaluates the health check functionality of a gRPC service implementation in the Conductor OSS framework. It verifies the health status reporting mechanism through comprehensive unit tests that simulate different service states and error conditions.

Test Coverage Overview

The test suite provides thorough coverage of the HealthServiceImpl functionality, including:

  • Verification of SERVING status when service is healthy
  • Validation of NOT_SERVING status when service is unhealthy
  • Error handling for interrupted health check operations
  • Edge cases around health check aggregator responses

Implementation Analysis

The testing approach utilizes JUnit and gRPC testing utilities to create isolated test environments. It employs mock objects through Mockito to simulate health check aggregator behavior and implements in-process server/client communication for reliable testing.

The implementation follows the gRPC service testing pattern with separate test cases for different service states.

Technical Details

Key technical components include:

  • GrpcCleanupRule for managing server/client lifecycle
  • InProcessServerBuilder for local testing
  • HealthGrpc.HealthBlockingStub for client interactions
  • Mockito for dependency mocking
  • JUnit ExpectedException for error case validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolation of test cases using unique server names
  • Proper resource cleanup through GrpcCleanupRule
  • Comprehensive error scenario coverage
  • Clear test method naming reflecting test intentions
  • Efficient use of mock objects for external dependencies

conductor-oss/conductor

grpc-server/src/test/java/com/netflix/conductor/grpc/server/service/HealthServiceImplTest.java

            
/*
 * Copyright 2020 Conductor Authors.
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.netflix.conductor.grpc.server.service;

public class HealthServiceImplTest {

    // SBMTODO: Move this Spring boot health check
    //    @Rule
    //    public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
    //
    //    @Rule
    //    public ExpectedException thrown = ExpectedException.none();
    //
    //    @Test
    //    public void healthServing() throws Exception {
    //        // Generate a unique in-process server name.
    //        String serverName = InProcessServerBuilder.generateName();
    //        HealthCheckAggregator hca = mock(HealthCheckAggregator.class);
    //        CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class);
    //        HealthCheckStatus hcs = mock(HealthCheckStatus.class);
    //        when(hcs.isHealthy()).thenReturn(true);
    //        when(hcsf.get()).thenReturn(hcs);
    //        when(hca.check()).thenReturn(hcsf);
    //        HealthServiceImpl healthyService = new HealthServiceImpl(hca);
    //
    //        addService(serverName, healthyService);
    //        HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub(
    //                // Create a client channel and register for automatic graceful shutdown.
    //
    // grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()));
    //
    //
    //        HealthCheckResponse reply =
    // blockingStub.check(HealthCheckRequest.newBuilder().build());
    //
    //        assertEquals(HealthCheckResponse.ServingStatus.SERVING, reply.getStatus());
    //    }
    //
    //    @Test
    //    public void healthNotServing() throws Exception {
    //        // Generate a unique in-process server name.
    //        String serverName = InProcessServerBuilder.generateName();
    //        HealthCheckAggregator hca = mock(HealthCheckAggregator.class);
    //        CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class);
    //        HealthCheckStatus hcs = mock(HealthCheckStatus.class);
    //        when(hcs.isHealthy()).thenReturn(false);
    //        when(hcsf.get()).thenReturn(hcs);
    //        when(hca.check()).thenReturn(hcsf);
    //        HealthServiceImpl healthyService = new HealthServiceImpl(hca);
    //
    //        addService(serverName, healthyService);
    //        HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub(
    //                // Create a client channel and register for automatic graceful shutdown.
    //
    // grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()));
    //
    //
    //        HealthCheckResponse reply =
    // blockingStub.check(HealthCheckRequest.newBuilder().build());
    //
    //        assertEquals(HealthCheckResponse.ServingStatus.NOT_SERVING, reply.getStatus());
    //    }
    //
    //    @Test
    //    public void healthException() throws Exception {
    //        // Generate a unique in-process server name.
    //        String serverName = InProcessServerBuilder.generateName();
    //        HealthCheckAggregator hca = mock(HealthCheckAggregator.class);
    //        CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class);
    //        when(hcsf.get()).thenThrow(InterruptedException.class);
    //        when(hca.check()).thenReturn(hcsf);
    //        HealthServiceImpl healthyService = new HealthServiceImpl(hca);
    //
    //        addService(serverName, healthyService);
    //        HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub(
    //                // Create a client channel and register for automatic graceful shutdown.
    //
    // grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()));
    //
    //        thrown.expect(StatusRuntimeException.class);
    //        thrown.expect(hasProperty("status", is(Status.INTERNAL)));
    //        blockingStub.check(HealthCheckRequest.newBuilder().build());
    //
    //    }
    //
    //    private void addService(String name, BindableService service) throws Exception {
    //        // Create a server, add service, start, and register for automatic graceful shutdown.
    //        grpcCleanup.register(InProcessServerBuilder
    //                .forName(name).directExecutor().addService(service).build().start());
    //    }
}