Ensure only one client is created by default

This commit is contained in:
Dave Syer
2025-04-03 11:33:45 +01:00
parent 97ba123fbf
commit 326c075b36
4 changed files with 131 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
package org.springframework.grpc.sample;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.grpc.client.FutureStubFactory;
import org.springframework.grpc.client.ImportGrpcClients;
import org.springframework.grpc.sample.proto.SimpleGrpc;
import org.springframework.grpc.test.AutoConfigureInProcessTransport;
import io.grpc.stub.AbstractStub;
public class GrpcClientApplicationTests {
@Nested
@SpringBootTest
@AutoConfigureInProcessTransport
class NoAutowiredClients {
@Autowired
private ApplicationContext context;
@Test
void noStubIsCreated() {
assertThat(context.containsBeanDefinition("simpleBlockingStub")).isFalse();
assertThat(context.containsBeanDefinition("simpleStub")).isFalse();
assertThat(context.containsBeanDefinition("simpleFutureStub")).isFalse();
assertThat(context.getBeanNamesForType(AbstractStub.class)).isEmpty();
}
}
@Nested
@SpringBootTest(properties = "spring.grpc.client.default-channel.address=0.0.0.0:9090")
@AutoConfigureInProcessTransport
class DefaultAutowiredClients {
@Autowired
private ApplicationContext context;
@Test
void onlyDefaultStubIsCreated() {
assertThat(context.containsBeanDefinition("simpleBlockingStub")).isTrue();
assertThat(context.getBean(SimpleGrpc.SimpleBlockingStub.class)).isNotNull();
assertThat(context.containsBeanDefinition("simpleStub")).isFalse();
assertThat(context.containsBeanDefinition("simpleFutureStub")).isFalse();
assertThat(context.getBeanNamesForType(AbstractStub.class)).hasSize(1);
}
}
@Nested
@SpringBootTest(properties = "spring.grpc.client.default-channel.address=0.0.0.0:9090")
@AutoConfigureInProcessTransport
class SpecificAutowiredClients {
@Autowired
private ApplicationContext context;
@Test
void stubOfCorrectTypeIsCreated() {
assertThat(context.containsBeanDefinition("simpleFutureStub")).isTrue();
assertThat(context.getBean(SimpleGrpc.SimpleFutureStub.class)).isNotNull();
assertThat(context.containsBeanDefinition("simpleStub")).isFalse();
assertThat(context.containsBeanDefinition("simpleBlockingStub")).isFalse();
assertThat(context.getBeanNamesForType(AbstractStub.class)).hasSize(1);
}
@TestConfiguration
@ImportGrpcClients(basePackageClasses = SimpleGrpc.class, factory = FutureStubFactory.class)
static class TestConfig {
}
}
}

View File

@@ -25,6 +25,11 @@ public class BlockingStubFactory extends AbstractStubFactory<AbstractBlockingStu
super(AbstractBlockingStub.class);
}
@Override
public boolean supports(Class<?> type) {
return super.supports(type) && !type.getSimpleName().contains("BlockingV2");
}
@Override
public int getOrder() {
return SimpleStubFactory.SIMPLE_STUB_ORDER - 30;

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2024-2024 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.springframework.grpc.client;
import org.springframework.core.Ordered;
import io.grpc.stub.AbstractBlockingStub;
public class BlockingV2StubFactory extends AbstractStubFactory<AbstractBlockingStub<?>> implements Ordered {
public BlockingV2StubFactory() {
super(AbstractBlockingStub.class);
}
@Override
public boolean supports(Class<?> type) {
return super.supports(type) && type.getSimpleName().contains("BlockingV2");
}
@Override
public int getOrder() {
return SimpleStubFactory.SIMPLE_STUB_ORDER - 30;
}
@Override
protected String methodName() {
return "newBlockingStub";
}
}

View File

@@ -56,6 +56,7 @@ public class GrpcClientRegistry {
public GrpcClientRegistry(GenericApplicationContext context) {
this.context = context;
stubs(new BlockingStubFactory());
stubs(new BlockingV2StubFactory());
stubs(new FutureStubFactory());
stubs(new ReactorStubFactory());
stubs(new SimpleStubFactory());