From 39e66fa22b363396dce75caa393f6880d82bb2a0 Mon Sep 17 00:00:00 2001 From: Kun Zhang Date: Wed, 17 Apr 2019 14:46:19 -0700 Subject: [PATCH] core: delete ManagedChannelBuilder.loadBalancerFactory() and all deprecated factories (#5480) This has been deprecated since 1.18.0 --- .../io/grpc/ForwardingChannelBuilder.java | 7 -- .../java/io/grpc/ManagedChannelBuilder.java | 21 ------ .../io/grpc/PickFirstBalancerFactory.java | 56 --------------- .../io/grpc/PickFirstBalancerFactoryTest.java | 41 ----------- .../AbstractManagedChannelImplBuilder.java | 13 ---- .../io/grpc/internal/ManagedChannelImpl.java | 12 +--- .../util/RoundRobinLoadBalancerFactory.java | 61 ----------------- ...AbstractManagedChannelImplBuilderTest.java | 29 -------- .../grpc/internal/ManagedChannelImplTest.java | 68 ------------------- .../RoundRobinLoadBalancerFactoryTest.java | 41 ----------- .../grpclb/GrpclbLoadBalancerFactory.java | 64 ----------------- .../grpclb/GrpclbLoadBalancerFactoryTest.java | 63 ----------------- 12 files changed, 3 insertions(+), 473 deletions(-) delete mode 100644 api/src/main/java/io/grpc/PickFirstBalancerFactory.java delete mode 100644 api/src/test/java/io/grpc/PickFirstBalancerFactoryTest.java delete mode 100644 core/src/main/java/io/grpc/util/RoundRobinLoadBalancerFactory.java delete mode 100644 core/src/test/java/io/grpc/util/RoundRobinLoadBalancerFactoryTest.java delete mode 100644 grpclb/src/main/java/io/grpc/grpclb/GrpclbLoadBalancerFactory.java delete mode 100644 grpclb/src/test/java/io/grpc/grpclb/GrpclbLoadBalancerFactoryTest.java diff --git a/api/src/main/java/io/grpc/ForwardingChannelBuilder.java b/api/src/main/java/io/grpc/ForwardingChannelBuilder.java index 396b808db4..db63180f60 100644 --- a/api/src/main/java/io/grpc/ForwardingChannelBuilder.java +++ b/api/src/main/java/io/grpc/ForwardingChannelBuilder.java @@ -123,13 +123,6 @@ public abstract class ForwardingChannelBuilder> @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1770") public abstract T nameResolverFactory(NameResolver.Factory resolverFactory); - /** - * Provides a custom {@link LoadBalancer.Factory} for the channel. - * - *

If this method is not called, the builder will use {@link PickFirstBalancerFactory} - * for the channel. - * - *

This method is implemented by all stock channel builders that - * are shipped with gRPC, but may not be implemented by custom channel builders, in which case - * this method will throw. - * - * @deprecated this method disables service-config-based policy selection, and may cause problems - * if NameResolver returns GRPCLB balancer addresses but a non-GRPCLB LoadBalancer - * is passed in here. Use {@link #defaultLoadBalancingPolicy} instead. - * - * @return this - * @since 1.0.0 - */ - @Deprecated - @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771") - public abstract T loadBalancerFactory(LoadBalancer.Factory loadBalancerFactory); - /** * Sets the default load-balancing policy that will be used if the service config doesn't specify * one. If not set, the default will be the "pick_first" policy. diff --git a/api/src/main/java/io/grpc/PickFirstBalancerFactory.java b/api/src/main/java/io/grpc/PickFirstBalancerFactory.java deleted file mode 100644 index 258b9c0807..0000000000 --- a/api/src/main/java/io/grpc/PickFirstBalancerFactory.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2015 The gRPC 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 - * - * http://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 io.grpc; - -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * A {@link LoadBalancer} that provides no load balancing mechanism over the - * addresses from the {@link NameResolver}. The channel's default behavior - * (currently pick-first) is used for all addresses found. - * - * @deprecated this is the default balancer and should not be referenced to. This will be deleted - * soon. - */ -@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771") -@Deprecated -public final class PickFirstBalancerFactory extends LoadBalancer.Factory { - private static PickFirstBalancerFactory instance; - - private final LoadBalancerProvider provider; - - private PickFirstBalancerFactory() { - provider = checkNotNull( - LoadBalancerRegistry.getDefaultRegistry().getProvider("pick_first"), - "pick_first balancer not available"); - } - - /** - * Gets an instance of this factory. - */ - public static synchronized PickFirstBalancerFactory getInstance() { - if (instance == null) { - instance = new PickFirstBalancerFactory(); - } - return instance; - } - - @Override - public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) { - return provider.newLoadBalancer(helper); - } -} diff --git a/api/src/test/java/io/grpc/PickFirstBalancerFactoryTest.java b/api/src/test/java/io/grpc/PickFirstBalancerFactoryTest.java deleted file mode 100644 index daa45f3084..0000000000 --- a/api/src/test/java/io/grpc/PickFirstBalancerFactoryTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2016 The gRPC 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 - * - * http://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 io.grpc; - -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verifyZeroInteractions; - -import io.grpc.LoadBalancer.Helper; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit test for {@link PickFirstBalancerFactory}. */ -@RunWith(JUnit4.class) -public class PickFirstBalancerFactoryTest { - - @SuppressWarnings("deprecation") - @Test - public void setUp() { - Helper helper = mock(Helper.class); - assertThat( - PickFirstBalancerFactory.getInstance().newLoadBalancer(helper).getClass().getName()) - .isEqualTo("io.grpc.internal.PickFirstLoadBalancer"); - verifyZeroInteractions(helper); - } -} diff --git a/core/src/main/java/io/grpc/internal/AbstractManagedChannelImplBuilder.java b/core/src/main/java/io/grpc/internal/AbstractManagedChannelImplBuilder.java index 0a01bcc7a9..85ba518717 100644 --- a/core/src/main/java/io/grpc/internal/AbstractManagedChannelImplBuilder.java +++ b/core/src/main/java/io/grpc/internal/AbstractManagedChannelImplBuilder.java @@ -28,7 +28,6 @@ import io.grpc.CompressorRegistry; import io.grpc.DecompressorRegistry; import io.grpc.EquivalentAddressGroup; import io.grpc.InternalChannelz; -import io.grpc.LoadBalancer; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import io.grpc.NameResolver; @@ -116,8 +115,6 @@ public abstract class AbstractManagedChannelImplBuilder @Nullable String authorityOverride; - @Nullable LoadBalancer.Factory loadBalancerFactory; - String defaultLbPolicy = GrpcUtil.DEFAULT_LB_POLICY; boolean fullStreamDecompression; @@ -246,16 +243,6 @@ public abstract class AbstractManagedChannelImplBuilder return thisT(); } - @Deprecated - @Override - public final T loadBalancerFactory(LoadBalancer.Factory loadBalancerFactory) { - Preconditions.checkState(directServerAddress == null, - "directServerAddress is set (%s), which forbids the use of LoadBalancer.Factory", - directServerAddress); - this.loadBalancerFactory = loadBalancerFactory; - return thisT(); - } - @Override public final T defaultLoadBalancingPolicy(String policy) { Preconditions.checkState(directServerAddress == null, diff --git a/core/src/main/java/io/grpc/internal/ManagedChannelImpl.java b/core/src/main/java/io/grpc/internal/ManagedChannelImpl.java index 663e2bcfc7..ec3b43e0fd 100644 --- a/core/src/main/java/io/grpc/internal/ManagedChannelImpl.java +++ b/core/src/main/java/io/grpc/internal/ManagedChannelImpl.java @@ -559,14 +559,9 @@ final class ManagedChannelImpl extends ManagedChannel implements ProxyDetector proxyDetector = builder.proxyDetector != null ? builder.proxyDetector : GrpcUtil.getDefaultProxyDetector(); this.retryEnabled = builder.retryEnabled && !builder.temporarilyDisableRetry; - AutoConfiguredLoadBalancerFactory autoConfiguredLoadBalancerFactory = null; - if (builder.loadBalancerFactory == null) { - autoConfiguredLoadBalancerFactory = - new AutoConfiguredLoadBalancerFactory(builder.defaultLbPolicy); - this.loadBalancerFactory = autoConfiguredLoadBalancerFactory; - } else { - this.loadBalancerFactory = builder.loadBalancerFactory; - } + AutoConfiguredLoadBalancerFactory autoConfiguredLoadBalancerFactory = + new AutoConfiguredLoadBalancerFactory(builder.defaultLbPolicy); + this.loadBalancerFactory = autoConfiguredLoadBalancerFactory; this.nameResolverHelper = new NrHelper( builder.getDefaultPort(), @@ -583,7 +578,6 @@ final class ManagedChannelImpl extends ManagedChannel implements logId, builder.maxTraceEvents, timeProvider.currentTimeNanos(), "Channel for '" + target + "'"); channelLogger = new ChannelLoggerImpl(channelTracer, timeProvider); - this.executorPool = checkNotNull(builder.executorPool, "executorPool"); this.balancerRpcExecutorPool = checkNotNull(balancerRpcExecutorPool, "balancerRpcExecutorPool"); this.balancerRpcExecutorHolder = new ExecutorHolder(balancerRpcExecutorPool); diff --git a/core/src/main/java/io/grpc/util/RoundRobinLoadBalancerFactory.java b/core/src/main/java/io/grpc/util/RoundRobinLoadBalancerFactory.java deleted file mode 100644 index 3116aa1157..0000000000 --- a/core/src/main/java/io/grpc/util/RoundRobinLoadBalancerFactory.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2016 The gRPC 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 - * - * http://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 io.grpc.util; - -import static com.google.common.base.Preconditions.checkNotNull; - -import io.grpc.ExperimentalApi; -import io.grpc.LoadBalancer; -import io.grpc.LoadBalancerProvider; -import io.grpc.LoadBalancerRegistry; - -/** - * A {@link LoadBalancer} that provides round-robin load balancing mechanism over the - * addresses. - * - * @deprecated use {@link io.grpc.LoadBalancerRegistry#getProvider} with "round_robin" policy. This - * class will be deleted soon. - */ -@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771") -@Deprecated -public final class RoundRobinLoadBalancerFactory extends LoadBalancer.Factory { - - private static RoundRobinLoadBalancerFactory instance; - - private final LoadBalancerProvider provider; - - private RoundRobinLoadBalancerFactory() { - provider = checkNotNull( - LoadBalancerRegistry.getDefaultRegistry().getProvider("round_robin"), - "round_robin balancer not available"); - } - - /** - * Gets the singleton instance of this factory. - */ - public static synchronized RoundRobinLoadBalancerFactory getInstance() { - if (instance == null) { - instance = new RoundRobinLoadBalancerFactory(); - } - return instance; - } - - @Override - public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) { - return provider.newLoadBalancer(helper); - } -} diff --git a/core/src/test/java/io/grpc/internal/AbstractManagedChannelImplBuilderTest.java b/core/src/test/java/io/grpc/internal/AbstractManagedChannelImplBuilderTest.java index 492c7547c5..3a750e5439 100644 --- a/core/src/test/java/io/grpc/internal/AbstractManagedChannelImplBuilderTest.java +++ b/core/src/test/java/io/grpc/internal/AbstractManagedChannelImplBuilderTest.java @@ -33,7 +33,6 @@ import io.grpc.ClientCall; import io.grpc.ClientInterceptor; import io.grpc.CompressorRegistry; import io.grpc.DecompressorRegistry; -import io.grpc.LoadBalancer; import io.grpc.MethodDescriptor; import io.grpc.NameResolver; import io.grpc.internal.testing.StatsTestUtils.FakeStatsRecorder; @@ -125,34 +124,6 @@ public class AbstractManagedChannelImplBuilderTest { directAddressBuilder.nameResolverFactory(mock(NameResolver.Factory.class)); } - @Test - public void loadBalancerFactory_default() { - assertNull(builder.loadBalancerFactory); - } - - @Test - @Deprecated - public void loadBalancerFactory_normal() { - LoadBalancer.Factory loadBalancerFactory = mock(LoadBalancer.Factory.class); - assertEquals(builder, builder.loadBalancerFactory(loadBalancerFactory)); - assertEquals(loadBalancerFactory, builder.loadBalancerFactory); - } - - @Test - @Deprecated - public void loadBalancerFactory_null() { - LoadBalancer.Factory defaultValue = builder.loadBalancerFactory; - builder.loadBalancerFactory(mock(LoadBalancer.Factory.class)); - assertEquals(builder, builder.loadBalancerFactory(null)); - assertEquals(defaultValue, builder.loadBalancerFactory); - } - - @Test(expected = IllegalStateException.class) - @Deprecated - public void loadBalancerFactory_notAllowedWithDirectAddress() { - directAddressBuilder.loadBalancerFactory(mock(LoadBalancer.Factory.class)); - } - @Test public void defaultLoadBalancingPolicy_default() { assertEquals("pick_first", builder.defaultLbPolicy); diff --git a/core/src/test/java/io/grpc/internal/ManagedChannelImplTest.java b/core/src/test/java/io/grpc/internal/ManagedChannelImplTest.java index d08ae5bc49..c92db8483e 100644 --- a/core/src/test/java/io/grpc/internal/ManagedChannelImplTest.java +++ b/core/src/test/java/io/grpc/internal/ManagedChannelImplTest.java @@ -959,74 +959,6 @@ public class ManagedChannelImplTest { serviceConfig, actualAttrs.get(GrpcAttributes.NAME_RESOLVER_SERVICE_CONFIG)); } - @Test - @Deprecated - public void nameResolverReturnsEmptySubLists_becomeErrorByDefault_lbFactorySetDirectly() - throws Exception { - String errorDescription = "returned an empty list"; - - // Pass a FakeNameResolverFactory with an empty list and LB config - FakeNameResolverFactory nameResolverFactory = - new FakeNameResolverFactory.Builder(expectedUri).build(); - Map serviceConfig = - parseConfig("{\"loadBalancingConfig\": [ {\"mock_lb\": { \"setting1\": \"high\" } } ] }"); - Attributes serviceConfigAttrs = - Attributes.newBuilder() - .set(GrpcAttributes.NAME_RESOLVER_SERVICE_CONFIG, serviceConfig) - .build(); - nameResolverFactory.nextResolvedAttributes.set(serviceConfigAttrs); - channelBuilder.nameResolverFactory(nameResolverFactory); - - // Pass a LoadBalancerFactory directly to the builder, bypassing - // AutoConfiguredLoadBalancerFactory. The empty-list check is done in ManagedChannelImpl rather - // than AutoConfiguredLoadBalancerFactory - channelBuilder.loadBalancerFactory(mockLoadBalancerProvider); - - createChannel(); - - // LoadBalancer received the error - verify(mockLoadBalancerProvider).newLoadBalancer(any(Helper.class)); - verify(mockLoadBalancer).handleNameResolutionError(statusCaptor.capture()); - Status status = statusCaptor.getValue(); - assertSame(Status.Code.UNAVAILABLE, status.getCode()); - Truth.assertThat(status.getDescription()).contains(errorDescription); - } - - @Test - @Deprecated - public void nameResolverReturnsEmptySubLists_optionallyAllowed_lbFactorySetDirectly() - throws Exception { - when(mockLoadBalancer.canHandleEmptyAddressListFromNameResolution()).thenReturn(true); - - // Pass a FakeNameResolverFactory with an empty list and LB config - FakeNameResolverFactory nameResolverFactory = - new FakeNameResolverFactory.Builder(expectedUri).build(); - Map serviceConfig = - parseConfig("{\"loadBalancingConfig\": [ {\"mock_lb\": { \"setting1\": \"high\" } } ] }"); - Attributes serviceConfigAttrs = - Attributes.newBuilder() - .set(GrpcAttributes.NAME_RESOLVER_SERVICE_CONFIG, serviceConfig) - .build(); - nameResolverFactory.nextResolvedAttributes.set(serviceConfigAttrs); - channelBuilder.nameResolverFactory(nameResolverFactory); - - // Pass a LoadBalancerFactory directly to the builder, bypassing - // AutoConfiguredLoadBalancerFactory. The empty-list check is done in ManagedChannelImpl rather - // than AutoConfiguredLoadBalancerFactory - channelBuilder.loadBalancerFactory(mockLoadBalancerProvider); - - createChannel(); - - // LoadBalancer received the empty list and the LB config - verify(mockLoadBalancerProvider).newLoadBalancer(any(Helper.class)); - verify(mockLoadBalancer).handleResolvedAddresses( - ResolvedAddresses.newBuilder() - .setAddresses(Collections.emptyList()) - .setAttributes(serviceConfigAttrs) - .build()); - - } - @Test public void loadBalancerThrowsInHandleResolvedAddresses() { RuntimeException ex = new RuntimeException("simulated"); diff --git a/core/src/test/java/io/grpc/util/RoundRobinLoadBalancerFactoryTest.java b/core/src/test/java/io/grpc/util/RoundRobinLoadBalancerFactoryTest.java deleted file mode 100644 index 53b5263c8b..0000000000 --- a/core/src/test/java/io/grpc/util/RoundRobinLoadBalancerFactoryTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2016 The gRPC 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 - * - * http://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 io.grpc.util; - -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verifyZeroInteractions; - -import io.grpc.LoadBalancer.Helper; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit test for {@link RoundRobinLoadBalancerFactory}. */ -@RunWith(JUnit4.class) -public class RoundRobinLoadBalancerFactoryTest { - - @SuppressWarnings("deprecation") - @Test - public void getInstance() { - Helper helper = mock(Helper.class); - assertThat( - RoundRobinLoadBalancerFactory.getInstance().newLoadBalancer(helper).getClass().getName()) - .isEqualTo("io.grpc.util.RoundRobinLoadBalancer"); - verifyZeroInteractions(helper); - } -} diff --git a/grpclb/src/main/java/io/grpc/grpclb/GrpclbLoadBalancerFactory.java b/grpclb/src/main/java/io/grpc/grpclb/GrpclbLoadBalancerFactory.java deleted file mode 100644 index 80ced90886..0000000000 --- a/grpclb/src/main/java/io/grpc/grpclb/GrpclbLoadBalancerFactory.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2017 The gRPC 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 - * - * http://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 io.grpc.grpclb; - -import static com.google.common.base.Preconditions.checkNotNull; - -import io.grpc.ExperimentalApi; -import io.grpc.LoadBalancer; -import io.grpc.LoadBalancerProvider; -import io.grpc.LoadBalancerRegistry; - -/** - * A factory for {@link LoadBalancer}s that uses the GRPCLB protocol. - * - *

Experimental:This only works with the GRPCLB load-balancer service, which is not - * available yet. Right now it's only good for internal testing. - * - * @deprecated The "grpclb" policy will be selected when environment is set up correctly, thus no - * need to directly reference the factory. If explicit selection is needed, use {@link - * io.grpc.LoadBalancerRegistry#getProvider} with "grpclb" policy. This class will be - * deleted soon. - */ -@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1782") -@Deprecated -public final class GrpclbLoadBalancerFactory extends LoadBalancer.Factory { - - private static GrpclbLoadBalancerFactory instance; - private final LoadBalancerProvider provider; - - private GrpclbLoadBalancerFactory() { - provider = checkNotNull( - LoadBalancerRegistry.getDefaultRegistry().getProvider("grpclb"), - "grpclb balancer not available"); - } - - /** - * Returns the instance. - */ - public static GrpclbLoadBalancerFactory getInstance() { - if (instance == null) { - instance = new GrpclbLoadBalancerFactory(); - } - return instance; - } - - @Override - public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) { - return provider.newLoadBalancer(helper); - } -} diff --git a/grpclb/src/test/java/io/grpc/grpclb/GrpclbLoadBalancerFactoryTest.java b/grpclb/src/test/java/io/grpc/grpclb/GrpclbLoadBalancerFactoryTest.java deleted file mode 100644 index 7f937e1426..0000000000 --- a/grpclb/src/test/java/io/grpc/grpclb/GrpclbLoadBalancerFactoryTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2016 The gRPC 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 - * - * http://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 io.grpc.grpclb; - -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -import io.grpc.ChannelLogger; -import io.grpc.LoadBalancer.Helper; -import io.grpc.SynchronizationContext; -import io.grpc.internal.FakeClock; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit test for {@link GrpclbLoadBalancerFactory}. */ -@RunWith(JUnit4.class) -public class GrpclbLoadBalancerFactoryTest { - private final FakeClock clock = new FakeClock(); - private final SynchronizationContext syncContext = new SynchronizationContext( - new Thread.UncaughtExceptionHandler() { - @Override - public void uncaughtException(Thread t, Throwable e) { - throw new AssertionError(e); - } - }); - - @SuppressWarnings("deprecation") - @Test - public void getInstance() { - Helper helper = mock(Helper.class); - when(helper.getSynchronizationContext()).thenReturn(syncContext); - when(helper.getScheduledExecutorService()).thenReturn(clock.getScheduledExecutorService()); - when(helper.getAuthority()).thenReturn("fakeauthority"); - when(helper.getChannelLogger()).thenReturn(mock(ChannelLogger.class)); - - assertThat(GrpclbLoadBalancerFactory.getInstance().newLoadBalancer(helper)) - .isInstanceOf(io.grpc.grpclb.GrpclbLoadBalancer.class); - - verify(helper).getSynchronizationContext(); - verify(helper).getScheduledExecutorService(); - verify(helper).getAuthority(); - verify(helper).getChannelLogger(); - verifyNoMoreInteractions(helper); - } -}