-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSegmentSynchronizationTaskImpTest.java
More file actions
185 lines (154 loc) · 8.98 KB
/
SegmentSynchronizationTaskImpTest.java
File metadata and controls
185 lines (154 loc) · 8.98 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
package io.split.engine.segments;
import io.split.client.LocalhostSegmentChangeFetcher;
import io.split.client.JsonLocalhostSplitChangeFetcher;
import io.split.client.interceptors.FlagSetsFilter;
import io.split.client.interceptors.FlagSetsFilterImpl;
import io.split.client.utils.InputStreamProvider;
import io.split.client.utils.StaticContentInputStreamProvider;
import io.split.engine.common.FetchOptions;
import io.split.engine.experiments.*;
import io.split.storages.*;
import io.split.telemetry.storage.TelemetryRuntimeProducer;
import io.split.storages.memory.InMemoryCacheImp;
import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp;
import io.split.storages.memory.SegmentCacheInMemoryImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals;
/**
* Tests for SegmentSynchronizationTaskImp
*
* @author adil+
*/
public class SegmentSynchronizationTaskImpTest {
private static final Logger _log = LoggerFactory.getLogger(SegmentSynchronizationTaskImpTest.class);
private static final TelemetryListener TELEMETRY_STORAGE = t -> {};
private static final TelemetryListener TELEMETRY_STORAGE_NOOP = t -> {};
private static final ExecutorFactory EXECUTOR_FACTORY = (tf, name, n) -> java.util.concurrent.Executors.newScheduledThreadPool(n);
private AtomicReference<SegmentFetcher> fetcher1 = null;
private AtomicReference<SegmentFetcher> fetcher2 = null;
@Before
public void beforeMethod() {
fetcher1 = new AtomicReference<>(null);
fetcher2 = new AtomicReference<>(null);
}
@Test
public void works() {
SegmentCacheProducer segmentCacheProducer = Mockito.mock(SegmentCacheProducer.class);
SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class);
final SegmentSynchronizationTaskImp fetchers = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1L, 1, segmentCacheProducer,
TELEMETRY_STORAGE, Mockito.mock(SplitCacheConsumer.class), EXECUTOR_FACTORY, null, Mockito.mock(RuleBasedSegmentCache.class));
// create two tasks that will separately call segment and make sure
// that both of them get the exact same instance.
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(new Runnable() {
@Override
public void run() {
fetcher1.set(fetchers.getFetcher("foo"));
}
});
executorService.execute(new Runnable() {
@Override
public void run() {
fetcher2.set(fetchers.getFetcher("foo"));
}
});
executorService.shutdown();
try {
if (!executorService.awaitTermination(10L, TimeUnit.SECONDS)) {
_log.info("Executor did not terminate in the specified time.");
List<Runnable> droppedTasks = executorService.shutdownNow();
_log.info("Executor was abruptly shut down. These tasks will not be executed: " + droppedTasks);
}
} catch (InterruptedException e) {
// reset the interrupt.
Thread.currentThread().interrupt();
}
Assert.assertNotNull(fetcher1.get());
assertEquals(fetcher1.get(), fetcher2.get());
}
@Test
public void testFetchAllAsynchronousAndGetFalse() throws NoSuchFieldException, IllegalAccessException {
SegmentCacheProducer segmentCacheProducer = Mockito.mock(SegmentCacheProducer.class);
ConcurrentMap<String, SegmentFetcher> _segmentFetchers = new ConcurrentHashMap<>();
SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class);
SegmentFetcherImp segmentFetcher = Mockito.mock(SegmentFetcherImp.class);
_segmentFetchers.put("SF", segmentFetcher);
final SegmentSynchronizationTaskImp fetchers = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1L, 1,
segmentCacheProducer, TELEMETRY_STORAGE, Mockito.mock(SplitCacheConsumer.class), EXECUTOR_FACTORY, null, Mockito.mock(RuleBasedSegmentCache.class));
Mockito.when(segmentFetcher.runWhitCacheHeader()).thenReturn(false);
Mockito.when(segmentFetcher.fetch(Mockito.anyObject())).thenReturn(false);
// Before executing, we'll update the map of segmentFecthers via reflection.
Field segmentFetchersForced = SegmentSynchronizationTaskImp.class.getDeclaredField("_segmentFetchers");
segmentFetchersForced.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(segmentFetchersForced, segmentFetchersForced.getModifiers() & ~Modifier.FINAL);
segmentFetchersForced.set(fetchers, _segmentFetchers);
boolean fetch = fetchers.fetchAllSynchronous();
Assert.assertEquals(false, fetch);
}
@Test
public void testFetchAllAsynchronousAndGetTrue() throws NoSuchFieldException, IllegalAccessException {
SegmentCacheProducer segmentCacheProducer = Mockito.mock(SegmentCacheProducer.class);
ConcurrentMap<String, SegmentFetcher> _segmentFetchers = new ConcurrentHashMap<>();
SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class);
SegmentFetcherImp segmentFetcher = Mockito.mock(SegmentFetcherImp.class);
final SegmentSynchronizationTaskImp fetchers = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1L, 1, segmentCacheProducer,
TELEMETRY_STORAGE, Mockito.mock(SplitCacheConsumer.class), EXECUTOR_FACTORY, null, Mockito.mock(RuleBasedSegmentCache.class));
// Before executing, we'll update the map of segmentFecthers via reflection.
Field segmentFetchersForced = SegmentSynchronizationTaskImp.class.getDeclaredField("_segmentFetchers");
segmentFetchersForced.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(segmentFetchersForced, segmentFetchersForced.getModifiers() & ~Modifier.FINAL);
segmentFetchersForced.set(fetchers, _segmentFetchers);
Mockito.when(segmentFetcher.runWhitCacheHeader()).thenReturn(true);
Mockito.when(segmentFetcher.fetch(Mockito.anyObject())).thenReturn(true);
boolean fetch = fetchers.fetchAllSynchronous();
Assert.assertEquals(true, fetch);
}
@Test
public void testLocalhostSegmentChangeFetcher() throws InterruptedException, FileNotFoundException {
FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>());
SplitCache splitCacheProducer = new InMemoryCacheImp(flagSetsFilter);
InputStream inputStream = new FileInputStream("src/test/resources/split_init.json");
InputStreamProvider inputStreamProvider = new StaticContentInputStreamProvider(inputStream);
SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider);
SplitParser splitParser = new SplitParser();
FetchOptions fetchOptions = new FetchOptions.Builder().build();
RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp();
RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser();
SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, Mockito.mock(TelemetryRuntimeProducer.class), flagSetsFilter,
ruleBasedSegmentParser, ruleBasedSegmentCache);
SplitSynchronizationTask splitSynchronizationTask = new SplitSynchronizationTask(splitFetcher, splitCacheProducer, 1000, null);
splitSynchronizationTask.start();
Thread.sleep(2000);
SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(LocalhostSegmentChangeFetcher.class);
SegmentCacheProducer segmentCacheProducer = new SegmentCacheInMemoryImpl();
SegmentSynchronizationTaskImp segmentSynchronizationTaskImp = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1000, 1, segmentCacheProducer,
TELEMETRY_STORAGE_NOOP, splitCacheProducer, EXECUTOR_FACTORY, null, ruleBasedSegmentCache);
segmentSynchronizationTaskImp.start();
Thread.sleep(2000);
Mockito.verify(segmentChangeFetcher, Mockito.times(1)).fetch("segment_1",-1, fetchOptions);
Mockito.verify(segmentChangeFetcher, Mockito.times(1)).fetch("segment_2",-1, fetchOptions);
}
}