-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRolloutCacheManagerIntegrationTest.java
More file actions
304 lines (253 loc) · 14 KB
/
RolloutCacheManagerIntegrationTest.java
File metadata and controls
304 lines (253 loc) · 14 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
package tests.integration.rollout;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static helper.IntegrationHelper.buildFactory;
import static helper.IntegrationHelper.dummyApiKey;
import static helper.IntegrationHelper.dummyUserKey;
import static helper.IntegrationHelper.getTimestampDaysAgo;
import static helper.IntegrationHelper.randomizedAllSegments;
import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import helper.DatabaseHelper;
import helper.FileHelper;
import helper.IntegrationHelper;
import helper.TestableSplitConfigBuilder;
import io.split.android.client.RolloutCacheConfiguration;
import io.split.android.client.ServiceEndpoints;
import io.split.android.client.SplitClientConfig;
import io.split.android.client.SplitFactory;
import io.split.android.client.dtos.SegmentsChange;
import io.split.android.client.dtos.Split;
import io.split.android.client.dtos.SplitChange;
import io.split.android.client.events.SplitEvent;
import io.split.android.client.storage.db.GeneralInfoEntity;
import io.split.android.client.storage.db.MyLargeSegmentEntity;
import io.split.android.client.storage.db.MySegmentEntity;
import io.split.android.client.storage.db.SplitEntity;
import io.split.android.client.storage.db.SplitRoomDatabase;
import io.split.android.client.utils.Json;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import tests.integration.shared.TestingHelper;
public class RolloutCacheManagerIntegrationTest {
private final AtomicReference<String> mSinceFromUri = new AtomicReference<>(null);
private MockWebServer mWebServer;
private SplitRoomDatabase mRoomDb;
private final Context mContext = InstrumentationRegistry.getInstrumentation().getContext();
private CountDownLatch mRequestCountdownLatch;
@Before
public void setUp() {
mSinceFromUri.set(null);
setupServer();
mRoomDb = DatabaseHelper.getTestDatabase(mContext);
mRoomDb.clearAllTables();
mRequestCountdownLatch = new CountDownLatch(1);
}
@Test
public void expirationPeriodIsUsed() throws InterruptedException {
test(getTimestampDaysAgo(1), RolloutCacheConfiguration.builder().expirationDays(1));
}
@Test
public void clearOnInitClearsCacheOnStartup() throws InterruptedException {
test(System.currentTimeMillis(), RolloutCacheConfiguration.builder().clearOnInit(true));
}
@Test
public void repeatedInitWithClearOnInitSetToTrueDoesNotClearIfMinDaysHasNotElapsed() throws InterruptedException {
// Preload DB with update timestamp of now
long oldTimestamp = System.currentTimeMillis();
preloadDb(oldTimestamp, 0L, 8000L);
// Track initial values
List<SplitEntity> initialFlags = mRoomDb.splitDao().getAll();
List<MySegmentEntity> initialSegments = mRoomDb.mySegmentDao().getAll();
List<MyLargeSegmentEntity> initialLargeSegments = mRoomDb.myLargeSegmentDao().getAll();
long initialChangeNumber = mRoomDb.generalInfoDao().getByName(GeneralInfoEntity.CHANGE_NUMBER_INFO).getLongValue();
CountDownLatch readyLatch = new CountDownLatch(1);
SplitFactory factory = getSplitFactory(RolloutCacheConfiguration.builder().clearOnInit(true).build());
Thread.sleep(2000);
// Track intermediate values
List<SplitEntity> intermediateFlags = mRoomDb.splitDao().getAll();
List<MySegmentEntity> intermediateSegments = mRoomDb.mySegmentDao().getAll();
List<MyLargeSegmentEntity> intermediateLargeSegments = mRoomDb.myLargeSegmentDao().getAll();
long intermediateChangeNumber = mRoomDb.generalInfoDao().getByName(GeneralInfoEntity.CHANGE_NUMBER_INFO).getLongValue();
// Resume server responses after tracking DB values
mRequestCountdownLatch.countDown();
// Wait for ready
factory.client().on(SplitEvent.SDK_READY, TestingHelper.testTask(readyLatch));
boolean readyAwait = readyLatch.await(10, TimeUnit.SECONDS);
// Destroy factory
Thread.sleep(2000);
factory.destroy();
mRequestCountdownLatch = new CountDownLatch(1);
preloadDb(null, null, null);
SplitFactory factory2 = getSplitFactory(RolloutCacheConfiguration.builder().clearOnInit(true).build());
Thread.sleep(2000);
// Track intermediate values
List<SplitEntity> factory2Flags = mRoomDb.splitDao().getAll();
List<MySegmentEntity> factory2Segments = mRoomDb.mySegmentDao().getAll();
List<MyLargeSegmentEntity> factory2LargeSegments = mRoomDb.myLargeSegmentDao().getAll();
long factory2ChangeNumber = mRoomDb.generalInfoDao().getByName(GeneralInfoEntity.CHANGE_NUMBER_INFO).getLongValue();
// initial values
assertTrue(readyAwait);
assertEquals(2, initialFlags.size());
assertEquals(1, initialSegments.size());
assertFalse(Json.fromJson(initialSegments.get(0).getSegmentList(), SegmentsChange.class).getSegments().isEmpty());
assertFalse(Json.fromJson(initialLargeSegments.get(0).getSegmentList(), SegmentsChange.class).getSegments().isEmpty());
assertEquals(8000L, initialChangeNumber);
// values after clear
assertEquals(0, intermediateSegments.size());
assertEquals(0, intermediateLargeSegments.size());
assertEquals(0, intermediateFlags.size());
assertEquals(-1, intermediateChangeNumber);
// values after second init (values were reinserted into DB); no clear
assertEquals(2, factory2Flags.size());
assertEquals(1, factory2Segments.size());
assertFalse(Json.fromJson(factory2Segments.get(0).getSegmentList(), SegmentsChange.class).getSegments().isEmpty());
assertFalse(Json.fromJson(factory2LargeSegments.get(0).getSegmentList(), SegmentsChange.class).getSegments().isEmpty());
assertEquals(10000L, factory2ChangeNumber);
assertTrue(0L < mRoomDb.generalInfoDao()
.getByName("rolloutCacheLastClearTimestamp").getLongValue());
}
@Test
public void sdkReadyFromCacheIsEmittedOnFreshInit() throws InterruptedException {
SplitFactory splitFactory = getSplitFactory(RolloutCacheConfiguration.builder().build());
CountDownLatch latch1 = new CountDownLatch(1);
CountDownLatch latch2 = new CountDownLatch(1);
splitFactory.client().on(SplitEvent.SDK_READY_FROM_CACHE, TestingHelper.testTask(latch1));
splitFactory.client("two").on(SplitEvent.SDK_READY_FROM_CACHE, TestingHelper.testTask(latch2));
mRequestCountdownLatch.countDown();
boolean await1 = latch1.await(10, TimeUnit.SECONDS);
boolean await2 = latch2.await(10, TimeUnit.SECONDS);
assertTrue(await1);
assertTrue(await2);
}
private void test(long timestampDaysAgo, RolloutCacheConfiguration.Builder configBuilder) throws InterruptedException {
// Preload DB with update timestamp of 1 day ago
long oldTimestamp = timestampDaysAgo;
preloadDb(oldTimestamp, 0L, 8000L);
// Track initial values
List<SplitEntity> initialFlags = mRoomDb.splitDao().getAll();
List<MySegmentEntity> initialSegments = mRoomDb.mySegmentDao().getAll();
List<MyLargeSegmentEntity> initialLargeSegments = mRoomDb.myLargeSegmentDao().getAll();
long initialChangeNumber = mRoomDb.generalInfoDao().getByName(GeneralInfoEntity.CHANGE_NUMBER_INFO).getLongValue();
// Initialize SDK
CountDownLatch readyLatch = new CountDownLatch(1);
SplitFactory factory = getSplitFactory(configBuilder.build());
Thread.sleep(1000);
// Track final values
verify(factory, readyLatch, initialFlags, initialSegments, initialLargeSegments, initialChangeNumber);
}
private void verify(SplitFactory factory, CountDownLatch readyLatch, List<SplitEntity> initialFlags, List<MySegmentEntity> initialSegments, List<MyLargeSegmentEntity> initialLargeSegments, long initialChangeNumber) throws InterruptedException {
// Track final values
Thread.sleep(10000);
List<SplitEntity> finalFlags = mRoomDb.splitDao().getAll();
List<MySegmentEntity> finalSegments = mRoomDb.mySegmentDao().getAll();
List<MyLargeSegmentEntity> finalLargeSegments = mRoomDb.myLargeSegmentDao().getAll();
long finalChangeNumber = mRoomDb.generalInfoDao().getByName(GeneralInfoEntity.CHANGE_NUMBER_INFO).getLongValue();
// Resume server responses after tracking DB values
mRequestCountdownLatch.countDown();
// Wait for ready
factory.client().on(SplitEvent.SDK_READY, TestingHelper.testTask(readyLatch));
boolean readyAwait = readyLatch.await(10, TimeUnit.SECONDS);
// Verify
assertTrue(readyAwait);
assertEquals(2, initialFlags.size());
assertEquals(1, initialSegments.size());
assertFalse(Json.fromJson(initialSegments.get(0).getSegmentList(), SegmentsChange.class).getSegments().isEmpty());
assertFalse(Json.fromJson(initialLargeSegments.get(0).getSegmentList(), SegmentsChange.class).getSegments().isEmpty());
assertEquals(8000L, initialChangeNumber);
assertEquals(0, Json.fromJson(finalSegments.get(0).getSegmentList(), SegmentsChange.class).getSegments().size());
assertEquals(0, finalFlags.size());
assertEquals(0, Json.fromJson(finalLargeSegments.get(0).getSegmentList(), SegmentsChange.class).getSegments().size());
assertEquals(-1, finalChangeNumber);
assertTrue(0L < mRoomDb.generalInfoDao()
.getByName("rolloutCacheLastClearTimestamp").getLongValue());
assertEquals("-1", mSinceFromUri.get());
}
private SplitFactory getSplitFactory(RolloutCacheConfiguration rolloutCacheConfiguration) {
final String url = mWebServer.url("/").url().toString();
ServiceEndpoints endpoints = ServiceEndpoints.builder()
.apiEndpoint(url).eventsEndpoint(url).build();
TestableSplitConfigBuilder builder = new TestableSplitConfigBuilder()
.serviceEndpoints(endpoints)
.streamingEnabled(false)
.featuresRefreshRate(9999)
.segmentsRefreshRate(9999)
.impressionsRefreshRate(9999)
.enableDebug()
.streamingEnabled(false);
if (rolloutCacheConfiguration != null) {
builder.rolloutCacheConfiguration(rolloutCacheConfiguration);
}
SplitClientConfig config = builder
.build();
return buildFactory(
dummyApiKey(), dummyUserKey(),
config, mContext, null, mRoomDb);
}
private void setupServer() {
mWebServer = new MockWebServer();
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
mRequestCountdownLatch.await();
if (request.getPath().contains("/" + IntegrationHelper.ServicePath.MEMBERSHIPS)) {
return new MockResponse().setResponseCode(200).setBody(randomizedAllSegments());
} else if (request.getPath().contains("/" + IntegrationHelper.ServicePath.SPLIT_CHANGES)) {
mSinceFromUri.compareAndSet(null, IntegrationHelper.getSinceFromUri(request.getRequestUrl().uri()));
return new MockResponse().setResponseCode(200)
.setBody(IntegrationHelper.emptySplitChanges(-1, 10000L));
} else {
return new MockResponse().setResponseCode(404);
}
}
};
mWebServer.setDispatcher(dispatcher);
}
private void preloadDb(Long updateTimestamp, Long lastClearTimestamp, Long changeNumber) {
List<Split> splitListFromJson = getSplitListFromJson();
List<SplitEntity> entities = splitListFromJson.stream()
.filter(split -> split.name != null)
.map(split -> {
SplitEntity result = new SplitEntity();
result.setName(split.name);
result.setBody(Json.toJson(split));
return result;
}).collect(Collectors.toList());
if (updateTimestamp != null) {
mRoomDb.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.SPLITS_UPDATE_TIMESTAMP, updateTimestamp));
}
if (lastClearTimestamp != null) {
mRoomDb.generalInfoDao().update(new GeneralInfoEntity("rolloutCacheLastClearTimestamp", lastClearTimestamp));
}
if (changeNumber != null) {
mRoomDb.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, changeNumber));
}
MyLargeSegmentEntity largeSegment = new MyLargeSegmentEntity();
largeSegment.setSegmentList("{\"k\":[{\"n\":\"ls1\"},{\"n\":\"ls2\"}],\"cn\":null}");
largeSegment.setUserKey(dummyUserKey().matchingKey());
largeSegment.setUpdatedAt(System.currentTimeMillis());
mRoomDb.myLargeSegmentDao().update(largeSegment);
MySegmentEntity segment = new MySegmentEntity();
segment.setSegmentList("{\"k\":[{\"n\":\"s1\"},{\"n\":\"s2\"}],\"cn\":null}");
segment.setUserKey(dummyUserKey().matchingKey());
segment.setUpdatedAt(System.currentTimeMillis());
mRoomDb.mySegmentDao().update(segment);
mRoomDb.splitDao().insert(entities);
}
private List<Split> getSplitListFromJson() {
FileHelper fileHelper = new FileHelper();
String s = fileHelper.loadFileContent(mContext, "attributes_test_split_change.json");
SplitChange changes = IntegrationHelper.getChangeFromJsonString(s);
return changes.splits;
}
}