|
| 1 | +/* |
| 2 | + * Copyright 2025 The gRPC 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 | + * http://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 | + |
| 17 | +package io.grpc.xds; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import io.grpc.CallOptions; |
| 22 | +import io.grpc.ConnectivityState; |
| 23 | +import io.grpc.EquivalentAddressGroup; |
| 24 | +import io.grpc.LoadBalancer; |
| 25 | +import io.grpc.LoadBalancer.ResolvedAddresses; |
| 26 | +import io.grpc.LoadBalancer.SubchannelPicker; |
| 27 | +import io.grpc.ManagedChannel; |
| 28 | +import io.grpc.Metadata; |
| 29 | +import io.grpc.SynchronizationContext; |
| 30 | +import io.grpc.internal.PickSubchannelArgsImpl; |
| 31 | +import io.grpc.testing.TestMethodDescriptors; |
| 32 | +import java.util.Arrays; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runners.JUnit4; |
| 36 | + |
| 37 | +/** Unit test for {@link io.grpc.xds.LazyLoadBalancer}. */ |
| 38 | +@RunWith(JUnit4.class) |
| 39 | +public final class LazyLoadBalancerTest { |
| 40 | + private SynchronizationContext syncContext = |
| 41 | + new SynchronizationContext((t, e) -> { |
| 42 | + throw new AssertionError(e); |
| 43 | + }); |
| 44 | + private LoadBalancer.PickSubchannelArgs args = new PickSubchannelArgsImpl( |
| 45 | + TestMethodDescriptors.voidMethod(), |
| 46 | + new Metadata(), |
| 47 | + CallOptions.DEFAULT, |
| 48 | + new LoadBalancer.PickDetailsConsumer() {}); |
| 49 | + private FakeHelper helper = new FakeHelper(); |
| 50 | + |
| 51 | + @Test |
| 52 | + public void pickerIsNoopAfterEarlyShutdown() { |
| 53 | + LazyLoadBalancer lb = new LazyLoadBalancer(helper, new LoadBalancer.Factory() { |
| 54 | + @Override |
| 55 | + public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) { |
| 56 | + throw new AssertionError("unexpected"); |
| 57 | + } |
| 58 | + }); |
| 59 | + lb.acceptResolvedAddresses(ResolvedAddresses.newBuilder() |
| 60 | + .setAddresses(Arrays.asList()) |
| 61 | + .build()); |
| 62 | + SubchannelPicker picker = helper.picker; |
| 63 | + assertThat(picker).isNotNull(); |
| 64 | + lb.shutdown(); |
| 65 | + |
| 66 | + picker.pickSubchannel(args); |
| 67 | + } |
| 68 | + |
| 69 | + class FakeHelper extends LoadBalancer.Helper { |
| 70 | + ConnectivityState state; |
| 71 | + SubchannelPicker picker; |
| 72 | + |
| 73 | + @Override |
| 74 | + public ManagedChannel createOobChannel(EquivalentAddressGroup eag, String authority) { |
| 75 | + throw new UnsupportedOperationException(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void updateBalancingState(ConnectivityState newState, SubchannelPicker newPicker) { |
| 80 | + this.state = newState; |
| 81 | + this.picker = newPicker; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public SynchronizationContext getSynchronizationContext() { |
| 86 | + return syncContext; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String getAuthority() { |
| 91 | + return "localhost"; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments