|
1 | 1 | package com.launchdarkly.client; |
2 | 2 |
|
3 | | -import static org.junit.Assert.assertEquals; |
| 3 | +import com.launchdarkly.client.VariationOrRollout.WeightedVariation; |
4 | 4 |
|
| 5 | +import org.hamcrest.Matchers; |
5 | 6 | import org.junit.Test; |
6 | 7 |
|
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 12 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | + |
| 15 | +@SuppressWarnings("javadoc") |
7 | 16 | public class VariationOrRolloutTest { |
| 17 | + @Test |
| 18 | + public void variationIndexIsReturnedForBucket() { |
| 19 | + LDUser user = new LDUser.Builder("userkey").build(); |
| 20 | + String flagKey = "flagkey"; |
| 21 | + String salt = "salt"; |
| 22 | + |
| 23 | + // First verify that with our test inputs, the bucket value will be greater than zero and less than 100000, |
| 24 | + // so we can construct a rollout whose second bucket just barely contains that value |
| 25 | + int bucketValue = (int)(VariationOrRollout.bucketUser(user, flagKey, "key", salt) * 100000); |
| 26 | + assertThat(bucketValue, greaterThanOrEqualTo(1)); |
| 27 | + assertThat(bucketValue, Matchers.lessThan(100000)); |
| 28 | + |
| 29 | + int badVariationA = 0, matchedVariation = 1, badVariationB = 2; |
| 30 | + List<WeightedVariation> variations = Arrays.asList( |
| 31 | + new WeightedVariation(badVariationA, bucketValue), // end of bucket range is not inclusive, so it will *not* match the target value |
| 32 | + new WeightedVariation(matchedVariation, 1), // size of this bucket is 1, so it only matches that specific value |
| 33 | + new WeightedVariation(badVariationB, 100000 - (bucketValue + 1))); |
| 34 | + VariationOrRollout vr = new VariationOrRollout(null, new VariationOrRollout.Rollout(variations, null)); |
| 35 | + |
| 36 | + Integer resultVariation = vr.variationIndexForUser(user, flagKey, salt); |
| 37 | + assertEquals(Integer.valueOf(matchedVariation), resultVariation); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void lastBucketIsUsedIfBucketValueEqualsTotalWeight() { |
| 42 | + LDUser user = new LDUser.Builder("userkey").build(); |
| 43 | + String flagKey = "flagkey"; |
| 44 | + String salt = "salt"; |
| 45 | + |
| 46 | + // We'll construct a list of variations that stops right at the target bucket value |
| 47 | + int bucketValue = (int)(VariationOrRollout.bucketUser(user, flagKey, "key", salt) * 100000); |
| 48 | + |
| 49 | + List<WeightedVariation> variations = Arrays.asList(new WeightedVariation(0, bucketValue)); |
| 50 | + VariationOrRollout vr = new VariationOrRollout(null, new VariationOrRollout.Rollout(variations, null)); |
| 51 | + |
| 52 | + Integer resultVariation = vr.variationIndexForUser(user, flagKey, salt); |
| 53 | + assertEquals(Integer.valueOf(0), resultVariation); |
| 54 | + } |
| 55 | + |
8 | 56 | @Test |
9 | 57 | public void canBucketByIntAttributeSameAsString() { |
10 | 58 | LDUser user = new LDUser.Builder("key") |
|
0 commit comments