Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Sep 12, 2023
1 parent a16d8f4 commit 14b3116
Showing 1 changed file with 47 additions and 113 deletions.
160 changes: 47 additions & 113 deletions src/androidTest/java/tests/integration/sets/FlagSetsStreamingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,16 @@ public void setUp() {
}

@Test
public void sdkWithoutSetsConfiguredDoesExcludeUpdates() throws IOException, InterruptedException {
/*
* Initialize a factory with streaming enabled and no sets.
*
* Receive notification with new feature flag with no sets.
*
* Verify that the feature flag is added.
*/
public void sdkWithoutSetsConfiguredDoesNotExcludeUpdates() throws IOException, InterruptedException {
// 1. Initialize a factory with streaming enabled and no sets.
LinkedBlockingDeque<String> mStreamingData = new LinkedBlockingDeque<>();
SplitClient readyClient = getReadyClient(mContext, mRoomDb, mStreamingData);

int initialSplitsSize = mRoomDb.splitDao().getAll().size();

// set up update listener
CountDownLatch updateLatch = new CountDownLatch(1);
readyClient.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(updateLatch));

// push change
// 2. Receive notification with new feature flag with no sets.
pushToStreaming(mStreamingData, noSetsSplitChange);
boolean updateAwait = updateLatch.await(5, TimeUnit.SECONDS);

Expand All @@ -97,128 +89,54 @@ public void sdkWithoutSetsConfiguredDoesExcludeUpdates() throws IOException, Int

@Test
public void sdkWithSetsConfiguredDeletedDueToEmptySets() throws IOException, InterruptedException {
/*
* Initialize a factory with set_1 & set_2 sets configured.
*
* 1. Receive a SPLIT_UPDATE with {name:"test", "sets":["set_1", "set_2"]}. It should process it since is part of the config.Sets
*
* 2. Receive a SPLIT_UPDATE with {name:"test", "sets":["set_1"]}. It should process it since is still part of the config.Sets
*
* 3. Receive a SPLIT_UPDATE with {name:"test", "sets":[]}. The featureFlag should be removed.
*
*/
LinkedBlockingDeque<String> streamingData = new LinkedBlockingDeque<>();
SplitClient readyClient = getReadyClient(mContext, mRoomDb, streamingData, "set_1", "set_2");

// 1. Receive a SPLIT_UPDATE with {name:"test", "sets":["set_1", "set_2"]}
CountDownLatch firstUpdate = new CountDownLatch(1);
readyClient.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(firstUpdate));
pushToStreaming(streamingData, splitChange2);
boolean firstUpdateAwait = firstUpdate.await(5, TimeUnit.SECONDS);
List<SplitEntity> entities = mRoomDb.splitDao().getAll();
boolean firstUpdateStored = entities.size() == 1 && entities.get(0).getBody().contains("\"sets\":[\"set_1\",\"set_2\"]") &&
entities.get(0).getBody().contains("\"name\":\"workm\"");
// 1. Receive a SPLIT_UPDATE with "sets":["set_1", "set_2"]
boolean firstChange = processUpdate(readyClient, streamingData, splitChange2, "\"sets\":[\"set_1\",\"set_2\"]", "\"name\":\"workm\"");

// 2. Receive a SPLIT_UPDATE with {name:"test", "sets":["set_1"]}
CountDownLatch secondUpdate = new CountDownLatch(1);
readyClient.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(secondUpdate));
pushToStreaming(streamingData, splitChange3);
boolean secondUpdateAwait = secondUpdate.await(5, TimeUnit.SECONDS);
entities = mRoomDb.splitDao().getAll();
boolean secondUpdateStored = entities.size() == 1 && entities.get(0).getBody().contains("\"sets\":[\"set_1\"]") &&
entities.get(0).getBody().contains("\"name\":\"workm\"");
// 2. Receive a SPLIT_UPDATE with "sets":["set_1"]
boolean secondChange = processUpdate(readyClient, streamingData, splitChange3, "\"sets\":[\"set_1\"]", "\"name\":\"workm\"");

// 3. Receive a SPLIT_UPDATE with {name:"test", "sets":[]}
CountDownLatch thirdUpdate = new CountDownLatch(1);
readyClient.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(thirdUpdate));
pushToStreaming(streamingData, splitChange4None);
boolean thirdUpdateAwait = thirdUpdate.await(5, TimeUnit.SECONDS);
entities = mRoomDb.splitDao().getAll();
boolean thirdUpdateStored = entities.isEmpty();
// 3. Receive a SPLIT_UPDATE with "sets":[]
boolean thirdChange = processUpdate(readyClient, streamingData, splitChange4None);

assertTrue(firstUpdateAwait);
assertTrue(firstUpdateStored);
assertTrue(secondUpdateAwait);
assertTrue(secondUpdateStored);
assertTrue(thirdUpdateAwait);
assertTrue(thirdUpdateStored);
assertTrue(firstChange);
assertTrue(secondChange);
assertTrue(thirdChange);
}

@Test
public void sdkWithSetsConfiguredDeletedDueToNonMatchingSets() throws IOException, InterruptedException {
/*
* Initialize a factory with set_1 & set_2 sets configured.
*
* 1. Receive a SPLIT_UPDATE with {name:"test", "sets":["set_1", "set_2"]}. It should process it since is part of the config.Sets
*
* 2. Receive a SPLIT_UPDATE with "sets":["set_1"]. The feature flag should be updated.
*
* 3. Receive a SPLIT_UPDATE with "sets":["set_3"]. The feature flag should be removed.
*
* 4. Receive a SPLIT_UPDATE with "sets":["set_3", "set_4"] No changes in storage.
*/

LinkedBlockingDeque<String> streamingData = new LinkedBlockingDeque<>();
SplitClient readyClient = getReadyClient(mContext, mRoomDb, streamingData, "set_1", "set_2");

// 1. Receive a SPLIT_UPDATE with "sets":["set_1", "set_2"]
CountDownLatch firstUpdate = new CountDownLatch(1);
readyClient.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(firstUpdate));
pushToStreaming(streamingData, splitChange2);
boolean firstUpdateAwait = firstUpdate.await(5, TimeUnit.SECONDS);
List<SplitEntity> entities = mRoomDb.splitDao().getAll();
boolean firstUpdateStored = entities.size() == 1 && entities.get(0).getBody().contains("\"sets\":[\"set_1\",\"set_2\"]") &&
entities.get(0).getBody().contains("\"name\":\"workm\"");
boolean firstChange = processUpdate(readyClient, streamingData, splitChange2, "\"sets\":[\"set_1\",\"set_2\"]", "\"name\":\"workm\"");

// 2. Receive a SPLIT_UPDATE with "sets":["set_1"]
CountDownLatch secondUpdate = new CountDownLatch(1);
readyClient.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(secondUpdate));
pushToStreaming(streamingData, splitChange3);
boolean secondUpdateAwait = secondUpdate.await(5, TimeUnit.SECONDS);
entities = mRoomDb.splitDao().getAll();
boolean secondUpdateStored = entities.size() == 1 && entities.get(0).getBody().contains("\"sets\":[\"set_1\"]") &&
entities.get(0).getBody().contains("\"name\":\"workm\"");
boolean secondChange = processUpdate(readyClient, streamingData, splitChange3, "\"sets\":[\"set_1\"]", "\"name\":\"workm\"");

// 3. Receive a SPLIT_UPDATE with "sets":["set_3"]
CountDownLatch thirdUpdate = new CountDownLatch(1);
readyClient.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(thirdUpdate));
pushToStreaming(streamingData, splitChange4);
boolean thirdUpdateAwait = thirdUpdate.await(5, TimeUnit.SECONDS);
entities = mRoomDb.splitDao().getAll();
boolean thirdUpdateStored = entities.size() == 0;
boolean thirdChange = processUpdate(readyClient, streamingData, splitChange4);

// 4. Receive a SPLIT_UPDATE with "sets":["set_3", "set_4"]
CountDownLatch fourthUpdate = new CountDownLatch(1);
readyClient.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(fourthUpdate));
pushToStreaming(streamingData, splitChange5);
boolean fourthUpdateAwait = fourthUpdate.await(5, TimeUnit.SECONDS);
entities = mRoomDb.splitDao().getAll();
boolean fourthUpdateStored = entities.size() == 0;
boolean fourthChange = processUpdate(readyClient, streamingData, splitChange5);

assertTrue(firstUpdateAwait);
assertTrue(firstUpdateStored);
assertTrue(secondUpdateAwait);
assertTrue(secondUpdateStored);
assertTrue(thirdUpdateAwait);
assertTrue(thirdUpdateStored);
assertTrue(fourthUpdateAwait);
assertTrue(fourthUpdateStored);
assertTrue(firstChange);
assertTrue(secondChange);
assertTrue(thirdChange);
assertTrue(fourthChange);
}

@Test
public void sdkWithSetsReceivesSplitKill() throws IOException, InterruptedException {
/*
* Initialize a factory with set_1 & set_2 sets configured.
*
* 1. Receive a SPLIT_UPDATE with {name:"test", "sets":["set_1", "set_2"]}. It should process it since is part of the config.Sets
*
* 2. Receive a SPLIT_KILL with {cn:2, name:"workm", "defaultTreatment":"off" }. The featureFlag should be killed and a fetch should be performed.
*/

// 1. Initialize a factory with set_1 & set_2 sets configured.
LinkedBlockingDeque<String> streamingData = new LinkedBlockingDeque<>();
SplitClient readyClient = getReadyClient(mContext, mRoomDb, streamingData, "set_1", "set_2");

// 1. Receive a SPLIT_UPDATE with {name:"test", "sets":["set_1", "set_2"]}
// 2. Receive a SPLIT_UPDATE with "sets":["set_1", "set_2"]
CountDownLatch firstUpdate = new CountDownLatch(1);
readyClient.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(firstUpdate));
pushToStreaming(streamingData, splitChange2);
Expand All @@ -228,7 +146,7 @@ public void sdkWithSetsReceivesSplitKill() throws IOException, InterruptedExcept
entities.get(0).getBody().contains("\"killed\":false") &&
entities.get(0).getBody().contains("\"name\":\"workm\"");

// 2. Receive a SPLIT_KILL with {cn:2, name:"test", "defaultTreatment":"off" }
// 3. Receive a SPLIT_KILL for workm
CountDownLatch secondUpdate = new CountDownLatch(1);
readyClient.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(secondUpdate));
pushToStreaming(streamingData, IntegrationHelper.splitKill("5", "workm"));
Expand All @@ -237,7 +155,7 @@ public void sdkWithSetsReceivesSplitKill() throws IOException, InterruptedExcept
boolean secondUpdateStored = entities.size() == 1 && entities.get(0).getBody().contains("\"killed\":true") &&
entities.get(0).getBody().contains("\"name\":\"workm\"");

// 3. A fetch is triggered due to the SPLIT_KILL
// 4. A fetch is triggered due to the SPLIT_KILL
boolean correctAmountOfChanges = mSplitChangesHits.get() == 3;

assertTrue(firstUpdateAwait);
Expand All @@ -249,17 +167,14 @@ public void sdkWithSetsReceivesSplitKill() throws IOException, InterruptedExcept

@Test
public void sdkWithSetsReceivesSplitKillForNonExistingFeatureFlag() throws IOException, InterruptedException {
/*
* Initialize a factory with set_1 & set_2 sets configured.
*
* 1. Receive a SPLIT_KILL with {cn:2, name:"workm", "defaultTreatment":"off" }. No changes in storage, a fetch should be performed.
*/

// 1. Initialize a factory with set_1 & set_2 sets configured.
LinkedBlockingDeque<String> streamingData = new LinkedBlockingDeque<>();
SplitClient readyClient = getReadyClient(mContext, mRoomDb, streamingData, "set_1", "set_2");

int initialEntities = mRoomDb.splitDao().getAll().size();

// 1. Receive a SPLIT_KILL with {cn:2, name:"test", "defaultTreatment":"off" }
// 2. Receive a SPLIT_KILL; storage is not modified since flag is not present.
CountDownLatch firstUpdate = new CountDownLatch(1);
readyClient.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(firstUpdate));
int initialChangesHits = mSplitChangesHits.get();
Expand All @@ -268,7 +183,7 @@ public void sdkWithSetsReceivesSplitKillForNonExistingFeatureFlag() throws IOExc
List<SplitEntity> entities = mRoomDb.splitDao().getAll();
boolean firstUpdateStored = entities.isEmpty();

// 2. A fetch is triggered due to the SPLIT_KILL
// 3. A fetch is triggered due to the SPLIT_KILL
int finalChangesHits = mSplitChangesHits.get();

assertFalse(firstUpdateAwait);
Expand Down Expand Up @@ -342,4 +257,23 @@ private static void pushToStreaming(LinkedBlockingDeque<String> streamingData, S
} catch (InterruptedException ignored) {
}
}

private boolean processUpdate(SplitClient client, LinkedBlockingDeque<String> streamingData, String splitChange, String... expectedContents) throws InterruptedException {
CountDownLatch updateLatch = new CountDownLatch(1);
client.on(SplitEvent.SDK_UPDATE, TestingHelper.testTask(updateLatch));
pushToStreaming(streamingData, splitChange);
boolean updateAwaited = updateLatch.await(5, TimeUnit.SECONDS);
List<SplitEntity> entities = mRoomDb.splitDao().getAll();

if (expectedContents == null || expectedContents.length == 0) {
return updateAwaited && entities.isEmpty();
}

boolean contentMatches = true;
for (String expected : expectedContents) {
contentMatches = contentMatches && entities.size() == 1 && entities.get(0).getBody().contains(expected);
}

return updateAwaited && contentMatches;
}
}

0 comments on commit 14b3116

Please sign in to comment.