Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Sep 4, 2023
1 parent d655538 commit 8ef9ed1
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ public enum Method {
TREATMENT_WITH_CONFIG("getTreatmentWithConfig"),
TREATMENTS_WITH_CONFIG("getTreatmentsWithConfig"),
TREATMENTS_BY_FLAG_SET("getTreatmentsByFlagSet"),
TREATMENTS_BY_FLAG_SETS("getTreatmentsByFlagSets"),
TREATMENTS_WITH_CONFIG_BY_FLAG_SET("getTreatmentsWithConfigByFlagSet"),
TREATMENTS_WITH_CONFIG_BY_FLAG_SETS("getTreatmentsWithConfigByFlagSets"),
TRACK("track");

private final String _method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private static class ValidationTag {
public static final String GET_TREATMENT_WITH_CONFIG = "getTreatmentWithConfig";
public static final String GET_TREATMENTS_WITH_CONFIG = "getTreatmentsWithConfig";
public static final String GET_TREATMENTS_BY_FLAG_SET = "getTreatmentsByFlagSet";
public static final String GET_TREATMENTS_BY_FLAG_SETS = "getTreatmentsByFlagSets";
public static final String GET_TREATMENTS_BY_FLAG_SETS = "getFeatureFlagNamesToEvaluate";
public static final String GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET = "getTreatmentsWithConfigByFlagSet";
public static final String GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS = "getTreatmentsWithConfigByFlagSets";
}
Expand Down Expand Up @@ -182,90 +182,42 @@ public Map<String, SplitResult> getTreatmentsWithConfig(List<String> splits, Map

@Override
public Map<String, String> getTreatmentsByFlagSet(@NonNull String flagSet, @Nullable Map<String, Object> attributes, boolean isClientDestroyed) {
String validationTag = ValidationTag.GET_TREATMENTS_BY_FLAG_SET;
if (isClientDestroyed) {
mValidationLogger.e(CLIENT_DESTROYED_MESSAGE, validationTag);
return new HashMap<>();
}

if (!mFlagSetsValidator.isValid(flagSet)) {
mValidationLogger.e("you passed " + flagSet + " which is not valid.", validationTag);
return new HashMap<>();
}

if (!mConfiguredFlagSets.isEmpty() && !mConfiguredFlagSets.contains(flagSet)) {
mValidationLogger.e("you passed " + flagSet + " which is not defined in the configuration.", validationTag);
return new HashMap<>();
}

long start = System.currentTimeMillis();

Map<String, String> result = new HashMap<>();
Set<String> featureFlagNamesInSet = mSplitsStorage.getNamesByFlagSets(Collections.singletonList(flagSet));
for (String featureFlagName : featureFlagNamesInSet) {
result.put(featureFlagName, getTreatmentWithConfigWithoutMetrics(featureFlagName, attributes, validationTag).treatment());
try {
return evaluateFeaturesGeneric(getFeatureFlagNamesToEvaluate(ValidationTag.GET_TREATMENTS_BY_FLAG_SET, Collections.singletonList(flagSet), isClientDestroyed), attributes, ValidationTag.GET_TREATMENTS_BY_FLAG_SET, SplitResult::treatment);
} finally {
recordLatency(Method.TREATMENTS_BY_FLAG_SET, start);
}

recordLatency(Method.TREATMENTS_BY_FLAG_SET, start);

return result;
}

@Override
public Map<String, String> getTreatmentsByFlagSets(@NonNull List<String> flagSets, @Nullable Map<String, Object> attributes, boolean isClientDestroyed) {
String validationTag = ValidationTag.GET_TREATMENTS_BY_FLAG_SETS;
if (isClientDestroyed) {
mValidationLogger.e(CLIENT_DESTROYED_MESSAGE, validationTag);
return new HashMap<>();
}

if (flagSets == null) {
return new HashMap<>();
}

long start = System.currentTimeMillis();

List<String> setsToEvaluate = new ArrayList<>();
for (String flagSet : flagSets) {
if (setsToEvaluate.contains(flagSet)) {
continue;
}

boolean isValid = mFlagSetsValidator.isValid(flagSet);
boolean isConfigured = mConfiguredFlagSets.isEmpty() || mConfiguredFlagSets.contains(flagSet);

if (!isValid) {
mValidationLogger.e("you passed " + flagSet + " which is not valid.", validationTag);
} else if (!isConfigured) {
mValidationLogger.e("you passed " + flagSet + " which is not defined in the configuration.", validationTag);
} else {
setsToEvaluate.add(flagSet);
}
}

if (setsToEvaluate.isEmpty()) {
return new HashMap<>();
}

Map<String, String> result = new HashMap<>();
Set<String> featureFlagNamesInSet = mSplitsStorage.getNamesByFlagSets(setsToEvaluate);
for (String featureFlagName : featureFlagNamesInSet) {
result.put(featureFlagName, getTreatmentWithConfigWithoutMetrics(featureFlagName, attributes, validationTag).treatment());
try {
return evaluateFeaturesGeneric(getFeatureFlagNamesToEvaluate(ValidationTag.GET_TREATMENTS_BY_FLAG_SETS, flagSets, isClientDestroyed), attributes, ValidationTag.GET_TREATMENTS_BY_FLAG_SETS, SplitResult::treatment);
} finally {
recordLatency(Method.TREATMENTS_BY_FLAG_SETS, start);
}

recordLatency(Method.TREATMENTS_BY_FLAG_SET, start);

return result;
}

@Override
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSet(@NonNull String flagSet, @Nullable Map<String, Object> attributes, boolean isClientDestroyed) {
return null;
long start = System.currentTimeMillis();
try {
return evaluateFeaturesGeneric(getFeatureFlagNamesToEvaluate(ValidationTag.GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET, Collections.singletonList(flagSet), isClientDestroyed), attributes, ValidationTag.GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET, splitResult -> splitResult);
} finally {
recordLatency(Method.TREATMENTS_WITH_CONFIG_BY_FLAG_SET, start);
}
}

@Override
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSets(@NonNull List<String> flagSets, @Nullable Map<String, Object> attributes, boolean isClientDestroyed) {
return null;
long start = System.currentTimeMillis();
try {
return evaluateFeaturesGeneric(getFeatureFlagNamesToEvaluate(ValidationTag.GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, flagSets, isClientDestroyed), attributes, ValidationTag.GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, splitResult -> splitResult);
} finally {
recordLatency(Method.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, start);
}
}

private SplitResult getTreatmentWithConfigWithoutMetrics(String split, Map<String, Object> attributes, String validationTag) {
Expand Down Expand Up @@ -387,4 +339,55 @@ private EvaluationResult evaluateIfReady(String splitName,
private void recordLatency(Method treatment, long startTime) {
mTelemetryStorageProducer.recordLatency(treatment, System.currentTimeMillis() - startTime);
}

@NonNull
private Set<String> getFeatureFlagNamesToEvaluate(String validationTag,
@NonNull List<String> flagSets,
boolean isClientDestroyed) {
if (isClientDestroyed) {
mValidationLogger.e(CLIENT_DESTROYED_MESSAGE, validationTag);
return new HashSet<>();
}

if (flagSets == null) {
return new HashSet<>();
}

List<String> setsToEvaluate = new ArrayList<>();
for (String flagSet : flagSets) {
if (setsToEvaluate.contains(flagSet)) {
continue;
}

boolean isValid = mFlagSetsValidator.isValid(flagSet);
boolean isConfigured = mConfiguredFlagSets.isEmpty() || mConfiguredFlagSets.contains(flagSet);

if (!isValid) {
mValidationLogger.e("you passed " + flagSet + " which is not valid.", validationTag);
} else if (!isConfigured) {
mValidationLogger.e("you passed " + flagSet + " which is not defined in the configuration.", validationTag);
} else {
setsToEvaluate.add(flagSet);
}
}

if (setsToEvaluate.isEmpty()) {
return new HashSet<>();
}

return mSplitsStorage.getNamesByFlagSets(setsToEvaluate);
}

private <T> Map<String, T> evaluateFeaturesGeneric(Set<String> names, @Nullable Map<String, Object> attributes, String validationTag, ResultTransformer<T> transformer) {
Map<String, T> result = new HashMap<>();
for (String featureFlagName : names) {
SplitResult splitResult = getTreatmentWithConfigWithoutMetrics(featureFlagName, attributes, validationTag);
result.put(featureFlagName, transformer.transform(splitResult));
}
return result;
}

private interface ResultTransformer<T> {
T transform(SplitResult splitResult);
}
}
Loading

0 comments on commit 8ef9ed1

Please sign in to comment.