-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSplitManagerImplTest.java
More file actions
266 lines (232 loc) · 12.6 KB
/
SplitManagerImplTest.java
File metadata and controls
266 lines (232 loc) · 12.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
package io.split.client;
import com.google.common.collect.Lists;
import io.split.client.api.SplitView;
import io.split.client.dtos.Prerequisites;
import io.split.client.dtos.Split;
import io.split.client.dtos.SplitChange;
import io.split.client.utils.Json;
import io.split.engine.ConditionsTestUtil;
import io.split.engine.SDKReadinessGates;
import io.split.engine.experiments.ParsedCondition;
import io.split.engine.experiments.ParsedSplit;
import io.split.engine.experiments.SplitParser;
import io.split.rules.matchers.AllKeysMatcher;
import io.split.rules.matchers.CombiningMatcher;
import io.split.rules.matchers.PrerequisitesMatcher;
import io.split.grammar.Treatments;
import io.split.storages.SplitCacheConsumer;
import io.split.telemetry.storage.InMemoryTelemetryStorage;
import io.split.telemetry.storage.TelemetryStorage;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class SplitManagerImplTest {
private SplitClientConfig config = SplitClientConfig.builder().setBlockUntilReadyTimeout(100).build();
private static TelemetryStorage TELEMETRY_STORAGE = mock(InMemoryTelemetryStorage.class);
@Before
public void updateTelemetryStorage() {
TELEMETRY_STORAGE = mock(InMemoryTelemetryStorage.class);
}
@Test
public void splitCallWithNonExistentSplit() {
String nonExistent = "nonExistent";
SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class);
when(splitCacheConsumer.get(nonExistent)).thenReturn(null);
SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer,
mock(SplitClientConfig.class),
mock(SDKReadinessGates.class), TELEMETRY_STORAGE);
Assert.assertNull(splitManager.split("nonExistent"));
}
@Test
public void splitCallWithExistentSplit() {
String existent = "existent";
SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class);
Prerequisites prereq = new Prerequisites();
prereq.featureFlagName = "feature1";
prereq.treatments = Lists.newArrayList("on");
io.split.rules.model.Prerequisite prerequisite = new io.split.rules.model.Prerequisite(prereq.featureFlagName, prereq.treatments);
ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1, new HashSet<>(), false,
new PrerequisitesMatcher(Lists.newArrayList(prerequisite)));
when(splitCacheConsumer.get(existent)).thenReturn(response);
SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer,
mock(SplitClientConfig.class),
mock(SDKReadinessGates.class), TELEMETRY_STORAGE);
SplitView theOne = splitManager.split(existent);
Assert.assertEquals(response.feature(), theOne.name);
Assert.assertEquals(response.changeNumber(), theOne.changeNumber);
Assert.assertEquals(response.killed(), theOne.killed);
Assert.assertEquals(response.trafficTypeName(), theOne.trafficType);
Assert.assertEquals(1, theOne.treatments.size());
Assert.assertEquals("off", theOne.treatments.get(0));
Assert.assertEquals(0, theOne.configs.size());
Assert.assertEquals("off", theOne.defaultTreatment);
Assert.assertEquals(1, theOne.prerequisites.size());
Assert.assertEquals(prereq.featureFlagName, theOne.prerequisites.get(0).featureFlagName);
Assert.assertEquals(prereq.treatments, theOne.prerequisites.get(0).treatments);
}
@Test
public void splitCallWithExistentSplitAndConfigs() {
String existent = "existent";
SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class);
// Add config for only one treatment(default)
Map<String, String> configurations = new HashMap<>();
configurations.put(Treatments.OFF, "{\"size\" : 30}");
ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1, configurations, new HashSet<>(), false, null);
when(splitCacheConsumer.get(existent)).thenReturn(response);
SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer,
mock(SplitClientConfig.class),
mock(SDKReadinessGates.class), TELEMETRY_STORAGE);
SplitView theOne = splitManager.split(existent);
Assert.assertEquals(response.feature(), theOne.name);
Assert.assertEquals(response.changeNumber(), theOne.changeNumber);
Assert.assertEquals(response.killed(), theOne.killed);
Assert.assertEquals(response.trafficTypeName(), theOne.trafficType);
Assert.assertEquals(1, theOne.treatments.size());
Assert.assertEquals("off", theOne.treatments.get(0));
Assert.assertEquals("{\"size\" : 30}", theOne.configs.get("off"));
}
@Test
public void splitsCallWithNoSplit() {
SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class);
when(splitCacheConsumer.getAll()).thenReturn(Lists.<ParsedSplit>newArrayList());
SDKReadinessGates gates = mock(SDKReadinessGates.class);
when(gates.isSDKReady()).thenReturn(false);
SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer,
mock(SplitClientConfig.class),
gates, TELEMETRY_STORAGE);
Assert.assertTrue(splitManager.splits().isEmpty());
verify(TELEMETRY_STORAGE, times(1)).recordNonReadyUsage();
}
@Test
public void splitsCallWithSplit() {
SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class);
List<ParsedSplit> parsedSplits = Lists.newArrayList();
SDKReadinessGates gates = mock(SDKReadinessGates.class);
when(gates.isSDKReady()).thenReturn(false);
ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1, new HashSet<>(), false, null);
parsedSplits.add(response);
when(splitCacheConsumer.getAll()).thenReturn(parsedSplits);
SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer,
mock(SplitClientConfig.class),
gates, TELEMETRY_STORAGE);
List<SplitView> splits = splitManager.splits();
Assert.assertEquals(1, splits.size());
Assert.assertEquals(response.feature(), splits.get(0).name);
Assert.assertEquals(response.changeNumber(), response.changeNumber());
Assert.assertEquals(response.killed(), splits.get(0).killed);
Assert.assertEquals(response.trafficTypeName(), splits.get(0).trafficType);
Assert.assertEquals(1, splits.get(0).treatments.size());
Assert.assertEquals("off", splits.get(0).treatments.get(0));
verify(TELEMETRY_STORAGE, times(1)).recordNonReadyUsage();
}
@Test
public void splitNamesCallWithNoSplit() {
SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class);
when(splitCacheConsumer.getAll()).thenReturn(Lists.<ParsedSplit>newArrayList());
SDKReadinessGates gates = mock(SDKReadinessGates.class);
when(gates.isSDKReady()).thenReturn(false);
SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer,
mock(SplitClientConfig.class),
gates, TELEMETRY_STORAGE);
Assert.assertTrue(splitManager.splitNames().isEmpty());
verify(TELEMETRY_STORAGE, times(1)).recordNonReadyUsage();
}
@Test
public void splitNamesCallWithSplit() {
SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class);
List<String> parsedSplits = new ArrayList<>();
parsedSplits.add("FeatureName");
when(splitCacheConsumer.splitNames()).thenReturn(parsedSplits);
SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer,
mock(SplitClientConfig.class),
mock(SDKReadinessGates.class), TELEMETRY_STORAGE);
List<String> splitNames = splitManager.splitNames();
Assert.assertEquals(1, splitNames.size());
Assert.assertEquals("FeatureName",splitNames.get(0));
}
@Test
public void blockUntilReadyDoesNotTimeWhenSdkIsReady() throws TimeoutException, InterruptedException {
SDKReadinessGates ready = mock(SDKReadinessGates.class);
when(ready.waitUntilInternalReady(100)).thenReturn(true);
SplitManagerImpl splitManager = new SplitManagerImpl(mock(SplitCacheConsumer.class),
config,
ready, TELEMETRY_STORAGE);
splitManager.blockUntilReady();
}
@Test(expected = TimeoutException.class)
public void blockUntilReadyTimesWhenSdkIsNotReady() throws TimeoutException, InterruptedException {
SDKReadinessGates ready = mock(SDKReadinessGates.class);
when(ready.waitUntilInternalReady(100)).thenReturn(false);
SplitManagerImpl splitManager = new SplitManagerImpl(mock(SplitCacheConsumer.class),
config,
ready, TELEMETRY_STORAGE);
splitManager.blockUntilReady();
verify(TELEMETRY_STORAGE, times(1)).recordBURTimeout();
}
@Test
public void splitCallWithExistentSets() {
String existent = "existent";
SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class);
ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off",
Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1, new HashSet<>(Arrays.asList("set1", "set2", "set3")), false, null);
when(splitCacheConsumer.get(existent)).thenReturn(response);
SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer,
mock(SplitClientConfig.class),
mock(SDKReadinessGates.class), TELEMETRY_STORAGE);
SplitView theOne = splitManager.split(existent);
Assert.assertEquals(response.flagSets().size(), theOne.sets.size());
}
@Test
public void splitCallWithEmptySets() {
String existent = "existent";
SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class);
ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off",
Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1, null, false, null);
when(splitCacheConsumer.get(existent)).thenReturn(response);
SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer,
mock(SplitClientConfig.class),
mock(SDKReadinessGates.class), TELEMETRY_STORAGE);
SplitView theOne = splitManager.split(existent);
Assert.assertEquals(0, theOne.sets.size());
}
private ParsedCondition getTestCondition(String treatment) {
return ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(ConditionsTestUtil.partition(treatment, 10)));
}
@Test
public void ImpressionToggleParseTest() throws IOException {
SplitParser parser = new SplitParser();
String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8);
SplitChange change = Json.fromJson(splits, SplitChange.class);
SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class);
for (Split split : change.featureFlags.d) {
ParsedSplit parsedSplit = parser.parse(split);
when(splitCacheConsumer.get(split.name)).thenReturn(parsedSplit);
}
SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer,
mock(SplitClientConfig.class),
mock(SDKReadinessGates.class), TELEMETRY_STORAGE);
SplitView splitView = splitManager.split("without_impression_toggle");
assertFalse(splitView.impressionsDisabled);
splitView = splitManager.split("impression_toggle_on");
assertFalse(splitView.impressionsDisabled);
splitView = splitManager.split("impression_toggle_off");
assertTrue(splitView.impressionsDisabled);
}
}