|
11 | 11 | import static org.mockito.ArgumentMatchers.anyString; |
12 | 12 | import static org.mockito.Mockito.inOrder; |
13 | 13 | import static org.mockito.Mockito.mock; |
| 14 | +import static org.mockito.Mockito.never; |
14 | 15 | import static org.mockito.Mockito.times; |
15 | 16 | import static org.mockito.Mockito.verify; |
16 | 17 | import static org.mockito.Mockito.verifyNoInteractions; |
|
23 | 24 | import io.grpc.ManagedChannel; |
24 | 25 | import io.grpc.ManagedChannelBuilder; |
25 | 26 | import java.util.List; |
| 27 | +import java.util.Map; |
26 | 28 | import java.util.concurrent.TimeUnit; |
27 | 29 | import org.junit.jupiter.api.BeforeEach; |
28 | 30 | import org.junit.jupiter.api.Test; |
@@ -145,6 +147,56 @@ void copyConstructorReusesExistingChannels() { |
145 | 147 | assertSame(firstChannel, new GrpcChannelRegistry(firstRegistry).forSecureAddress("foo", 1000)); |
146 | 148 | } |
147 | 149 |
|
| 150 | + @Test |
| 151 | + void appliesServiceConfigAndEnablesRetryWhenServiceConfigIsSet() { |
| 152 | + try (MockedStatic<ManagedChannelBuilder> mockedBuilderStatic = |
| 153 | + Mockito.mockStatic(ManagedChannelBuilder.class)) { |
| 154 | + ManagedChannelBuilder<?> mockBuilder = mock(ManagedChannelBuilder.class); |
| 155 | + ManagedChannel mockChannel = mock(ManagedChannel.class); |
| 156 | + |
| 157 | + mockedBuilderStatic |
| 158 | + .when(() -> ManagedChannelBuilder.forAddress("foo", 1000)) |
| 159 | + .thenAnswer( |
| 160 | + invocation -> { |
| 161 | + when(mockBuilder.intercept(anyList())).then(RETURNS_SELF); |
| 162 | + when(mockBuilder.defaultServiceConfig(any())).then(RETURNS_SELF); |
| 163 | + when(mockBuilder.enableRetry()).then(RETURNS_SELF); |
| 164 | + when(mockBuilder.build()).thenReturn(mockChannel); |
| 165 | + return mockBuilder; |
| 166 | + }); |
| 167 | + |
| 168 | + Map<String, Object> serviceConfig = Map.of("methodConfig", List.of()); |
| 169 | + this.channelRegistry.forPlaintextAddress( |
| 170 | + "foo", 1000, GrpcChannelConfig.builder().serviceConfig(serviceConfig).build()); |
| 171 | + |
| 172 | + verify(mockBuilder, times(1)).defaultServiceConfig(serviceConfig); |
| 173 | + verify(mockBuilder, times(1)).enableRetry(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void doesNotApplyServiceConfigOrEnableRetryWhenServiceConfigIsAbsent() { |
| 179 | + try (MockedStatic<ManagedChannelBuilder> mockedBuilderStatic = |
| 180 | + Mockito.mockStatic(ManagedChannelBuilder.class)) { |
| 181 | + ManagedChannelBuilder<?> mockBuilder = mock(ManagedChannelBuilder.class); |
| 182 | + ManagedChannel mockChannel = mock(ManagedChannel.class); |
| 183 | + |
| 184 | + mockedBuilderStatic |
| 185 | + .when(() -> ManagedChannelBuilder.forAddress("foo", 1000)) |
| 186 | + .thenAnswer( |
| 187 | + invocation -> { |
| 188 | + when(mockBuilder.intercept(anyList())).then(RETURNS_SELF); |
| 189 | + when(mockBuilder.build()).thenReturn(mockChannel); |
| 190 | + return mockBuilder; |
| 191 | + }); |
| 192 | + |
| 193 | + this.channelRegistry.forPlaintextAddress("foo", 1000); |
| 194 | + |
| 195 | + verify(mockBuilder, never()).defaultServiceConfig(any()); |
| 196 | + verify(mockBuilder, never()).enableRetry(); |
| 197 | + } |
| 198 | + } |
| 199 | + |
148 | 200 | @Test |
149 | 201 | void registersRegistryInterceptors() { |
150 | 202 | try (MockedStatic<ManagedChannelBuilder> mockedBuilderStatic = |
|
0 commit comments