|
16 | 16 |
|
17 | 17 | package net.devh.boot.grpc.server.autoconfigure; |
18 | 18 |
|
| 19 | +import io.grpc.ManagedChannel; |
| 20 | +import io.grpc.ManagedChannelBuilder; |
| 21 | +import io.grpc.Status; |
| 22 | +import io.grpc.StatusRuntimeException; |
| 23 | +import io.grpc.health.v1.HealthCheckRequest; |
| 24 | +import io.grpc.health.v1.HealthCheckResponse; |
| 25 | +import io.grpc.health.v1.HealthGrpc; |
| 26 | +import io.grpc.health.v1.HealthGrpc.HealthStub; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration; |
| 30 | +import org.springframework.boot.actuate.health.Health; |
| 31 | +import org.springframework.boot.actuate.health.HealthIndicator; |
19 | 32 | import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
20 | 33 | import org.springframework.boot.test.context.SpringBootTest; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
21 | 36 | import org.springframework.test.annotation.DirtiesContext; |
22 | 37 |
|
23 | | -@SpringBootTest(classes = GrpcHealthServiceDefaultAutoConfigurationTest.TestConfig.class, |
| 38 | +import static org.junit.jupiter.api.Assertions.*; |
| 39 | + |
| 40 | +@SpringBootTest(classes = { |
| 41 | + GrpcHealthServiceDefaultAutoConfigurationTest.TestConfig.class, |
| 42 | + GrpcHealthServiceTrueActuatorConfigurationTest.TestConfig.class |
| 43 | +}, |
24 | 44 | properties = { |
25 | | - "grpc.server.health-service-enabled=true", |
26 | 45 | "grpc.server.health-service-type=actuator", |
27 | 46 | }) |
28 | 47 | @ImportAutoConfiguration({ |
29 | 48 | GrpcServerAutoConfiguration.class, |
30 | 49 | GrpcServerFactoryAutoConfiguration.class, |
31 | 50 | GrpcHealthServiceAutoConfiguration.class, |
32 | | - GrpcActuatoHealthServiceAutoConfiguration.class |
| 51 | + HealthEndpointAutoConfiguration.class |
33 | 52 | }) |
34 | 53 | @DirtiesContext |
35 | 54 | class GrpcHealthServiceTrueActuatorConfigurationTest extends GrpcHealthServiceDefaultAutoConfigurationTest { |
| 55 | + @Configuration |
| 56 | + static class TestConfig { |
| 57 | + @Bean |
| 58 | + TestIndicator customIndicator() { |
| 59 | + return new TestIndicator(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + static class TestIndicator implements HealthIndicator { |
| 64 | + Health health = Health.up().build(); |
| 65 | + |
| 66 | + @Override |
| 67 | + public Health health() { |
| 68 | + return health; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Autowired |
| 73 | + TestIndicator customIndicator; |
| 74 | + |
| 75 | + @Test |
| 76 | + void testUnhealthyService() { |
| 77 | + final ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:9090").usePlaintext().build(); |
| 78 | + try { |
| 79 | + final HealthStub stub = HealthGrpc.newStub(channel); |
| 80 | + |
| 81 | + customIndicator.health = Health.down().build(); |
| 82 | + final AwaitableStreamObserver<HealthCheckResponse> resultObserver = new AwaitableStreamObserver<>(); |
| 83 | + stub.check(HealthCheckRequest.getDefaultInstance(), resultObserver); |
| 84 | + |
| 85 | + final HealthCheckResponse response = assertDoesNotThrow(resultObserver::getSingle); |
| 86 | + assertEquals(HealthCheckResponse.ServingStatus.NOT_SERVING, response.getStatus()); |
| 87 | + } finally { |
| 88 | + channel.shutdown(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void testSpecificUnhealthyService() { |
| 94 | + final ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:9090").usePlaintext().build(); |
| 95 | + try { |
| 96 | + final HealthStub stub = HealthGrpc.newStub(channel); |
| 97 | + |
| 98 | + customIndicator.health = Health.down().build(); |
| 99 | + final AwaitableStreamObserver<HealthCheckResponse> resultObserver = new AwaitableStreamObserver<>(); |
| 100 | + stub.check(HealthCheckRequest.newBuilder() |
| 101 | + .setService("customIndicator") |
| 102 | + .build(), resultObserver); |
| 103 | + |
| 104 | + final HealthCheckResponse response = assertDoesNotThrow(resultObserver::getSingle); |
| 105 | + assertEquals(HealthCheckResponse.ServingStatus.NOT_SERVING, response.getStatus()); |
| 106 | + } finally { |
| 107 | + channel.shutdown(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void testNotFoundService() throws InterruptedException { |
| 113 | + final ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:9090").usePlaintext().build(); |
| 114 | + try { |
| 115 | + final HealthStub stub = HealthGrpc.newStub(channel); |
| 116 | + |
| 117 | + customIndicator.health = Health.down().build(); |
| 118 | + final AwaitableStreamObserver<HealthCheckResponse> resultObserver = new AwaitableStreamObserver<>(); |
| 119 | + stub.check(HealthCheckRequest.newBuilder() |
| 120 | + .setService("someservice") |
| 121 | + .build(), resultObserver); |
| 122 | + |
| 123 | + var error = resultObserver.getError(); |
| 124 | + assertInstanceOf( StatusRuntimeException.class, error); |
| 125 | + assertEquals(Status.NOT_FOUND.getCode(),((StatusRuntimeException) error).getStatus().getCode()); |
| 126 | + } finally { |
| 127 | + channel.shutdown(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
36 | 131 | } |
0 commit comments