Skip to content

Commit 9fd594b

Browse files
jianggangjianggang
andauthored
feat: add stable rollout function for FPUser (#20)
* feat: add stable rollout function to FPUser * feat: add stable rollout function for FPUser * feat: add stable rollout function for FPUser * feat: add stable rollout function for FPUser * feat: add stable rollout function for FPUser Co-authored-by: jianggang <jianggang@didiglobal.com>
1 parent 89e4341 commit 9fd594b

File tree

7 files changed

+23
-8
lines changed

7 files changed

+23
-8
lines changed

src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public static void main(String[] args) throws IOException {
3131
final FeatureProbe fpClient = new FeatureProbe(FEATURE_PROBE_SERVER_SDK_KEY, config);
3232

3333
// Create one user.
34-
FPUser user = new FPUser("00001") // key is for percentage rollout, normally use userId as key
35-
.with("userId", "00001"); // "userId" is used in rules, should be filled in.
34+
FPUser user = new FPUser()
35+
.stableRollout("00001") // key is for percentage rollout, normally use userId as key
36+
.with("userId", "00001"); // "userId" is used in rules, should be filled in.
3637

3738
// Get toggle result for this user.
3839
final String YOUR_TOGGLE_KEY = "campaign_allow_list";

src/main/java/com/featureprobe/sdk/server/FPUser.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package com.featureprobe.sdk.server;
2-
32
import java.util.HashMap;
43
import java.util.Map;
54

@@ -13,14 +12,29 @@ public class FPUser {
1312

1413
private Map<String, String> attrs = new HashMap<>();
1514

15+
/**
16+
* Creates a new FPUser
17+
*/
18+
public FPUser() {}
19+
1620
/**
1721
* Creates a new FPUser
1822
* @param key user unique id for percentage rollout
1923
*/
24+
@Deprecated
2025
public FPUser(String key) {
2126
this.key = key;
2227
}
2328

29+
/**
30+
* Set user unique id for percentage rollout
31+
* @param key user unique id for percentage rollout
32+
*/
33+
public FPUser stableRollout(String key) {
34+
this.key = key;
35+
return this;
36+
}
37+
2438
/**
2539
* Add an attribute to the user
2640
* @param name attribute name

src/main/java/com/featureprobe/sdk/server/model/Split.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Split(List<List<List<Integer>>> distribution) {
3535
}
3636

3737
public HitResult findIndex(FPUser user, String toggleKey) {
38-
String hashKey = user.getKey();
38+
String hashKey = user.getKey() != null ? user.getKey() : String.valueOf(System.nanoTime());
3939
if (StringUtils.isNotBlank(bucketBy)) {
4040
if (user.containAttr(bucketBy)) {
4141
hashKey = user.getAttr(bucketBy);

src/test/groovy/com/featureprobe/sdk/server/AccessRecorderSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class AccessRecorderSpec extends Specification {
99

1010
def setup() {
1111
accessRecorder = new AccessRecorder()
12-
FPUser user = new FPUser("test_user")
12+
FPUser user = new FPUser().stableRollout("test_user")
1313
event = new AccessEvent(System.currentTimeMillis(), user, "test_toggle", "true", 1, 0)
1414
}
1515

src/test/groovy/com/featureprobe/sdk/server/ConditionSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ConditionSpec extends Specification {
1616
condition = new Condition()
1717
condition.setType(ConditionType.STRING)
1818
condition.setSubject("userId")
19-
user = new FPUser("test_user")
19+
user = new FPUser().stableRollout("test_user")
2020
segments = ["test_project\$test_segment": new Segment(uniqueId: "test_project\$test_segment", version: 1,
2121
rules: [new SegmentRule(conditions: [new Condition(type: ConditionType.STRING, subject: "userId",
2222
predicate: PredicateType.IS_ONE_OF, objects: ["1", "2"])])])]

src/test/groovy/com/featureprobe/sdk/server/FeatureProbeSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class FeatureProbeSpec extends Specification {
6363
def caseName = testCase.get("name").asText()
6464
println("starting execute scenario : " + name + ",case : " + caseName)
6565
def userCase = testCase.get("user")
66-
FPUser user = new FPUser(userCase.get("key").asText())
66+
FPUser user = new FPUser().stableRollout(userCase.get("key").asText())
6767
def customValues = userCase.get("customValues").asList()
6868
for (int x = 0; x < customValues.size(); x++) {
6969
def customValue = customValues.get(x)

src/test/groovy/com/featureprobe/sdk/server/SplitSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class SplitSpec extends Specification {
1010

1111
def setup() {
1212
split = new Split([[[0, 5000]], [[5000, 10000]]])
13-
user = new FPUser("test_user_key")
13+
user = new FPUser().stableRollout("test_user_key")
1414
}
1515

1616
def "Get user group"() {

0 commit comments

Comments
 (0)