diff --git a/src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java b/src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java index 1e43a2c..61ceeda 100644 --- a/src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java +++ b/src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java @@ -32,8 +32,9 @@ public static void main(String[] args) throws MalformedURLException, Interrupted final FeatureProbe fpClient = new FeatureProbe(FEATURE_PROBE_SERVER_SDK_KEY, config); // Create one user. - FPUser user = new FPUser("00001") // key is for percentage rollout, normally use userId as key - .with("userId", "00001"); // "userId" is used in rules, should be filled in. + FPUser user = new FPUser() + .stableRollout("00001") // key is for percentage rollout, normally use userId as key + .with("userId", "00001"); // "userId" is used in rules, should be filled in. // Get toggle result for this user. final String YOUR_TOGGLE_KEY = "campaign_allow_list"; diff --git a/src/main/java/com/featureprobe/sdk/server/FPUser.java b/src/main/java/com/featureprobe/sdk/server/FPUser.java index 18c77a2..8990c48 100644 --- a/src/main/java/com/featureprobe/sdk/server/FPUser.java +++ b/src/main/java/com/featureprobe/sdk/server/FPUser.java @@ -1,5 +1,4 @@ package com.featureprobe.sdk.server; - import java.util.HashMap; import java.util.Map; @@ -13,14 +12,29 @@ public class FPUser { private Map attrs = new HashMap<>(); + /** + * Creates a new FPUser + */ + public FPUser() {} + /** * Creates a new FPUser * @param key user unique id for percentage rollout */ + @Deprecated public FPUser(String key) { this.key = key; } + /** + * Set user unique id for percentage rollout + * @param key user unique id for percentage rollout + */ + public FPUser stableRollout(String key) { + this.key = key; + return this; + } + /** * Add an attribute to the user * @param name attribute name diff --git a/src/main/java/com/featureprobe/sdk/server/model/Split.java b/src/main/java/com/featureprobe/sdk/server/model/Split.java index ec22028..163fbcb 100644 --- a/src/main/java/com/featureprobe/sdk/server/model/Split.java +++ b/src/main/java/com/featureprobe/sdk/server/model/Split.java @@ -35,7 +35,7 @@ public Split(List>> distribution) { } public HitResult findIndex(FPUser user, String toggleKey) { - String hashKey = user.getKey(); + String hashKey = user.getKey() != null ? user.getKey() : String.valueOf(System.nanoTime()); if (StringUtils.isNotBlank(bucketBy)) { if (user.containAttr(bucketBy)) { hashKey = user.getAttr(bucketBy); diff --git a/src/test/groovy/com/featureprobe/sdk/server/AccessRecorderSpec.groovy b/src/test/groovy/com/featureprobe/sdk/server/AccessRecorderSpec.groovy index 253666d..e99c235 100644 --- a/src/test/groovy/com/featureprobe/sdk/server/AccessRecorderSpec.groovy +++ b/src/test/groovy/com/featureprobe/sdk/server/AccessRecorderSpec.groovy @@ -9,7 +9,7 @@ class AccessRecorderSpec extends Specification { def setup() { accessRecorder = new AccessRecorder() - FPUser user = new FPUser("test_user") + FPUser user = new FPUser().stableRollout("test_user") event = new AccessEvent(System.currentTimeMillis(), user, "test_toggle", "true", 1, 0) } diff --git a/src/test/groovy/com/featureprobe/sdk/server/ConditionSpec.groovy b/src/test/groovy/com/featureprobe/sdk/server/ConditionSpec.groovy index 8bb38f8..bbedcb5 100644 --- a/src/test/groovy/com/featureprobe/sdk/server/ConditionSpec.groovy +++ b/src/test/groovy/com/featureprobe/sdk/server/ConditionSpec.groovy @@ -16,7 +16,7 @@ class ConditionSpec extends Specification { condition = new Condition() condition.setType(ConditionType.STRING) condition.setSubject("userId") - user = new FPUser("test_user") + user = new FPUser().stableRollout("test_user") segments = ["test_project\$test_segment": new Segment(uniqueId: "test_project\$test_segment", version: 1, rules: [new SegmentRule(conditions: [new Condition(type: ConditionType.STRING, subject: "userId", predicate: PredicateType.IS_ONE_OF, objects: ["1", "2"])])])] diff --git a/src/test/groovy/com/featureprobe/sdk/server/FeatureProbeSpec.groovy b/src/test/groovy/com/featureprobe/sdk/server/FeatureProbeSpec.groovy index 649d948..65ec0f9 100644 --- a/src/test/groovy/com/featureprobe/sdk/server/FeatureProbeSpec.groovy +++ b/src/test/groovy/com/featureprobe/sdk/server/FeatureProbeSpec.groovy @@ -63,7 +63,7 @@ class FeatureProbeSpec extends Specification { def caseName = testCase.get("name").asText() println("starting execute scenario : " + name + ",case : " + caseName) def userCase = testCase.get("user") - FPUser user = new FPUser(userCase.get("key").asText()) + FPUser user = new FPUser().stableRollout(userCase.get("key").asText()) def customValues = userCase.get("customValues").asList() for (int x = 0; x < customValues.size(); x++) { def customValue = customValues.get(x) diff --git a/src/test/groovy/com/featureprobe/sdk/server/SplitSpec.groovy b/src/test/groovy/com/featureprobe/sdk/server/SplitSpec.groovy index 20f4878..9b49e4b 100644 --- a/src/test/groovy/com/featureprobe/sdk/server/SplitSpec.groovy +++ b/src/test/groovy/com/featureprobe/sdk/server/SplitSpec.groovy @@ -10,7 +10,7 @@ class SplitSpec extends Specification { def setup() { split = new Split([[[0, 5000]], [[5000, 10000]]]) - user = new FPUser("test_user_key") + user = new FPUser().stableRollout("test_user_key") } def "Get user group"() {