-
Notifications
You must be signed in to change notification settings - Fork 71
feat: Add support for high resolution fractional and arbitrary targeting key types #1740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ce6fab9
d42a51d
411d642
9586f8c
1ae9e2f
a14ed15
c5f4e15
195a836
40c6ee7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,36 @@ | ||
| package dev.openfeature.contrib.tools.flagd.core.targeting; | ||
|
|
||
| import static dev.openfeature.contrib.tools.flagd.core.targeting.Operator.*; | ||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.junit.jupiter.api.Named.named; | ||
| import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import io.github.jamsesso.jsonlogic.JsonLogicException; | ||
| import io.github.jamsesso.jsonlogic.evaluator.JsonLogicEvaluationException; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.converter.ArgumentConversionException; | ||
| import org.junit.jupiter.params.converter.ConvertWith; | ||
| import org.junit.jupiter.params.converter.TypedArgumentConverter; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| class FractionalTest { | ||
|
|
||
|
|
@@ -47,6 +56,60 @@ void validate_emptyJson_targetingReturned(@ConvertWith(FileContentConverter.clas | |
| assertEquals(testData.result, evaluate); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(ints = {0, 1, -1, Integer.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MIN_VALUE, Integer.MIN_VALUE + 1}) | ||
| void edgeCasesDoNotThrow(int hash) throws JsonLogicException { | ||
| int totalWeight = 8; | ||
| int buckets = 4; | ||
| List<Fractional.FractionProperty> bucketsList = new ArrayList<>(buckets); | ||
| for (int i = 0; i < buckets; i++) { | ||
| bucketsList.add(new Fractional.FractionProperty(List.of("bucket" + i, totalWeight / buckets), "")); | ||
| } | ||
|
|
||
| AtomicReference<String> result = new AtomicReference<>(); | ||
| assertDoesNotThrow(() -> result.set(Fractional.distributeValueFromHash(hash, bucketsList, totalWeight, ""))); | ||
|
|
||
| assertNotNull(result.get()); | ||
| assertTrue(result.get().startsWith("bucket")); | ||
| } | ||
|
|
||
| @Test | ||
| void statistics() throws JsonLogicException { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tests the statistical distribution. The test takes 340ms on my machine, I think we can live with it, but I can also delete or disable it if we want to |
||
| int totalWeight = Integer.MAX_VALUE; | ||
| int buckets = 16; | ||
| int[] hits = new int[buckets]; | ||
| List<Fractional.FractionProperty> bucketsList = new ArrayList<>(buckets); | ||
| int weight = totalWeight / buckets; | ||
| for (int i = 0; i < buckets - 1; i++) { | ||
| bucketsList.add(new Fractional.FractionProperty(List.of("" + i, weight), "")); | ||
| } | ||
| bucketsList.add( | ||
| new Fractional.FractionProperty(List.of("" + (buckets - 1), totalWeight - weight * (buckets - 1)), "")); | ||
|
|
||
| for (long i = Integer.MIN_VALUE; i <= Integer.MAX_VALUE; i += 127) { | ||
| String bucketStr = Fractional.distributeValueFromHash((int) i, bucketsList, totalWeight, ""); | ||
| int bucket = Integer.parseInt(bucketStr); | ||
| hits[bucket]++; | ||
| } | ||
|
|
||
| int min = Integer.MAX_VALUE; | ||
| int max = Integer.MIN_VALUE; | ||
| for (int i = 0; i < hits.length; i++) { | ||
| int current = hits[i]; | ||
| if (current < min) { | ||
| min = current; | ||
| } | ||
| if (current > max) { | ||
| max = current; | ||
| } | ||
| } | ||
|
|
||
| int delta = max - min; | ||
| assertTrue( | ||
| delta < 3, | ||
| "Delta should be less than 3, but was " + delta + ". Distributions: " + Arrays.toString(hits)); | ||
| } | ||
|
|
||
| public static Stream<?> allFilesInDir() throws IOException { | ||
| return Files.list(Paths.get("src", "test", "resources", "fractional")) | ||
| .map(path -> arguments(named(path.getFileName().toString(), path))); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "rule": [ | ||
| true, | ||
| [ | ||
| "blue", | ||
| 50 | ||
| ], | ||
| [ | ||
| "green", | ||
| 70 | ||
| ] | ||
| ], | ||
| "result": "green" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "rule": [ | ||
| 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.9, | ||
| [ | ||
| "blue", | ||
| 50 | ||
| ], | ||
| [ | ||
| "green", | ||
| 70 | ||
| ] | ||
| ], | ||
| "result": "green" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "rule": [ | ||
| 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999, | ||
| [ | ||
| "blue", | ||
| 50 | ||
| ], | ||
| [ | ||
| "green", | ||
| 70 | ||
| ] | ||
| ], | ||
| "result": "blue" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,5 +10,5 @@ | |
| 50 | ||
| ] | ||
| ], | ||
| "result": "blue" | ||
| "result": "red" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In some cases, we will compute a different bucket now |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "rule": [ | ||
| "some string", | ||
| [ | ||
| "blue", | ||
| 50 | ||
| ], | ||
| [ | ||
| "green", | ||
| 70 | ||
| ] | ||
| ], | ||
| "result": "blue" | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only tests that we will find a bucket for edge case inputs, but it will not validate the correctness