-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: change seed for variantutils to ensure fair distribution
- Loading branch information
Showing
8 changed files
with
56 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 41 additions & 2 deletions
43
src/test/java/io/getunleash/strategy/StrategyUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,53 @@ | ||
package io.getunleash.strategy; | ||
|
||
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 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 | ||
public void normalized_values_are_the_same_across_node_java_and_go_clients() { | ||
assertEquals(73, StrategyUtils.getNormalizedNumber("123", "gr1")); | ||
assertEquals(25, StrategyUtils.getNormalizedNumber("999", "groupX")); | ||
assertEquals(73, StrategyUtils.getNormalizedNumber("123", "gr1", 0)); | ||
assertEquals(25, StrategyUtils.getNormalizedNumber("999", "groupX", 0)); | ||
} | ||
|
||
@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); | ||
} | ||
|
||
@Test | ||
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; | ||
Map<Integer, Integer> variantCounts = new HashMap<>(); | ||
for (int i = 0; i < loopSize; i++) { | ||
String id = UUID.randomUUID().toString(); | ||
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); | ||
variantCounts.compute(variantGroup, (k, v) -> { if (v == null) { return 1; } else { return v+1; }}); | ||
if(variantGroup <= 333) { | ||
ones++; | ||
} else if (variantGroup <= 666) { | ||
twos++; | ||
} else if (variantGroup <= 1000) { | ||
threes++; | ||
} | ||
selectionSize++; | ||
} | ||
} | ||
assertThat(ones / (double) (selectionSize)).isCloseTo(0.33, Offset.offset(0.01)); | ||
assertThat(twos / (double) (selectionSize)).isCloseTo(0.33, Offset.offset(0.01)); | ||
assertThat(threes / (double) (selectionSize)).isCloseTo(0.33, Offset.offset(0.01)); | ||
} | ||
} |