forked from Netflix/eureka
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscoveryClientRegistryTest.java
More file actions
371 lines (309 loc) · 17.5 KB
/
DiscoveryClientRegistryTest.java
File metadata and controls
371 lines (309 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package com.netflix.discovery;
import jakarta.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import com.netflix.appinfo.DataCenterInfo;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.discovery.junit.resource.DiscoveryClientResource;
import com.netflix.discovery.shared.Applications;
import com.netflix.discovery.shared.transport.EurekaHttpClient;
import com.netflix.discovery.shared.transport.EurekaHttpResponse;
import com.netflix.discovery.shared.transport.SimpleEurekaHttpServer;
import com.netflix.discovery.util.InstanceInfoGenerator;
import com.netflix.spectator.api.Counter;
import com.netflix.spectator.api.DefaultRegistry;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Spectator;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import static com.netflix.discovery.shared.transport.EurekaHttpResponse.anEurekaHttpResponse;
import static com.netflix.discovery.util.EurekaEntityFunctions.copyApplications;
import static com.netflix.discovery.util.EurekaEntityFunctions.countInstances;
import static com.netflix.discovery.util.EurekaEntityFunctions.mergeApplications;
import static com.netflix.discovery.util.EurekaEntityFunctions.takeFirst;
import static com.netflix.discovery.util.EurekaEntityFunctions.toApplications;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Nitesh Kant
*/
public class DiscoveryClientRegistryTest {
private static final String TEST_LOCAL_REGION = "us-east-1";
private static final String TEST_REMOTE_REGION = "us-west-2";
private static final String TEST_REMOTE_ZONE = "us-west-2c";
private static final EurekaHttpClient requestHandler = mock(EurekaHttpClient.class);
private static SimpleEurekaHttpServer eurekaHttpServer;
private static DefaultRegistry testRegistry;
@Rule
public DiscoveryClientResource discoveryClientResource = DiscoveryClientResource.newBuilder()
.withRegistration(false)
.withRegistryFetch(true)
.withRemoteRegions(TEST_REMOTE_REGION)
.connectWith(eurekaHttpServer)
.build();
/**
* Share server stub by all tests.
*/
@BeforeClass
public static void setUpClass() throws IOException {
eurekaHttpServer = new SimpleEurekaHttpServer(requestHandler);
testRegistry = new DefaultRegistry();
Spectator.globalRegistry().add(testRegistry);
}
@AfterClass
public static void tearDownClass() throws Exception {
if (eurekaHttpServer != null) {
eurekaHttpServer.shutdown();
}
if (testRegistry != null) {
Spectator.globalRegistry().remove(testRegistry);
}
}
@Before
public void setUp() throws Exception {
reset(requestHandler);
when(requestHandler.cancel(anyString(), anyString())).thenReturn(EurekaHttpResponse.status(200));
when(requestHandler.getDelta()).thenReturn(
anEurekaHttpResponse(200, new Applications()).type(MediaType.APPLICATION_JSON_TYPE).build()
);
}
@Test
public void testGetByVipInLocalRegion() throws Exception {
Applications applications = InstanceInfoGenerator.newBuilder(4, "app1", "app2").build().toApplications();
InstanceInfo instance = applications.getRegisteredApplications("app1").getInstances().get(0);
when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, applications).type(MediaType.APPLICATION_JSON_TYPE).build()
);
List<InstanceInfo> result = discoveryClientResource.getClient().getInstancesByVipAddress(instance.getVIPAddress(), false);
assertThat(result.size(), is(equalTo(2)));
assertThat(result.get(0).getVIPAddress(), is(equalTo(instance.getVIPAddress())));
}
@Test
public void testGetAllKnownRegions() throws Exception {
prepareRemoteRegionRegistry();
EurekaClient client = discoveryClientResource.getClient();
Set<String> allKnownRegions = client.getAllKnownRegions();
assertThat(allKnownRegions.size(), is(equalTo(2)));
assertThat(allKnownRegions, hasItem(TEST_REMOTE_REGION));
}
@Test
public void testAllAppsForRegions() throws Exception {
prepareRemoteRegionRegistry();
EurekaClient client = discoveryClientResource.getClient();
Applications appsForRemoteRegion = client.getApplicationsForARegion(TEST_REMOTE_REGION);
assertThat(countInstances(appsForRemoteRegion), is(equalTo(4)));
Applications appsForLocalRegion = client.getApplicationsForARegion(TEST_LOCAL_REGION);
assertThat(countInstances(appsForLocalRegion), is(equalTo(4)));
}
@Test
public void testCacheRefreshSingleAppForLocalRegion() throws Exception {
InstanceInfoGenerator instanceGen = InstanceInfoGenerator.newBuilder(2, "testApp").build();
Applications initialApps = instanceGen.takeDelta(1);
String vipAddress = initialApps.getRegisteredApplications().get(0).getInstances().get(0).getVIPAddress();
DiscoveryClientResource vipClientResource = discoveryClientResource.fork().withVipFetch(vipAddress).build();
// Take first portion
when(requestHandler.getVip(vipAddress, TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, initialApps).type(MediaType.APPLICATION_JSON_TYPE).build()
);
EurekaClient vipClient = vipClientResource.getClient();
assertThat(countInstances(vipClient.getApplications()), is(equalTo(1)));
// Now second one
when(requestHandler.getVip(vipAddress, TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, instanceGen.toApplications()).type(MediaType.APPLICATION_JSON_TYPE).build()
);
assertThat(vipClientResource.awaitCacheUpdate(5, TimeUnit.SECONDS), is(true));
assertThat(countInstances(vipClient.getApplications()), is(equalTo(2)));
}
@Test
public void testEurekaClientPeriodicHeartbeat() throws Exception {
DiscoveryClientResource registeringClientResource = discoveryClientResource.fork().withRegistration(true).withRegistryFetch(false).build();
InstanceInfo instance = registeringClientResource.getMyInstanceInfo();
when(requestHandler.register(any(InstanceInfo.class))).thenReturn(EurekaHttpResponse.status(204));
when(requestHandler.sendHeartBeat(instance.getAppName(), instance.getId(), null, null)).thenReturn(anEurekaHttpResponse(200, InstanceInfo.class).build());
registeringClientResource.getClient(); // Initialize
verify(requestHandler, timeout(5 * 1000).atLeast(2)).sendHeartBeat(instance.getAppName(), instance.getId(), null, null);
}
@Test
public void testEurekaClientPeriodicCacheRefresh() throws Exception {
InstanceInfoGenerator instanceGen = InstanceInfoGenerator.newBuilder(3, 1).build();
Applications initialApps = instanceGen.takeDelta(1);
// Full fetch
when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, initialApps).type(MediaType.APPLICATION_JSON_TYPE).build()
);
EurekaClient client = discoveryClientResource.getClient();
assertThat(countInstances(client.getApplications()), is(equalTo(1)));
// Delta 1
when(requestHandler.getDelta(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, instanceGen.takeDelta(1)).type(MediaType.APPLICATION_JSON_TYPE).build()
);
assertThat(discoveryClientResource.awaitCacheUpdate(5, TimeUnit.SECONDS), is(true));
// Delta 2
when(requestHandler.getDelta(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, instanceGen.takeDelta(1)).type(MediaType.APPLICATION_JSON_TYPE).build()
);
assertThat(discoveryClientResource.awaitCacheUpdate(5, TimeUnit.SECONDS), is(true));
assertThat(countInstances(client.getApplications()), is(equalTo(3)));
}
@Test
public void testGetInvalidVIP() throws Exception {
Applications applications = InstanceInfoGenerator.newBuilder(1, "testApp").build().toApplications();
when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, applications).type(MediaType.APPLICATION_JSON_TYPE).build()
);
EurekaClient client = discoveryClientResource.getClient();
assertThat(countInstances(client.getApplications()), is(equalTo(1)));
List<InstanceInfo> instancesByVipAddress = client.getInstancesByVipAddress("XYZ", false);
assertThat(instancesByVipAddress.isEmpty(), is(true));
}
@Test
public void testGetInvalidVIPForRemoteRegion() throws Exception {
prepareRemoteRegionRegistry();
EurekaClient client = discoveryClientResource.getClient();
List<InstanceInfo> instancesByVipAddress = client.getInstancesByVipAddress("XYZ", false, TEST_REMOTE_REGION);
assertThat(instancesByVipAddress.isEmpty(), is(true));
}
@Test
public void testGetByVipInRemoteRegion() throws Exception {
prepareRemoteRegionRegistry();
EurekaClient client = discoveryClientResource.getClient();
String vipAddress = takeFirst(client.getApplicationsForARegion(TEST_REMOTE_REGION)).getVIPAddress();
List<InstanceInfo> instancesByVipAddress = client.getInstancesByVipAddress(vipAddress, false, TEST_REMOTE_REGION);
assertThat(instancesByVipAddress.size(), is(equalTo(2)));
InstanceInfo instance = instancesByVipAddress.iterator().next();
assertThat(instance.getVIPAddress(), is(equalTo(vipAddress)));
}
@Test
public void testAppsHashCodeAfterRefresh() throws Exception {
InstanceInfoGenerator instanceGen = InstanceInfoGenerator.newBuilder(2, "testApp").build();
// Full fetch with one item
InstanceInfo first = instanceGen.first();
Applications initial = toApplications(first);
when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, initial).type(MediaType.APPLICATION_JSON_TYPE).build()
);
EurekaClient client = discoveryClientResource.getClient();
assertThat(client.getApplications().getAppsHashCode(), is(equalTo("UP_1_")));
// Delta with one add
InstanceInfo second = new InstanceInfo.Builder(instanceGen.take(1)).setStatus(InstanceStatus.DOWN).build();
Applications delta = toApplications(second);
delta.setAppsHashCode("DOWN_1_UP_1_");
when(requestHandler.getDelta(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, delta).type(MediaType.APPLICATION_JSON_TYPE).build()
);
assertThat(discoveryClientResource.awaitCacheUpdate(5, TimeUnit.SECONDS), is(true));
assertThat(client.getApplications().getAppsHashCode(), is(equalTo("DOWN_1_UP_1_")));
}
@Test
public void testApplyDeltaWithBadInstanceInfoDataCenterInfoAsNull() throws Exception {
InstanceInfoGenerator instanceGen = InstanceInfoGenerator.newBuilder(2, "testApp").build();
// Full fetch with one item
InstanceInfo first = instanceGen.first();
Applications initial = toApplications(first);
when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, initial).type(MediaType.APPLICATION_JSON_TYPE).build()
);
EurekaClient client = discoveryClientResource.getClient();
assertThat(client.getApplications().getAppsHashCode(), is(equalTo("UP_1_")));
// Delta with one add
InstanceInfo second = new InstanceInfo.Builder(instanceGen.take(1)).setInstanceId("foo1").setStatus(InstanceStatus.DOWN).setDataCenterInfo(null).build();
InstanceInfo third = new InstanceInfo.Builder(instanceGen.take(1)).setInstanceId("foo2").setStatus(InstanceStatus.UP).setDataCenterInfo(new DataCenterInfo() {
@Override
public Name getName() {
return null;
}
}).build();
Applications delta = toApplications(second, third);
delta.setAppsHashCode("DOWN_1_UP_2_");
when(requestHandler.getDelta(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, delta).type(MediaType.APPLICATION_JSON_TYPE).build()
);
assertThat(discoveryClientResource.awaitCacheUpdate(5, TimeUnit.SECONDS), is(true));
assertThat(client.getApplications().getAppsHashCode(), is(equalTo("DOWN_1_UP_2_")));
}
@Test
public void testEurekaClientPeriodicCacheRefreshForDelete() throws Exception {
InstanceInfoGenerator instanceGen = InstanceInfoGenerator.newBuilder(3, 1).build();
Applications initialApps = instanceGen.takeDelta(2);
Applications deltaForDelete = instanceGen.takeDeltaForDelete(true, 1);
when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, initialApps).type(MediaType.APPLICATION_JSON_TYPE).build()
);
EurekaClient client = discoveryClientResource.getClient();
assertThat(countInstances(client.getApplications()), is(equalTo(2)));
when(requestHandler.getDelta(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, deltaForDelete).type(MediaType.APPLICATION_JSON_TYPE).build()
);
assertThat(discoveryClientResource.awaitCacheUpdate(5, TimeUnit.SECONDS), is(true));
assertThat(client.getApplications().getRegisteredApplications().size(), is(equalTo(1)));
assertThat(countInstances(client.getApplications()), is(equalTo(1)));
}
@Test
public void testEurekaClientPeriodicCacheRefreshForDeleteAndNoApplication() throws Exception {
InstanceInfoGenerator instanceGen = InstanceInfoGenerator.newBuilder(3, 1).build();
Applications initialApps = instanceGen.takeDelta(1);
Applications deltaForDelete = instanceGen.takeDeltaForDelete(true, 1);
when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, initialApps).type(MediaType.APPLICATION_JSON_TYPE).build()
);
EurekaClient client = discoveryClientResource.getClient();
assertThat(countInstances(client.getApplications()), is(equalTo(1)));
when(requestHandler.getDelta(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, deltaForDelete).type(MediaType.APPLICATION_JSON_TYPE).build()
);
assertThat(discoveryClientResource.awaitCacheUpdate(5, TimeUnit.SECONDS), is(true));
assertEquals(client.getApplications().getRegisteredApplications(), new ArrayList<>());
}
@Test
public void testLookupMetricsIncremented() throws Exception {
Applications applications = InstanceInfoGenerator.newBuilder(2, "app1").build().toApplications();
InstanceInfo instance = applications.getRegisteredApplications("app1").getInstances().get(0);
String vipAddress = instance.getVIPAddress();
when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, applications).type(MediaType.APPLICATION_JSON_TYPE).build()
);
Registry registry = Spectator.globalRegistry();
Counter vipCounter = registry.counter(
registry.createId("DiscoveryClient_Lookup")
.withTag("id", "getInstancesByVipAddress")
.withTag("class", "DiscoveryClient"));
long initialCount = vipCounter.count();
discoveryClientResource.getClient().getInstancesByVipAddress(vipAddress, false);
assertThat(vipCounter.count(), is(equalTo(initialCount + 1)));
}
/**
* There is a bug, because of which remote registry data structures are not initialized during full registry fetch, only during delta.
*/
private void prepareRemoteRegionRegistry() throws Exception {
Applications localApplications = InstanceInfoGenerator.newBuilder(4, "app1", "app2").build().toApplications();
Applications remoteApplications = InstanceInfoGenerator.newBuilder(4, "remote1", "remote2").withZone(TEST_REMOTE_ZONE).build().toApplications();
Applications allApplications = mergeApplications(localApplications, remoteApplications);
// Load remote data in delta, to go around exiting bug in DiscoveryClient
Applications delta = copyApplications(remoteApplications);
delta.setAppsHashCode(allApplications.getAppsHashCode());
when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, localApplications).type(MediaType.APPLICATION_JSON_TYPE).build()
);
when(requestHandler.getDelta(TEST_REMOTE_REGION)).thenReturn(
anEurekaHttpResponse(200, delta).type(MediaType.APPLICATION_JSON_TYPE).build()
);
assertThat(discoveryClientResource.awaitCacheUpdate(5, TimeUnit.SECONDS), is(true));
}
}