Skip to content

Commit

Permalink
[SDKS-7608] Add tests cases and pr suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
nmayorsplit committed Oct 20, 2023
1 parent e57d00f commit f7ab2a8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 33 deletions.
38 changes: 20 additions & 18 deletions client/src/main/java/io/split/storages/memory/InMemoryCacheImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,30 +187,32 @@ public Set<String> getSegments() {

private void addToFlagSets(ParsedSplit featureFlag) {
HashSet<String> sets = featureFlag.flagSets();
if( sets != null) {
for (String set: sets) {
if (!_flagSetsFilter.Intersect(set)) {
continue;
}
HashSet<String> features = _flagSets.get(set);
if (features == null) {
features = new HashSet<>();
}
features.add(featureFlag.feature());
_flagSets.put(set, features);
if(sets == null) {
return;
}
for (String set: sets) {
if (!_flagSetsFilter.Intersect(set)) {
continue;
}
HashSet<String> features = _flagSets.get(set);
if (features == null) {
features = new HashSet<>();
}
features.add(featureFlag.feature());
_flagSets.put(set, features);
}
}

private void removeFromFlagSets(String featureFlagName, HashSet<String> sets) {
if (sets != null) {
for (String set : sets) {
HashSet<String> features = _flagSets.get(set);
if (features != null) {
features.remove(featureFlagName);
_flagSets.put(set, features);
}
if(sets == null) {
return;
}
for (String set : sets) {
HashSet<String> features = _flagSets.get(set);
if (features == null){
continue;
}
features.remove(featureFlagName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void before() {

@Test
public void putAndGetSplit() {
ParsedSplit split = getParsedSplit("split_name");
ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name");
_cache.putMany(Stream.of(split).collect(Collectors.toList()));

ParsedSplit result = _cache.get("split_name");
Expand All @@ -47,8 +47,8 @@ public void putAndGetSplit() {

@Test
public void putDuplicateSplit() {
ParsedSplit split = getParsedSplit("split_name");
ParsedSplit split2 = getParsedSplit("split_name");
ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name");
ParsedSplit split2 = getParsedSplitWithFlagSetsSameStorage("split_name");
_cache.putMany(Stream.of(split, split2).collect(Collectors.toList()));

int result = _cache.getAll().size();
Expand All @@ -58,7 +58,7 @@ public void putDuplicateSplit() {

@Test
public void getInExistentSplit() {
ParsedSplit split = getParsedSplit("split_name");
ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name");
_cache.putMany(Stream.of(split).collect(Collectors.toList()));

ParsedSplit result = _cache.get("split_name_2");
Expand All @@ -67,15 +67,37 @@ public void getInExistentSplit() {

@Test
public void removeSplit() {
ParsedSplit split = getParsedSplit("split_name");
ParsedSplit split2 = getParsedSplit("split_name_2");
_cache.putMany(Stream.of(split, split2).collect(Collectors.toList()));
ParsedSplit splitWithFlagSetsSameStorage = getParsedSplitWithFlagSetsSameStorage("split_name");
ParsedSplit split2WithFlagSetsSameStorage = getParsedSplitWithFlagSetsSameStorage("split_name_2");
ParsedSplit splitWithFlagSetsNotSameStorage = getParsedSplitWithFlagSetsNotSameStorage("split_name_3");
ParsedSplit splitFlagSetsEmpty = getParsedSplitFlagSetsEmpty("split_name_4");
ParsedSplit splitFlagSetsNull = getParsedSplitFlagSetsNull("split_name_5");
_cache.putMany(Stream.of(splitWithFlagSetsSameStorage, split2WithFlagSetsSameStorage, splitWithFlagSetsNotSameStorage,
splitFlagSetsEmpty, splitFlagSetsNull).collect(Collectors.toList()));

int result = _cache.getAll().size();
Assert.assertEquals(2, result);
Assert.assertEquals(5, result);
Map<String, HashSet<String>> namesByFlagSets = _cache.getNamesByFlagSets(Arrays.asList("set1", "set2"));
Assert.assertTrue(namesByFlagSets.get("set1").contains("split_name"));
Assert.assertTrue(namesByFlagSets.get("set2").contains("split_name"));

_cache.remove("split_name");
result = _cache.getAll().size();
Assert.assertEquals(4, result);
namesByFlagSets = _cache.getNamesByFlagSets(Arrays.asList("set1", "set2"));
Assert.assertFalse(namesByFlagSets.get("set1").contains("split_name"));
Assert.assertFalse(namesByFlagSets.get("set2").contains("split_name"));

_cache.remove("split_name_3");
result = _cache.getAll().size();
Assert.assertEquals(3, result);

_cache.remove("split_name_4");
result = _cache.getAll().size();
Assert.assertEquals(2, result);

_cache.remove("split_name_5");
result = _cache.getAll().size();
Assert.assertEquals(1, result);

Assert.assertNull(_cache.get("split_name"));
Expand All @@ -95,10 +117,10 @@ public void setAndGetChangeNumber() {

@Test
public void getMany() {
ParsedSplit split = getParsedSplit("split_name_1");
ParsedSplit split2 = getParsedSplit("split_name_2");
ParsedSplit split3 = getParsedSplit("split_name_3");
ParsedSplit split4 = getParsedSplit("split_name_4");
ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name_1");
ParsedSplit split2 = getParsedSplitWithFlagSetsSameStorage("split_name_2");
ParsedSplit split3 = getParsedSplitWithFlagSetsSameStorage("split_name_3");
ParsedSplit split4 = getParsedSplitWithFlagSetsSameStorage("split_name_4");
_cache.putMany(Stream.of(split, split2, split3, split4).collect(Collectors.toList()));

List<String> names = new ArrayList<>();
Expand Down Expand Up @@ -150,13 +172,25 @@ public void testSegmentNames() {

}

private ParsedSplit getParsedSplit(String splitName) {
return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(Arrays.asList("set1", "set2", "set3")));
private ParsedSplit getParsedSplitWithFlagSetsSameStorage(String splitName) {
return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(Arrays.asList("set1", "set2")));
}

private ParsedSplit getParsedSplitWithFlagSetsNotSameStorage(String splitName) {
return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(Arrays.asList("set3")));
}

private ParsedSplit getParsedSplitFlagSetsNull(String splitName) {
return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, null);
}

private ParsedSplit getParsedSplitFlagSetsEmpty(String splitName) {
return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>());
}

@Test
public void testPutMany() {
_cache.putMany(Stream.of(getParsedSplit("split_name_1"),getParsedSplit("split_name_2"),getParsedSplit("split_name_3"),getParsedSplit("split_name_4")).collect(Collectors.toList()));
_cache.putMany(Stream.of(getParsedSplitWithFlagSetsSameStorage("split_name_1"), getParsedSplitWithFlagSetsSameStorage("split_name_2"), getParsedSplitWithFlagSetsSameStorage("split_name_3"), getParsedSplitWithFlagSetsSameStorage("split_name_4")).collect(Collectors.toList()));
List<String> names = Stream.of("split_name_1","split_name_2","split_name_3","split_name_4").collect(Collectors.toList());

Map<String, ParsedSplit> result = _cache.fetchMany(names);
Expand Down

0 comments on commit f7ab2a8

Please sign in to comment.