Skip to content

Commit

Permalink
fix: start work on reproducing client-spec tests in java tests to sim…
Browse files Browse the repository at this point in the history
…plify debugging
  • Loading branch information
chriswk committed Oct 26, 2023
1 parent e0b244d commit 1aac936
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<version.junit5>5.10.0</version.junit5>
<version.okhttp>4.10.0</version.okhttp>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.unleash.specification>4.5.1</version.unleash.specification>
<version.unleash.specification>5.0.0</version.unleash.specification>
<arguments />
<version.jackson>2.14.3</version.jackson>
</properties>
Expand Down Expand Up @@ -111,7 +111,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.4.0</version>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public boolean isEnabled(final Map<String, String> parameters, UnleashContext un
final int percentage = StrategyUtils.getPercentage(parameters.get(PERCENTAGE));
final String groupId = parameters.getOrDefault(GROUP_ID, "");

final int normalizedSessionId = StrategyUtils.getNormalizedNumber(sessionId.get(), groupId, 0);
final int normalizedSessionId =
StrategyUtils.getNormalizedNumber(sessionId.get(), groupId, 0);

return percentage > 0 && normalizedSessionId <= percentage;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/getunleash/strategy/StrategyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public static int getNormalizedNumber(String identifier, String groupId, long se
return getNormalizedNumber(identifier, groupId, ONE_HUNDRED, seed);
}

public static int getNormalizedNumber(String identifier, String groupId, int normalizer, long seed) {
public static int getNormalizedNumber(
String identifier, String groupId, int normalizer, long seed) {
byte[] value = (groupId + ':' + identifier).getBytes();
long hash = Murmur3.hash_x86_32(value, value.length, seed);
return (int) (hash % normalizer) + 1;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/getunleash/variant/VariantUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.getunleash.Variant;
import io.getunleash.lang.Nullable;
import io.getunleash.strategy.StrategyUtils;

import java.util.*;
import java.util.List;
import java.util.function.Predicate;
Expand Down Expand Up @@ -119,10 +118,11 @@ public static Variant selectVariant(
StrategyUtils.getNormalizedNumber(
getSeed(context, customStickiness),
parameters.get(GROUP_ID_KEY),
totalWeight, VARIANT_NORMALIZATION_SEED);
totalWeight,
VARIANT_NORMALIZATION_SEED);

int counter = 0;
for (VariantDefinition variant : variants) {
l for (VariantDefinition variant : variants) {
if (variant.getWeight() != 0) {
counter += variant.getWeight();
if (counter >= target) {
Expand Down
31 changes: 20 additions & 11 deletions src/test/java/io/getunleash/strategy/StrategyUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.google.errorprone.annotations.Var;
import io.getunleash.variant.VariantUtil;
import java.util.UUID;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class StrategyUtilsTest {

@Test
Expand All @@ -22,19 +18,32 @@ public void normalized_values_are_the_same_across_node_java_and_go_clients() {

@Test
public void normalized_values_with_variant_seed_are_the_same_across_node_java() {
assertThat(StrategyUtils.getNormalizedNumber("123", "gr1", VariantUtil.VARIANT_NORMALIZATION_SEED)).isEqualTo(96);
assertThat(StrategyUtils.getNormalizedNumber("999", "groupX", VariantUtil.VARIANT_NORMALIZATION_SEED)).isEqualTo(60);
assertThat(
StrategyUtils.getNormalizedNumber(
"123", "gr1", VariantUtil.VARIANT_NORMALIZATION_SEED))
.isEqualTo(96);
assertThat(
StrategyUtils.getNormalizedNumber(
"999", "groupX", VariantUtil.VARIANT_NORMALIZATION_SEED))
.isEqualTo(60);
}

@Test
public void selecting_ten_percent_of_users_and_then_finding_variants_should_still_have_variants_evenly_distributed() {
public void
selecting_ten_percent_of_users_and_then_finding_variants_should_still_have_variants_evenly_distributed() {
int ones = 0, twos = 0, threes = 0, loopSize = 500000, selectionSize = 0;
for (int i = 0; i < loopSize; i++) {
String id = UUID.randomUUID().toString();
int featureRollout = StrategyUtils.getNormalizedNumber(id, "feature.name.that.is.quite.long", 0);
int featureRollout =
StrategyUtils.getNormalizedNumber(id, "feature.name.that.is.quite.long", 0);
if (featureRollout < 11) {
int variantGroup = StrategyUtils.getNormalizedNumber(id, "feature.name.that.is.quite.long", 1000, VariantUtil.VARIANT_NORMALIZATION_SEED);
if(variantGroup <= 333) {
int variantGroup =
StrategyUtils.getNormalizedNumber(
id,
"feature.name.that.is.quite.long",
1000,
VariantUtil.VARIANT_NORMALIZATION_SEED);
if (variantGroup <= 333) {
ones++;
} else if (variantGroup <= 666) {
twos++;
Expand Down
55 changes: 49 additions & 6 deletions src/test/java/io/getunleash/variant/VariantUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import io.getunleash.Variant;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -81,7 +80,7 @@ public void should_return_variant2() {
new FeatureToggle(
"test.variants", true, asList(defaultStrategy), asList(v1, v2, v3));

UnleashContext context = UnleashContext.builder().userId("163").build();
UnleashContext context = UnleashContext.builder().userId("80").build();

Variant variant = VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT);

Expand All @@ -98,7 +97,7 @@ public void should_return_variant3() {
new FeatureToggle(
"test.variants", true, asList(defaultStrategy), asList(v1, v2, v3));

UnleashContext context = UnleashContext.builder().userId("40").build();
UnleashContext context = UnleashContext.builder().userId("163").build();

Variant variant = VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT);

Expand Down Expand Up @@ -233,7 +232,7 @@ public void should_return_same_variant_when_stickiness_is_set_to_default() {
new FeatureToggle(
"test.variants", true, asList(defaultStrategy), asList(v1, v2, v3));

UnleashContext context = UnleashContext.builder().userId("40").build();
UnleashContext context = UnleashContext.builder().userId("163;").build();
List<Variant> results =
IntStream.range(0, 500)
.mapToObj(i -> VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT))
Expand All @@ -243,4 +242,48 @@ public void should_return_same_variant_when_stickiness_is_set_to_default() {
(Consumer<Variant>)
variant -> assertThat(variant.getName()).isEqualTo(v3.getName()));
}

@Test
public void custom_stickiness_variants() {
Map<String, String> parameters = new HashMap<>();
parameters.put("rollout", "100");
parameters.put("stickiness", "customField");
parameters.put("groupId", "Feature.flexible.rollout.custom.stickiness_100");
ActivationStrategy flexibleRollout = new ActivationStrategy("flexibleRollout", parameters);
List<VariantDefinition> variants = new ArrayList<>();
variants.add(new VariantDefinition("blue", 25, new Payload("string", "val1"), Collections.emptyList(), "customField"));
variants.add(new VariantDefinition("red", 25, new Payload("string", "val1"), Collections.emptyList(), "customField"));
variants.add(new VariantDefinition("green", 25, new Payload("string", "val1"), Collections.emptyList(), "customField"));
variants.add(new VariantDefinition("yellow", 25, new Payload("string", "val1"), Collections.emptyList(), "customField"));
FeatureToggle toggle = new FeatureToggle("Feature.flexible.rollout.custom.stickiness_100", true, asList(flexibleRollout), variants);
Variant variant = VariantUtil.selectVariant(toggle, UnleashContext.builder().addProperty("customField", "528").build(), DISABLED_VARIANT);
assertThat(variant.getName()).isEqualTo("red");
}

@Test
public void feature_variants_variant_b_client_spec_tests() {
List<VariantDefinition> variants = new ArrayList<>();
variants.add(new VariantDefinition("variant1", 1, new Payload("string", "val1"), Collections.emptyList()));
variants.add(new VariantDefinition("variant2", 1, new Payload("string", "val1"), Collections.emptyList()));
FeatureToggle toggle = new FeatureToggle("Feature.Variants.B", true, Collections.emptyList(), variants);
Variant variantUser2 = VariantUtil.selectVariant(toggle, UnleashContext.builder().userId("2").build(), DISABLED_VARIANT);
assertThat(variantUser2.getName()).isEqualTo("variant2");
Variant variantUser0 = VariantUtil.selectVariant(toggle, UnleashContext.builder().userId("0").build(), DISABLED_VARIANT);
assertThat(variantUser0.getName()).isEqualTo("variant1");
}

@Test
public void feature_variants_variant_c_client_spec_tests() {
List<VariantDefinition> variants = new ArrayList<>();
variants.add(new VariantDefinition("variant1", 33, new Payload("string", "val1"), Collections.emptyList()));
variants.add(new VariantDefinition("variant2", 33, new Payload("string", "val1"), Collections.emptyList()));
variants.add(new VariantDefinition("variant3", 33, new Payload("string", "val1"), Collections.emptyList()));
FeatureToggle toggle = new FeatureToggle("Feature.Variants.C", true, Collections.emptyList(), variants);
Variant variantUser269 = VariantUtil.selectVariant(toggle, UnleashContext.builder().userId("269").build(), DISABLED_VARIANT);
assertThat(variantUser269.getName()).isEqualTo("variant1");
Variant variantUser320 = VariantUtil.selectVariant(toggle, UnleashContext.builder().userId("268").build(), DISABLED_VARIANT);
assertThat(variantUser320.getName()).isEqualTo("variant2");
Variant variantUser729 = VariantUtil.selectVariant(toggle, UnleashContext.builder().userId("569").build(), DISABLED_VARIANT);
assertThat(variantUser729.getName()).isEqualTo("variant3");
}
}

0 comments on commit 1aac936

Please sign in to comment.