|
| 1 | +package net.devh.boot.grpc.test.inject; |
| 2 | + |
| 3 | +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; |
| 4 | + |
| 5 | +import io.grpc.stub.AbstractStub; |
| 6 | +import io.micrometer.core.instrument.MeterRegistry; |
| 7 | +import net.devh.boot.grpc.client.inject.GrpcClient; |
| 8 | +import net.devh.boot.grpc.client.stubfactory.StandardJavaGrpcStubFactory; |
| 9 | +import net.devh.boot.grpc.client.stubfactory.StubFactory; |
| 10 | +import net.devh.boot.grpc.test.config.BaseAutoConfiguration; |
| 11 | +import net.devh.boot.grpc.test.config.InProcessConfiguration; |
| 12 | +import net.devh.boot.grpc.test.config.MetricConfiguration; |
| 13 | +import net.devh.boot.grpc.test.config.ServiceConfiguration; |
| 14 | +import net.devh.boot.grpc.test.proto.TestServiceGrpc; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.boot.actuate.autoconfigure.metrics.JvmMetricsAutoConfiguration; |
| 18 | +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; |
| 19 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 20 | +import org.springframework.boot.test.context.SpringBootTest; |
| 21 | +import org.springframework.context.annotation.Bean; |
| 22 | +import org.springframework.context.annotation.Configuration; |
| 23 | +import org.springframework.stereotype.Component; |
| 24 | +import org.springframework.test.annotation.DirtiesContext; |
| 25 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 26 | + |
| 27 | +@SpringBootTest |
| 28 | +@SpringJUnitConfig( |
| 29 | + classes = { |
| 30 | + GrpcClientMetricsTest.TestConfig.class, |
| 31 | + GrpcClientMetricsTest.GrpcClientConstructorInjectionBean.class, |
| 32 | + InProcessConfiguration.class, |
| 33 | + ServiceConfiguration.class, |
| 34 | + BaseAutoConfiguration.class, |
| 35 | + MetricConfiguration.class |
| 36 | + }) |
| 37 | +@DirtiesContext |
| 38 | +public class GrpcClientMetricsTest { |
| 39 | + @Autowired |
| 40 | + MeterRegistry registry; |
| 41 | + |
| 42 | + @Test |
| 43 | + void jvmMetricsTest() { |
| 44 | + assertThat(registry.getMeters()) |
| 45 | + .filteredOn(meter -> meter.getId().getName().contains("jvm")) |
| 46 | + .isNotEmpty(); |
| 47 | + } |
| 48 | + |
| 49 | + @Component |
| 50 | + public static class GrpcClientConstructorInjectionBean { |
| 51 | + public TestServiceGrpc.TestServiceBlockingStub blockingStub; |
| 52 | + public TestServiceGrpc.TestServiceFutureStub futureStubForClientTest; |
| 53 | + public TestServiceGrpc.TestServiceBlockingStub anotherBlockingStub; |
| 54 | + public TestServiceGrpc.TestServiceBlockingStub unnamedTestServiceBlockingStub; |
| 55 | + public CustomGrpc.FactoryMethodAccessibleStub anotherServiceClientBean; |
| 56 | + |
| 57 | + public GrpcClientConstructorInjectionBean( |
| 58 | + @GrpcClient("test") TestServiceGrpc.TestServiceBlockingStub blockingStub, |
| 59 | + @GrpcClient("test") TestServiceGrpc.TestServiceFutureStub futureStubForClientTest, |
| 60 | + @GrpcClient("anotherTest") TestServiceGrpc.TestServiceBlockingStub anotherBlockingStub, |
| 61 | + @GrpcClient("unnamed") TestServiceGrpc.TestServiceBlockingStub unnamedTestServiceBlockingStub, |
| 62 | + @GrpcClient("test") CustomGrpc.FactoryMethodAccessibleStub anotherServiceClientBean) { |
| 63 | + this.blockingStub = blockingStub; |
| 64 | + this.futureStubForClientTest = futureStubForClientTest; |
| 65 | + this.anotherBlockingStub = anotherBlockingStub; |
| 66 | + this.unnamedTestServiceBlockingStub = unnamedTestServiceBlockingStub; |
| 67 | + this.anotherServiceClientBean = anotherServiceClientBean; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Configuration |
| 72 | + @ImportAutoConfiguration({MetricsAutoConfiguration.class, JvmMetricsAutoConfiguration.class}) |
| 73 | + public static class TestConfig { |
| 74 | + @Bean |
| 75 | + StubFactory customStubFactory() { |
| 76 | + return new StandardJavaGrpcStubFactory() { |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean isApplicable(final Class<? extends AbstractStub<?>> stubType) { |
| 80 | + return CustomStub.class.isAssignableFrom(stubType); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected String getFactoryMethodName() { |
| 85 | + return "custom"; |
| 86 | + } |
| 87 | + |
| 88 | + }; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments