Skip to content

Commit 326c075

Browse files
committed
Ensure only one client is created by default
1 parent 97ba123 commit 326c075

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.springframework.grpc.sample;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Nested;
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.test.context.TestConfiguration;
10+
import org.springframework.context.ApplicationContext;
11+
import org.springframework.grpc.client.FutureStubFactory;
12+
import org.springframework.grpc.client.ImportGrpcClients;
13+
import org.springframework.grpc.sample.proto.SimpleGrpc;
14+
import org.springframework.grpc.test.AutoConfigureInProcessTransport;
15+
16+
import io.grpc.stub.AbstractStub;
17+
18+
public class GrpcClientApplicationTests {
19+
20+
@Nested
21+
@SpringBootTest
22+
@AutoConfigureInProcessTransport
23+
class NoAutowiredClients {
24+
25+
@Autowired
26+
private ApplicationContext context;
27+
28+
@Test
29+
void noStubIsCreated() {
30+
assertThat(context.containsBeanDefinition("simpleBlockingStub")).isFalse();
31+
assertThat(context.containsBeanDefinition("simpleStub")).isFalse();
32+
assertThat(context.containsBeanDefinition("simpleFutureStub")).isFalse();
33+
assertThat(context.getBeanNamesForType(AbstractStub.class)).isEmpty();
34+
}
35+
36+
}
37+
38+
@Nested
39+
@SpringBootTest(properties = "spring.grpc.client.default-channel.address=0.0.0.0:9090")
40+
@AutoConfigureInProcessTransport
41+
class DefaultAutowiredClients {
42+
43+
@Autowired
44+
private ApplicationContext context;
45+
46+
@Test
47+
void onlyDefaultStubIsCreated() {
48+
assertThat(context.containsBeanDefinition("simpleBlockingStub")).isTrue();
49+
assertThat(context.getBean(SimpleGrpc.SimpleBlockingStub.class)).isNotNull();
50+
assertThat(context.containsBeanDefinition("simpleStub")).isFalse();
51+
assertThat(context.containsBeanDefinition("simpleFutureStub")).isFalse();
52+
assertThat(context.getBeanNamesForType(AbstractStub.class)).hasSize(1);
53+
}
54+
55+
}
56+
57+
@Nested
58+
@SpringBootTest(properties = "spring.grpc.client.default-channel.address=0.0.0.0:9090")
59+
@AutoConfigureInProcessTransport
60+
class SpecificAutowiredClients {
61+
62+
@Autowired
63+
private ApplicationContext context;
64+
65+
@Test
66+
void stubOfCorrectTypeIsCreated() {
67+
assertThat(context.containsBeanDefinition("simpleFutureStub")).isTrue();
68+
assertThat(context.getBean(SimpleGrpc.SimpleFutureStub.class)).isNotNull();
69+
assertThat(context.containsBeanDefinition("simpleStub")).isFalse();
70+
assertThat(context.containsBeanDefinition("simpleBlockingStub")).isFalse();
71+
assertThat(context.getBeanNamesForType(AbstractStub.class)).hasSize(1);
72+
}
73+
74+
@TestConfiguration
75+
@ImportGrpcClients(basePackageClasses = SimpleGrpc.class, factory = FutureStubFactory.class)
76+
static class TestConfig {
77+
78+
}
79+
80+
}
81+
82+
}

spring-grpc-core/src/main/java/org/springframework/grpc/client/BlockingStubFactory.java

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public BlockingStubFactory() {
2525
super(AbstractBlockingStub.class);
2626
}
2727

28+
@Override
29+
public boolean supports(Class<?> type) {
30+
return super.supports(type) && !type.getSimpleName().contains("BlockingV2");
31+
}
32+
2833
@Override
2934
public int getOrder() {
3035
return SimpleStubFactory.SIMPLE_STUB_ORDER - 30;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.grpc.client;
17+
18+
import org.springframework.core.Ordered;
19+
20+
import io.grpc.stub.AbstractBlockingStub;
21+
22+
public class BlockingV2StubFactory extends AbstractStubFactory<AbstractBlockingStub<?>> implements Ordered {
23+
24+
public BlockingV2StubFactory() {
25+
super(AbstractBlockingStub.class);
26+
}
27+
28+
@Override
29+
public boolean supports(Class<?> type) {
30+
return super.supports(type) && type.getSimpleName().contains("BlockingV2");
31+
}
32+
33+
@Override
34+
public int getOrder() {
35+
return SimpleStubFactory.SIMPLE_STUB_ORDER - 30;
36+
}
37+
38+
@Override
39+
protected String methodName() {
40+
return "newBlockingStub";
41+
}
42+
43+
}

spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientRegistry.java

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class GrpcClientRegistry {
5656
public GrpcClientRegistry(GenericApplicationContext context) {
5757
this.context = context;
5858
stubs(new BlockingStubFactory());
59+
stubs(new BlockingV2StubFactory());
5960
stubs(new FutureStubFactory());
6061
stubs(new ReactorStubFactory());
6162
stubs(new SimpleStubFactory());

0 commit comments

Comments
 (0)