Skip to content

Commit 97ba123

Browse files
committed
Add composite pattern to GrpcChannelBuilderCustomizer
1 parent 3f709fd commit 97ba123

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ public ChannelBuilderOptions withShutdownGracePeriod(Duration shutdownGracePerio
142142
* @return a new immutable options instance populated with the specified
143143
* {@code customizer} and the settings of this current options instance.
144144
*/
145+
@SuppressWarnings("unchecked")
145146
public <T extends ManagedChannelBuilder<T>> ChannelBuilderOptions withCustomizer(
146147
GrpcChannelBuilderCustomizer<T> customizer) {
147148
return new ChannelBuilderOptions(this.interceptors, this.mergeWithGlobalInterceptors, this.shutdownGracePeriod,
148-
customizer);
149+
this.customizer.then(customizer));
149150
}
150151

151152
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public interface GrpcChannelBuilderCustomizer<T extends ManagedChannelBuilder<T>
3737
*/
3838
void customize(String authority, T builder);
3939

40+
default GrpcChannelBuilderCustomizer<T> then(GrpcChannelBuilderCustomizer<T> other) {
41+
return (authority, builder) -> {
42+
customize(authority, builder);
43+
other.customize(authority, builder);
44+
};
45+
}
46+
4047
/**
4148
* Used to indicate no customizations should be made to the builder.
4249
* @param <T> type of channel builder

spring-grpc-core/src/test/java/org/springframework/grpc/client/ChannelBuilderOptionsTests.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.verify;
2021
import static org.mockito.Mockito.verifyNoInteractions;
2122

2223
import java.time.Duration;
@@ -26,6 +27,7 @@
2627
import org.junit.jupiter.api.Test;
2728

2829
import io.grpc.ClientInterceptor;
30+
import io.grpc.ManagedChannelBuilder;
2931
import io.grpc.netty.NettyChannelBuilder;
3032

3133
/**
@@ -56,7 +58,18 @@ void userSpecifiedOptions() {
5658
assertThat(options.interceptors()).containsExactly(interceptor1, interceptor2);
5759
assertThat(options.mergeWithGlobalInterceptors()).isTrue();
5860
assertThat(options.shutdownGracePeriod()).isEqualTo(Duration.ofMinutes(1));
59-
assertThat(options.customizer()).isSameAs(customizer);
61+
assertThat(options.customizer()).isNotEqualTo(GrpcChannelBuilderCustomizer.defaults());
62+
}
63+
64+
@SuppressWarnings({ "rawtypes", "unchecked" })
65+
@Test
66+
void customizerApplied() {
67+
ManagedChannelBuilder builder = mock();
68+
GrpcChannelBuilderCustomizer customizer = mock();
69+
var options = ChannelBuilderOptions.defaults().withCustomizer(customizer);
70+
var applied = options.<ManagedChannelBuilder>customizer();
71+
applied.customize("localhost", builder);
72+
verify(customizer).customize("localhost", builder);
6073
}
6174

6275
@Test

0 commit comments

Comments
 (0)