Add composite pattern to GrpcChannelBuilderCustomizer
This commit is contained in:
@@ -142,10 +142,11 @@ public final class ChannelBuilderOptions {
|
||||
* @return a new immutable options instance populated with the specified
|
||||
* {@code customizer} and the settings of this current options instance.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends ManagedChannelBuilder<T>> ChannelBuilderOptions withCustomizer(
|
||||
GrpcChannelBuilderCustomizer<T> customizer) {
|
||||
return new ChannelBuilderOptions(this.interceptors, this.mergeWithGlobalInterceptors, this.shutdownGracePeriod,
|
||||
customizer);
|
||||
this.customizer.then(customizer));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,6 +37,13 @@ public interface GrpcChannelBuilderCustomizer<T extends ManagedChannelBuilder<T>
|
||||
*/
|
||||
void customize(String authority, T builder);
|
||||
|
||||
default GrpcChannelBuilderCustomizer<T> then(GrpcChannelBuilderCustomizer<T> other) {
|
||||
return (authority, builder) -> {
|
||||
customize(authority, builder);
|
||||
other.customize(authority, builder);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to indicate no customizations should be made to the builder.
|
||||
* @param <T> type of channel builder
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.grpc.client;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
|
||||
import java.time.Duration;
|
||||
@@ -26,6 +27,7 @@ import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import io.grpc.ClientInterceptor;
|
||||
import io.grpc.ManagedChannelBuilder;
|
||||
import io.grpc.netty.NettyChannelBuilder;
|
||||
|
||||
/**
|
||||
@@ -56,7 +58,18 @@ class ChannelBuilderOptionsTests {
|
||||
assertThat(options.interceptors()).containsExactly(interceptor1, interceptor2);
|
||||
assertThat(options.mergeWithGlobalInterceptors()).isTrue();
|
||||
assertThat(options.shutdownGracePeriod()).isEqualTo(Duration.ofMinutes(1));
|
||||
assertThat(options.customizer()).isSameAs(customizer);
|
||||
assertThat(options.customizer()).isNotEqualTo(GrpcChannelBuilderCustomizer.defaults());
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@Test
|
||||
void customizerApplied() {
|
||||
ManagedChannelBuilder builder = mock();
|
||||
GrpcChannelBuilderCustomizer customizer = mock();
|
||||
var options = ChannelBuilderOptions.defaults().withCustomizer(customizer);
|
||||
var applied = options.<ManagedChannelBuilder>customizer();
|
||||
applied.customize("localhost", builder);
|
||||
verify(customizer).customize("localhost", builder);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user