-
Notifications
You must be signed in to change notification settings - Fork 68
/
FeatureRepositoryTest.java
528 lines (451 loc) · 22.4 KB
/
FeatureRepositoryTest.java
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package io.getunleash.repository;
import io.getunleash.*;
import io.getunleash.event.EventDispatcher;
import io.getunleash.lang.Nullable;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static io.getunleash.repository.FeatureToggleResponse.Status.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
public class FeatureRepositoryTest {
FeatureBackupHandlerFile backupHandler;
FeatureBootstrapHandler bootstrapHandler;
HttpFeatureFetcher fetcher;
UnleashConfig defaultConfig;
@BeforeEach
public void setUp() {
backupHandler = mock(FeatureBackupHandlerFile.class);
bootstrapHandler = mock(FeatureBootstrapHandler.class);
fetcher = mock(HttpFeatureFetcher.class);
defaultConfig =
new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.fetchTogglesInterval(200L)
.synchronousFetchOnInitialisation(false)
.build();
}
@Test
public void no_backup_file_and_no_repository_available_should_give_empty_repo() {
UnleashScheduledExecutor executor = mock(UnleashScheduledExecutor.class);
UnleashConfig config =
UnleashConfig.builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.scheduledExecutor(executor)
.build();
when(backupHandler.read()).thenReturn(new FeatureCollection());
when(bootstrapHandler.read()).thenReturn(new FeatureCollection());
FeatureRepository featureRepository = new FeatureRepository(config);
assertNull(featureRepository.getToggle("unknownFeature"), "should be null");
}
@Test
public void backup_toggles_should_be_loaded_at_startup() {
UnleashScheduledExecutor executor = mock(UnleashScheduledExecutor.class);
UnleashConfig config =
UnleashConfig.builder()
.appName("test")
.scheduledExecutor(executor)
.unleashAPI("http://localhost:4242/api/")
.fetchTogglesInterval(Long.MAX_VALUE)
.build();
when(backupHandler.read())
.thenReturn(
new FeatureCollection(
new ToggleCollection(Collections.emptyList()),
new SegmentCollection(Collections.emptyList())));
new FeatureRepository(
config, backupHandler, new EventDispatcher(config), fetcher, bootstrapHandler);
verify(backupHandler, times(1)).read();
}
@Test
public void feature_toggles_should_be_updated() {
UnleashScheduledExecutor executor = mock(UnleashScheduledExecutor.class);
ArgumentCaptor<Runnable> runnableArgumentCaptor = ArgumentCaptor.forClass(Runnable.class);
UnleashConfig config =
new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.scheduledExecutor(executor)
.fetchTogglesInterval(200L)
.synchronousFetchOnInitialisation(false)
.build();
when(backupHandler.read()).thenReturn(simpleFeatureCollection(false));
FeatureRepository featureRepository =
new FeatureRepository(config, backupHandler, executor, fetcher, bootstrapHandler);
// run the toggleName fetcher callback
verify(executor).setInterval(runnableArgumentCaptor.capture(), anyLong(), anyLong());
verify(fetcher, times(0)).fetchFeatures();
ClientFeaturesResponse response = new ClientFeaturesResponse(CHANGED, simpleFeatureCollection(true));
when(fetcher.fetchFeatures()).thenReturn(response);
runnableArgumentCaptor.getValue().run();
verify(backupHandler, times(1)).read();
verify(fetcher, times(1)).fetchFeatures();
assertTrue(featureRepository.getToggle("toggleFetcherCalled").isEnabled());
}
@Test
public void get_feature_names_should_return_list_of_names() {
FeatureCollection featureCollection =
populatedFeatureCollection(
null,
new FeatureToggle(
"toggleFeatureName1",
true,
Arrays.asList(new ActivationStrategy("custom", null))),
new FeatureToggle(
"toggleFeatureName2",
true,
Arrays.asList(new ActivationStrategy("custom", null))));
when(backupHandler.read()).thenReturn(featureCollection);
FeatureRepository featureRepository =
new FeatureRepository(
defaultConfig,
backupHandler,
new EventDispatcher(defaultConfig),
fetcher,
bootstrapHandler);
assertEquals(2, featureRepository.getFeatureNames().size());
assertEquals("toggleFeatureName2", featureRepository.getFeatureNames().get(1));
}
@Test
public void should_perform_synchronous_fetch_on_initialisation() {
UnleashScheduledExecutor executor = mock(UnleashScheduledExecutor.class);
UnleashConfig config =
UnleashConfig.builder()
.synchronousFetchOnInitialisation(true)
.scheduledExecutor(executor)
.appName("test-sync-update")
.unleashAPI("http://localhost:8080")
.build();
when(backupHandler.read()).thenReturn(new FeatureCollection());
FeatureCollection featureCollection = populatedFeatureCollection(null);
ClientFeaturesResponse response = new ClientFeaturesResponse(CHANGED, featureCollection);
when(fetcher.fetchFeatures()).thenReturn(response);
new FeatureRepository(
config, backupHandler, new EventDispatcher(config), fetcher, bootstrapHandler);
verify(fetcher, times(1)).fetchFeatures();
}
@Test
public void should_not_perform_synchronous_fetch_on_initialisation() {
UnleashScheduledExecutor executor = mock(UnleashScheduledExecutor.class);
UnleashConfig config =
UnleashConfig.builder()
.synchronousFetchOnInitialisation(false)
.scheduledExecutor(executor)
.appName("test-sync-update")
.unleashAPI("http://localhost:8080")
.build();
when(backupHandler.read()).thenReturn(new FeatureCollection());
FeatureCollection featureCollection = populatedFeatureCollection(null);
ClientFeaturesResponse response =
new ClientFeaturesResponse(
CHANGED, featureCollection);
when(fetcher.fetchFeatures()).thenReturn(response);
FeatureRepository featureRepository = new FeatureRepository(config);
verify(fetcher, times(0)).fetchFeatures();
}
private FeatureCollection populatedFeatureCollection(
@Nullable List<Segment> segments, FeatureToggle... featureToggles) {
List<FeatureToggle> toggleList = new ArrayList<>();
toggleList.addAll(Arrays.asList(featureToggles));
List<Segment> segmentList = new ArrayList<>();
if (segments != null) segmentList.addAll(segments);
return new FeatureCollection(
new ToggleCollection(toggleList), new SegmentCollection(segmentList));
}
@Test
public void should_read_from_bootstrap_location_if_backup_was_empty()
throws URISyntaxException, IOException {
File file =
new File(getClass().getClassLoader().getResource("unleash-repo-v2.json").toURI());
ToggleBootstrapProvider toggleBootstrapProvider = mock(ToggleBootstrapProvider.class);
when(toggleBootstrapProvider.read()).thenReturn(fileToString(file));
UnleashScheduledExecutor executor = mock(UnleashScheduledExecutor.class);
UnleashConfig config =
UnleashConfig.builder()
.synchronousFetchOnInitialisation(false)
.appName("test-sync-update")
.scheduledExecutor(executor)
.unleashAPI("http://localhost:8080")
.toggleBootstrapProvider(toggleBootstrapProvider)
.build();
when(backupHandler.read()).thenReturn(new FeatureCollection());
FeatureRepository repo = new FeatureRepository(config);
assertThat(repo.getFeatureNames()).hasSize(5);
}
@Test
public void should_not_read_bootstrap_if_backup_was_found()
throws IOException, URISyntaxException {
File file =
new File(getClass().getClassLoader().getResource("unleash-repo-v2.json").toURI());
ToggleBootstrapProvider toggleBootstrapProvider = mock(ToggleBootstrapProvider.class);
UnleashScheduledExecutor executor = mock(UnleashScheduledExecutor.class);
UnleashConfig config =
UnleashConfig.builder()
.synchronousFetchOnInitialisation(false)
.appName("test-sync-update")
.scheduledExecutor(executor)
.unleashAPI("http://localhost:8080")
.toggleBootstrapProvider(toggleBootstrapProvider)
.build();
when(toggleBootstrapProvider.read()).thenReturn(fileToString(file));
when(backupHandler.read())
.thenReturn(
getFeatureCollection());
new FeatureRepository(
config, backupHandler, new EventDispatcher(config), fetcher, bootstrapHandler);
verify(toggleBootstrapProvider, times(0)).read();
}
@ParameterizedTest
@ValueSource(ints = {403, 404})
public void should_increase_to_max_interval_when_code(int code)
throws URISyntaxException, IOException {
TestRunner runner = new TestRunner();
File file =
new File(getClass().getClassLoader().getResource("unleash-repo-v2.json").toURI());
ToggleBootstrapProvider toggleBootstrapProvider = mock(ToggleBootstrapProvider.class);
when(toggleBootstrapProvider.read()).thenReturn(fileToString(file));
UnleashConfig config =
UnleashConfig.builder()
.synchronousFetchOnInitialisation(false)
.appName("test-sync-update")
.scheduledExecutor(runner.executor)
.unleashAPI("http://localhost:8080")
.build();
when(backupHandler.read()).thenReturn(getFeatureCollection());
FeatureRepository featureRepository =
new FeatureRepository(
config,
backupHandler,
new EventDispatcher(config),
fetcher,
bootstrapHandler);
runner.assertThatFetchesAndReceives(CHANGED, 200); // set it ready
runner.assertThatFetchesAndReceives(UNAVAILABLE, code);
assertThat(featureRepository.getFailures()).isEqualTo(1);
assertThat(featureRepository.getSkips()).isEqualTo(30);
for (int i = 0; i < 30; i++) {
runner.assertThatSkipsNextRun();
}
assertThat(featureRepository.getFailures()).isEqualTo(1);
assertThat(featureRepository.getSkips()).isEqualTo(0);
runner.assertThatFetchesAndReceives(NOT_CHANGED, 304);
assertThat(featureRepository.getFailures()).isEqualTo(0);
assertThat(featureRepository.getSkips()).isEqualTo(0);
}
@Test
public void should_incrementally_increase_interval_as_we_receive_too_many_requests()
throws URISyntaxException, IOException {
TestRunner runner = new TestRunner();
File file =
new File(getClass().getClassLoader().getResource("unleash-repo-v2.json").toURI());
ToggleBootstrapProvider toggleBootstrapProvider = mock(ToggleBootstrapProvider.class);
when(toggleBootstrapProvider.read()).thenReturn(fileToString(file));
UnleashConfig config =
UnleashConfig.builder()
.synchronousFetchOnInitialisation(false)
.appName("test-sync-update")
.scheduledExecutor(runner.executor)
.unleashAPI("http://localhost:8080")
.build();
when(backupHandler.read()).thenReturn(getFeatureCollection());
FeatureRepository featureRepository =
new FeatureRepository(
config,
backupHandler,
new EventDispatcher(config),
fetcher,
bootstrapHandler);
runner.assertThatFetchesAndReceives(UNAVAILABLE, 429);
// client is not ready don't count errors or skips
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(0);
runner.assertThatFetchesAndReceives(UNAVAILABLE, 429);
// client is not ready don't count errors or skips
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(0);
// this changes the client to ready
runner.assertThatFetchesAndReceives(CHANGED, 200);
assertThat(featureRepository.getSkips()).isEqualTo(0);
runner.assertThatFetchesAndReceives(UNAVAILABLE, 429);
assertThat(featureRepository.getSkips()).isEqualTo(1);
assertThat(featureRepository.getFailures()).isEqualTo(1);
runner.assertThatSkipsNextRun();
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(1);
runner.assertThatFetchesAndReceives(UNAVAILABLE, 429);
assertThat(featureRepository.getSkips()).isEqualTo(2);
assertThat(featureRepository.getFailures()).isEqualTo(2);
runner.assertThatSkipsNextRun();
assertThat(featureRepository.getSkips()).isEqualTo(1);
runner.assertThatSkipsNextRun();
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(2);
runner.assertThatFetchesAndReceives(UNAVAILABLE, 429);
assertThat(featureRepository.getSkips()).isEqualTo(3);
assertThat(featureRepository.getFailures()).isEqualTo(3);
runner.assertThatSkipsNextRun();
runner.assertThatSkipsNextRun();
runner.assertThatSkipsNextRun();
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(3);
runner.assertThatFetchesAndReceives(NOT_CHANGED, 304);
assertThat(featureRepository.getSkips()).isEqualTo(2);
assertThat(featureRepository.getFailures()).isEqualTo(2);
runner.assertThatSkipsNextRun();
runner.assertThatSkipsNextRun();
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(2);
runner.assertThatFetchesAndReceives(NOT_CHANGED, 304);
assertThat(featureRepository.getSkips()).isEqualTo(1);
assertThat(featureRepository.getFailures()).isEqualTo(1);
runner.assertThatSkipsNextRun();
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(1);
runner.assertThatFetchesAndReceives(NOT_CHANGED, 304);
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(0);
}
@Test
public void server_errors_should_incrementally_increase_interval()
throws URISyntaxException, IOException {
TestRunner runner = new TestRunner();
File file =
new File(getClass().getClassLoader().getResource("unleash-repo-v2.json").toURI());
ToggleBootstrapProvider toggleBootstrapProvider = mock(ToggleBootstrapProvider.class);
when(toggleBootstrapProvider.read()).thenReturn(fileToString(file));
UnleashConfig config =
UnleashConfig.builder()
.synchronousFetchOnInitialisation(false)
.appName("test-sync-update")
.scheduledExecutor(runner.executor)
.unleashAPI("http://localhost:8080")
.build();
when(backupHandler.read()).thenReturn(getFeatureCollection());
FeatureRepository featureRepository =
new FeatureRepository(
config,
backupHandler,
new EventDispatcher(config),
fetcher,
bootstrapHandler);
runner.assertThatFetchesAndReceives(CHANGED, 200); // set it ready
runner.assertThatFetchesAndReceives(UNAVAILABLE, 500);
assertThat(featureRepository.getSkips()).isEqualTo(1);
assertThat(featureRepository.getFailures()).isEqualTo(1);
runner.assertThatSkipsNextRun();
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(1);
runner.assertThatFetchesAndReceives(UNAVAILABLE, 502);
assertThat(featureRepository.getSkips()).isEqualTo(2);
assertThat(featureRepository.getFailures()).isEqualTo(2);
runner.assertThatSkipsNextRun();
runner.assertThatSkipsNextRun();
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(2);
runner.assertThatFetchesAndReceives(UNAVAILABLE, 503);
assertThat(featureRepository.getSkips()).isEqualTo(3);
assertThat(featureRepository.getFailures()).isEqualTo(3);
runner.assertThatSkipsNextRun();
runner.assertThatSkipsNextRun();
runner.assertThatSkipsNextRun();
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(3);
runner.assertThatFetchesAndReceives(NOT_CHANGED, 304);
assertThat(featureRepository.getSkips()).isEqualTo(2);
assertThat(featureRepository.getFailures()).isEqualTo(2);
runner.assertThatSkipsNextRun();
runner.assertThatSkipsNextRun();
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(2);
runner.assertThatFetchesAndReceives(NOT_CHANGED, 304);
assertThat(featureRepository.getSkips()).isEqualTo(1);
assertThat(featureRepository.getFailures()).isEqualTo(1);
runner.assertThatSkipsNextRun();
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(1);
runner.assertThatFetchesAndReceives(NOT_CHANGED, 304);
assertThat(featureRepository.getSkips()).isEqualTo(0);
assertThat(featureRepository.getFailures()).isEqualTo(0);
}
private String fileToString(File f) throws IOException {
return new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8);
}
@NotNull
private FeatureCollection simpleFeatureCollection(boolean enabled) {
return populatedFeatureCollection(
null,
new FeatureToggle(
"toggleFetcherCalled",
enabled,
Arrays.asList(new ActivationStrategy("custom", null))));
}
@NotNull
private FeatureCollection getFeatureCollection() {
return populatedFeatureCollection(
Arrays.asList(
new Segment(
1,
"some-name",
Arrays.asList(
new Constraint(
"some-context",
Operator.IN,
"some-value")))),
new FeatureToggle(
"toggleFeatureName1",
true,
Collections.singletonList(
new ActivationStrategy("custom", null))),
new FeatureToggle(
"toggleFeatureName2",
true,
Collections.singletonList(
new ActivationStrategy("custom", null))));
}
private class TestRunner {
private final UnleashScheduledExecutor executor;
private final ArgumentCaptor<Runnable> runnableArgumentCaptor;
private int count = 0;
private boolean initialized = false;
public TestRunner() {
this.executor = mock(UnleashScheduledExecutor.class);
this.runnableArgumentCaptor = ArgumentCaptor.forClass(Runnable.class);
}
private void ensureInitialized() {
if (!initialized) {
verify(executor).setInterval(runnableArgumentCaptor.capture(), anyLong(), anyLong());
initialized = true;
}
}
public void assertThatFetchesAndReceives(FeatureToggleResponse.Status status, int statusCode) {
ensureInitialized();
when(fetcher.fetchFeatures())
.thenReturn(new ClientFeaturesResponse(status, statusCode));
runnableArgumentCaptor.getValue().run();
verify(fetcher, times(++count)).fetchFeatures();
}
public void assertThatSkipsNextRun() {
ensureInitialized();
runnableArgumentCaptor.getValue().run();
verify(fetcher, times(count)).fetchFeatures();
}
}
}