-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSynchronizerTest.java
More file actions
342 lines (298 loc) · 15.6 KB
/
SynchronizerTest.java
File metadata and controls
342 lines (298 loc) · 15.6 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
package io.split.engine.common;
import io.split.client.events.EventsTask;
import io.split.client.impressions.ImpressionsManager;
import io.split.client.impressions.UniqueKeysTracker;
import io.split.client.interceptors.FlagSetsFilter;
import io.split.client.interceptors.FlagSetsFilterImpl;
import io.split.engine.segments.SegmentChangeFetcher;
import io.split.engine.segments.SegmentSynchronizationTaskImp;
import io.split.storages.*;
import io.split.storages.memory.InMemoryCacheImp;
import io.split.engine.experiments.FetchResult;
import io.split.engine.experiments.SplitFetcherImp;
import io.split.engine.experiments.SplitSynchronizationTask;
import io.split.engine.segments.SegmentFetcher;
import io.split.engine.segments.SegmentSynchronizationTask;
import io.split.telemetry.storage.TelemetryRuntimeProducer;
import io.split.telemetry.synchronizer.TelemetrySyncTask;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.internal.matchers.Any;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.mockito.Mockito.when;
public class SynchronizerTest {
private static final FlagSetsFilter FLAG_SETS_FILTER = new FlagSetsFilterImpl(new HashSet<>());
private SplitSynchronizationTask _refreshableSplitFetcherTask;
private SegmentSynchronizationTask _segmentFetcher;
private SplitFetcherImp _splitFetcher;
private SplitCacheProducer _splitCacheProducer;
private RuleBasedSegmentCacheProducer _ruleBasedSegmentCacheProducer;
private Synchronizer _synchronizer;
private SegmentCacheProducer _segmentCacheProducer;
private SplitTasks _splitTasks;
private TelemetrySyncTask _telemetrySyncTask;
private ImpressionsManager _impressionsManager;
private EventsTask _eventsTask;
private UniqueKeysTracker _uniqueKeysTracker;
@Before
public void beforeMethod() {
_refreshableSplitFetcherTask = Mockito.mock(SplitSynchronizationTask.class);
_segmentFetcher = Mockito.mock(SegmentSynchronizationTask.class);
_splitFetcher = Mockito.mock(SplitFetcherImp.class);
_ruleBasedSegmentCacheProducer = Mockito.mock(RuleBasedSegmentCacheProducer.class);
_splitCacheProducer = Mockito.mock(SplitCacheProducer.class);
_segmentCacheProducer = Mockito.mock(SegmentCache.class);
_telemetrySyncTask = Mockito.mock(TelemetrySyncTask.class);
_impressionsManager = Mockito.mock(ImpressionsManager.class);
_eventsTask = Mockito.mock(EventsTask.class);
_uniqueKeysTracker = Mockito.mock(UniqueKeysTracker.class);
_splitTasks = SplitTasks.build(_refreshableSplitFetcherTask, _segmentFetcher, _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker);
_synchronizer = new SynchronizerImp(_splitTasks, _splitFetcher, _splitCacheProducer, _segmentCacheProducer, _ruleBasedSegmentCacheProducer, 50, 10, 5, new HashSet<>());
}
@Test
public void syncAll() throws InterruptedException {
Mockito.when(_splitFetcher.forceRefresh(Mockito.anyObject())).thenReturn(new FetchResult(true, false, new HashSet<>()));
Mockito.when(_segmentFetcher.fetchAllSynchronous()).thenReturn(true);
_synchronizer.syncAll();
Thread.sleep(1000);
Mockito.verify(_splitFetcher, Mockito.times(1)).forceRefresh(Mockito.anyObject());
Mockito.verify(_segmentFetcher, Mockito.times(1)).fetchAllSynchronous();
}
@Test
public void testSyncAllSegments() throws InterruptedException, NoSuchFieldException, IllegalAccessException {
SegmentSynchronizationTask segmentSynchronizationTask = new SegmentSynchronizationTaskImp(Mockito.mock(SegmentChangeFetcher.class),
20L, 1, _segmentCacheProducer, Mockito.mock(TelemetryRuntimeProducer.class),
Mockito.mock(SplitCacheConsumer.class), null, Mockito.mock(RuleBasedSegmentCache.class));
Field synchronizerSegmentFetcher = SynchronizerImp.class.getDeclaredField("_segmentSynchronizationTaskImp");
synchronizerSegmentFetcher.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(synchronizerSegmentFetcher, synchronizerSegmentFetcher.getModifiers() & ~Modifier.FINAL);
synchronizerSegmentFetcher.set(_synchronizer, segmentSynchronizationTask);
Mockito.when(_splitFetcher.forceRefresh(Mockito.anyObject())).thenReturn(new FetchResult(true, false, Stream.of("Segment1", "Segment2").collect(Collectors.toSet())));
Mockito.when(_segmentFetcher.fetchAllSynchronous()).thenReturn(true);
_synchronizer.syncAll();
Thread.sleep(1000);
Mockito.verify(_splitFetcher, Mockito.times(1)).forceRefresh(Mockito.anyObject());
Assert.assertNotNull(segmentSynchronizationTask.getFetcher("Segment1"));
Assert.assertNotNull(segmentSynchronizationTask.getFetcher("Segment2"));
}
@Test
public void startPeriodicFetching() {
_synchronizer.startPeriodicFetching();
Mockito.verify(_refreshableSplitFetcherTask, Mockito.times(1)).start();
Mockito.verify(_segmentFetcher, Mockito.times(1)).start();
}
@Test
public void stopPeriodicFetching() {
_synchronizer.stopPeriodicFetching();
Mockito.verify(_refreshableSplitFetcherTask, Mockito.times(1)).stop();
Mockito.verify(_segmentFetcher, Mockito.times(1)).stop();
}
@Test
public void streamingRetryOnSplit() {
when(_splitCacheProducer.getChangeNumber()).thenReturn(0l).thenReturn(0l).thenReturn(1l);
when(_splitFetcher.forceRefresh(Mockito.anyObject())).thenReturn(new FetchResult(true, false, new HashSet<>()));
_synchronizer.refreshSplits(1L, 0L);
Mockito.verify(_splitCacheProducer, Mockito.times(3)).getChangeNumber();
}
@Test
public void streamingRetryOnSegment() {
SegmentFetcher fetcher = Mockito.mock(SegmentFetcher.class);
when(_segmentFetcher.getFetcher(Mockito.anyString())).thenReturn(fetcher);
when(_segmentCacheProducer.getChangeNumber(Mockito.anyString())).thenReturn(0l).thenReturn(0l).thenReturn(1l);
_synchronizer.refreshSegment("Segment",1l);
Mockito.verify(_segmentCacheProducer, Mockito.times(3)).getChangeNumber(Mockito.anyString());
}
@Test
public void streamingRetryOnSplitAndSegment() {
when(_splitCacheProducer.getChangeNumber()).thenReturn(0l).thenReturn(0l).thenReturn(1l);
Set<String> segments = new HashSet<>();
segments.add("segment1");
segments.add("segment2");
when(_splitFetcher.forceRefresh(Mockito.anyObject())).thenReturn(new FetchResult(true, false, segments));
SegmentFetcher fetcher = Mockito.mock(SegmentFetcher.class);
when(_segmentCacheProducer.getChangeNumber(Mockito.anyString())).thenReturn(0l).thenReturn(0l).thenReturn(1l);
when(_segmentFetcher.getFetcher(Mockito.anyString())).thenReturn(fetcher);
_synchronizer.refreshSplits(1L, 0L);
Mockito.verify(_splitCacheProducer, Mockito.times(3)).getChangeNumber();
Mockito.verify(_segmentFetcher, Mockito.times(2)).getFetcher(Mockito.anyString());
}
@Test
public void testCDNBypassIsRequestedAfterNFailures() {
SplitCache cache = new InMemoryCacheImp(FLAG_SETS_FILTER);
Synchronizer imp = new SynchronizerImp(_splitTasks,
_splitFetcher,
cache,
_segmentCacheProducer,
_ruleBasedSegmentCacheProducer,
50,
3,
1,
new HashSet<>());
ArgumentCaptor<FetchOptions> optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class);
AtomicInteger calls = new AtomicInteger();
Mockito.doAnswer(invocationOnMock -> {
calls.getAndIncrement();
switch (calls.get()) {
case 4: cache.setChangeNumber(123);
}
return new FetchResult(true, false, new HashSet<>());
}).when(_splitFetcher).forceRefresh(optionsCaptor.capture());
imp.refreshSplits(123L, 0L);
List<FetchOptions> options = optionsCaptor.getAllValues();
Assert.assertEquals(options.size(), 4);
Assert.assertFalse(options.get(0).hasCustomCN());
Assert.assertFalse(options.get(1).hasCustomCN());
Assert.assertFalse(options.get(2).hasCustomCN());
Assert.assertTrue(options.get(3).hasCustomCN());
}
@Test
public void testCDNBypassRequestLimitAndBackoff() throws NoSuchFieldException, IllegalAccessException {
SplitCache cache = new InMemoryCacheImp(FLAG_SETS_FILTER);
Synchronizer imp = new SynchronizerImp(_splitTasks,
_splitFetcher,
cache,
_segmentCacheProducer,
_ruleBasedSegmentCacheProducer,
50,
3,
1,
new HashSet<>());
ArgumentCaptor<FetchOptions> optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class);
AtomicInteger calls = new AtomicInteger();
Mockito.doAnswer(invocationOnMock -> {
calls.getAndIncrement();
switch (calls.get()) {
case 14: Assert.assertTrue(false); // should never get here
}
return null;
}).when(_splitFetcher).forceRefresh(optionsCaptor.capture());
// Before executing, we'll update the backoff via reflection, to avoid waiting minutes for the test to run.
Field backoffBase = SynchronizerImp.class.getDeclaredField("ON_DEMAND_FETCH_BACKOFF_BASE_MS");
backoffBase.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(backoffBase, backoffBase.getModifiers() & ~Modifier.FINAL);
backoffBase.set(imp, 1); // 1ms
long before = System.currentTimeMillis();
imp.refreshSplits(1L, 0L);
long after = System.currentTimeMillis();
List<FetchOptions> options = optionsCaptor.getAllValues();
Assert.assertEquals(options.size(), 13);
Assert.assertFalse(options.get(0).hasCustomCN());
Assert.assertFalse(options.get(1).hasCustomCN());
Assert.assertFalse(options.get(2).hasCustomCN());
Assert.assertTrue(options.get(3).hasCustomCN());
Assert.assertTrue(options.get(4).hasCustomCN());
Assert.assertTrue(options.get(5).hasCustomCN());
Assert.assertTrue(options.get(6).hasCustomCN());
Assert.assertTrue(options.get(7).hasCustomCN());
Assert.assertTrue(options.get(8).hasCustomCN());
Assert.assertTrue(options.get(9).hasCustomCN());
Assert.assertTrue(options.get(10).hasCustomCN());
Assert.assertTrue(options.get(11).hasCustomCN());
Assert.assertTrue(options.get(12).hasCustomCN());
Assert.assertEquals(calls.get(), 13);
long minDiffExpected = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256;
Assert.assertTrue((after - before) > minDiffExpected);
}
@Test
public void testCDNBypassRequestLimitAndForSegmentsBackoff() throws NoSuchFieldException, IllegalAccessException {
SplitCache cache = new InMemoryCacheImp(FLAG_SETS_FILTER);
Synchronizer imp = new SynchronizerImp(_splitTasks,
_splitFetcher,
cache,
_segmentCacheProducer,
_ruleBasedSegmentCacheProducer,
50,
3,
1,
new HashSet<>());
SegmentFetcher fetcher = Mockito.mock(SegmentFetcher.class);
when(_segmentFetcher.getFetcher("someSegment")).thenReturn(fetcher);
ArgumentCaptor<FetchOptions> optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class);
AtomicInteger calls = new AtomicInteger();
Mockito.doAnswer(invocationOnMock -> {
calls.getAndIncrement();
switch (calls.get()) {
case 14: Assert.assertTrue(false); // should never get here
}
return null;
}).when(fetcher).fetch(optionsCaptor.capture());
// Before executing, we'll update the backoff via reflection, to avoid waiting minutes for the test to run.
Field backoffBase = SynchronizerImp.class.getDeclaredField("ON_DEMAND_FETCH_BACKOFF_BASE_MS");
backoffBase.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(backoffBase, backoffBase.getModifiers() & ~Modifier.FINAL);
backoffBase.set(imp, 1); // 1ms
long before = System.currentTimeMillis();
imp.refreshSegment("someSegment",1L);
long after = System.currentTimeMillis();
List<FetchOptions> options = optionsCaptor.getAllValues();
Assert.assertEquals(options.size(), 13);
Assert.assertFalse(options.get(0).hasCustomCN());
Assert.assertFalse(options.get(1).hasCustomCN());
Assert.assertFalse(options.get(2).hasCustomCN());
Assert.assertTrue(options.get(3).hasCustomCN());
Assert.assertTrue(options.get(4).hasCustomCN());
Assert.assertTrue(options.get(5).hasCustomCN());
Assert.assertTrue(options.get(6).hasCustomCN());
Assert.assertTrue(options.get(7).hasCustomCN());
Assert.assertTrue(options.get(8).hasCustomCN());
Assert.assertTrue(options.get(9).hasCustomCN());
Assert.assertTrue(options.get(10).hasCustomCN());
Assert.assertTrue(options.get(11).hasCustomCN());
Assert.assertTrue(options.get(12).hasCustomCN());
Assert.assertEquals(calls.get(), 13);
long minDiffExpected = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256;
Assert.assertTrue((after - before) > minDiffExpected);
}
@Test
public void testDataRecording(){
SplitCache cache = new InMemoryCacheImp(FLAG_SETS_FILTER);
Synchronizer imp = new SynchronizerImp(_splitTasks,
_splitFetcher,
cache,
_segmentCacheProducer,
_ruleBasedSegmentCacheProducer,
50,
3,
1,
new HashSet<>());
imp.startPeriodicDataRecording();
Mockito.verify(_eventsTask, Mockito.times(1)).start();
Mockito.verify(_impressionsManager, Mockito.times(1)).start();
Mockito.verify(_uniqueKeysTracker, Mockito.times(1)).start();
Mockito.verify(_telemetrySyncTask, Mockito.times(1)).startScheduledTask();
imp.stopPeriodicDataRecording();
Mockito.verify(_eventsTask, Mockito.times(1)).close();
Mockito.verify(_impressionsManager, Mockito.times(1)).close();
Mockito.verify(_uniqueKeysTracker, Mockito.times(1)).stop();
Mockito.verify(_telemetrySyncTask, Mockito.times(1)).stopScheduledTask();
}
@Test
public void skipSyncWhenChangeNumbersAreZero() {
_synchronizer.refreshSplits(0L, 0L);
Mockito.verify(_splitFetcher, Mockito.times(0)).forceRefresh(Mockito.anyObject());
}
@Test
public void testSyncRuleBasedSegment() {
when(_ruleBasedSegmentCacheProducer.getChangeNumber()).thenReturn(-1l).thenReturn(-1l).thenReturn(123l);
when(_splitFetcher.forceRefresh(Mockito.anyObject())).thenReturn(new FetchResult(true, false, new HashSet<>()));
_synchronizer.refreshSplits(0L, 123L);
Mockito.verify(_splitFetcher, Mockito.times(2)).forceRefresh(Mockito.anyObject());
}
}