• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 
20 import static com.google.common.truth.Truth.assertThat;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.verifyNoMoreInteractions;
23 import static org.mockito.Mockito.when;
24 
25 import io.grpc.InsecureChannelCredentials;
26 import io.grpc.internal.ObjectPool;
27 import io.grpc.xds.Bootstrapper.BootstrapInfo;
28 import io.grpc.xds.Bootstrapper.ServerInfo;
29 import io.grpc.xds.EnvoyProtoData.Node;
30 import io.grpc.xds.SharedXdsClientPoolProvider.RefCountedXdsClientObjectPool;
31 import java.util.Collections;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.JUnit4;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnit;
39 import org.mockito.junit.MockitoRule;
40 
41 /** Tests for {@link SharedXdsClientPoolProvider}. */
42 @RunWith(JUnit4.class)
43 public class SharedXdsClientPoolProviderTest {
44 
45   private static final String SERVER_URI = "trafficdirector.googleapis.com";
46   @Rule
47   public final MockitoRule mocks = MockitoJUnit.rule();
48   @SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
49   @Rule
50   public final ExpectedException thrown = ExpectedException.none();
51   private final Node node = Node.newBuilder().setId("SharedXdsClientPoolProviderTest").build();
52 
53   @Mock
54   private Bootstrapper bootstrapper;
55 
56   @Test
noServer()57   public void noServer() throws XdsInitializationException {
58     BootstrapInfo bootstrapInfo =
59         BootstrapInfo.builder().servers(Collections.<ServerInfo>emptyList()).node(node).build();
60     when(bootstrapper.bootstrap()).thenReturn(bootstrapInfo);
61     SharedXdsClientPoolProvider provider = new SharedXdsClientPoolProvider(bootstrapper);
62     thrown.expect(XdsInitializationException.class);
63     thrown.expectMessage("No xDS server provided");
64     provider.getOrCreate();
65     assertThat(provider.get()).isNull();
66   }
67 
68   @Test
sharedXdsClientObjectPool()69   public void sharedXdsClientObjectPool() throws XdsInitializationException {
70     ServerInfo server = ServerInfo.create(SERVER_URI, InsecureChannelCredentials.create());
71     BootstrapInfo bootstrapInfo =
72         BootstrapInfo.builder().servers(Collections.singletonList(server)).node(node).build();
73     when(bootstrapper.bootstrap()).thenReturn(bootstrapInfo);
74 
75     SharedXdsClientPoolProvider provider = new SharedXdsClientPoolProvider(bootstrapper);
76     assertThat(provider.get()).isNull();
77     ObjectPool<XdsClient> xdsClientPool = provider.getOrCreate();
78     verify(bootstrapper).bootstrap();
79     assertThat(provider.getOrCreate()).isSameInstanceAs(xdsClientPool);
80     assertThat(provider.get()).isNotNull();
81     assertThat(provider.get()).isSameInstanceAs(xdsClientPool);
82     verifyNoMoreInteractions(bootstrapper);
83   }
84 
85   @Test
refCountedXdsClientObjectPool_delayedCreation()86   public void refCountedXdsClientObjectPool_delayedCreation() {
87     ServerInfo server = ServerInfo.create(SERVER_URI, InsecureChannelCredentials.create());
88     BootstrapInfo bootstrapInfo =
89         BootstrapInfo.builder().servers(Collections.singletonList(server)).node(node).build();
90     RefCountedXdsClientObjectPool xdsClientPool = new RefCountedXdsClientObjectPool(bootstrapInfo);
91     assertThat(xdsClientPool.getXdsClientForTest()).isNull();
92     XdsClient xdsClient = xdsClientPool.getObject();
93     assertThat(xdsClientPool.getXdsClientForTest()).isNotNull();
94     xdsClientPool.returnObject(xdsClient);
95   }
96 
97   @Test
refCountedXdsClientObjectPool_refCounted()98   public void refCountedXdsClientObjectPool_refCounted() {
99     ServerInfo server = ServerInfo.create(SERVER_URI, InsecureChannelCredentials.create());
100     BootstrapInfo bootstrapInfo =
101         BootstrapInfo.builder().servers(Collections.singletonList(server)).node(node).build();
102     RefCountedXdsClientObjectPool xdsClientPool = new RefCountedXdsClientObjectPool(bootstrapInfo);
103     // getObject once
104     XdsClient xdsClient = xdsClientPool.getObject();
105     assertThat(xdsClient).isNotNull();
106     // getObject twice
107     assertThat(xdsClientPool.getObject()).isSameInstanceAs(xdsClient);
108     // returnObject once
109     assertThat(xdsClientPool.returnObject(xdsClient)).isNull();
110     assertThat(xdsClient.isShutDown()).isFalse();
111     // returnObject twice
112     assertThat(xdsClientPool.returnObject(xdsClient)).isNull();
113     assertThat(xdsClient.isShutDown()).isTrue();
114   }
115 
116   @Test
refCountedXdsClientObjectPool_getObjectCreatesNewInstanceIfAlreadyShutdown()117   public void refCountedXdsClientObjectPool_getObjectCreatesNewInstanceIfAlreadyShutdown() {
118     ServerInfo server = ServerInfo.create(SERVER_URI, InsecureChannelCredentials.create());
119     BootstrapInfo bootstrapInfo =
120         BootstrapInfo.builder().servers(Collections.singletonList(server)).node(node).build();
121     RefCountedXdsClientObjectPool xdsClientPool = new RefCountedXdsClientObjectPool(bootstrapInfo);
122     XdsClient xdsClient1 = xdsClientPool.getObject();
123     assertThat(xdsClientPool.returnObject(xdsClient1)).isNull();
124     assertThat(xdsClient1.isShutDown()).isTrue();
125 
126     XdsClient xdsClient2 = xdsClientPool.getObject();
127     assertThat(xdsClient2).isNotSameInstanceAs(xdsClient1);
128     xdsClientPool.returnObject(xdsClient2);
129   }
130 }
131