-
Notifications
You must be signed in to change notification settings - Fork 32
[FSSDK-11373] add holdout support and refactor decision logic in DefaultDecisionService #587
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
Open
muzahidul-opti
wants to merge
13
commits into
master
Choose a base branch
from
muzahid/FSSDK-11373
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,978
−106
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5b3e6f0
wip: holdout bucketing logic added
muzahidul-opti fc15409
wip: add unit test for bucket to variation with 10% traffic for holdout
muzahidul-opti 793d1d5
wip: DecisionService testcases added for holdouts
muzahidul-opti 7a56d47
wip: rename api
muzahidul-opti e92a3b6
wip: add code documentation
muzahidul-opti e445f7b
wip: decide reasons test cases adde for holdout
muzahidul-opti c182d04
wip: add decide test cases for holdout
muzahidul-opti f2248c6
wip: clean up
muzahidul-opti ab11df5
wip: clean up
muzahidul-opti b3e855e
wip: clean up
muzahidul-opti 90b46c9
wip: add flag decision method
muzahidul-opti 43c9c78
wip: fix flag decision logic
muzahidul-opti 557f6ea
wip: add code doc
muzahidul-opti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -115,4 +115,3 @@ struct HoldoutConfig { | |
return holdoutIdMap[id] | ||
} | ||
} | ||
|
This file contains hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
143 changes: 143 additions & 0 deletions
143
Tests/OptimizelyTests-Common/BucketTests_HoldoutToVariation.swift
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// | ||
// Copyright 2019, 2021, Optimizely, Inc. and contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import XCTest | ||
|
||
class BucketTests_HoldoutToVariation: XCTestCase { | ||
var optimizely: OptimizelyClient! | ||
var config: ProjectConfig! | ||
var bucketer: DefaultBucketer! | ||
|
||
var kUserId = "123456" | ||
var kHoldoutId = "4444444" | ||
var kHoldoutKey = "holdout_key" | ||
|
||
var kVariationKeyA = "a" | ||
var kVariationIdA = "a11" | ||
|
||
var kAudienceIdCountry = "10" | ||
var kAudienceIdAge = "20" | ||
var kAudienceIdInvalid = "9999999" | ||
|
||
var kAttributesCountryMatch: [String: Any] = ["country": "us"] | ||
var kAttributesCountryNotMatch: [String: Any] = ["country": "ca"] | ||
var kAttributesAgeMatch: [String: Any] = ["age": 30] | ||
var kAttributesAgeNotMatch: [String: Any] = ["age": 10] | ||
var kAttributesEmpty: [String: Any] = [:] | ||
|
||
var holdout: Holdout! | ||
|
||
// MARK: - Sample datafile data | ||
|
||
var sampleHoldoutData: [String: Any] { | ||
return [ | ||
"status": "Running", | ||
"id": kHoldoutId, | ||
"key": kHoldoutKey, | ||
"layerId": "10420273888", | ||
"trafficAllocation": [ | ||
["entityId": kVariationIdA, "endOfRange": 1000] // 10% traffic allocation (0-1000 out of 10000) | ||
], | ||
"audienceIds": [kAudienceIdCountry], | ||
"variations": [ | ||
["variables": [], "id": kVariationIdA, "key": kVariationKeyA] | ||
], | ||
] | ||
} | ||
|
||
// MARK: - Setup | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
self.optimizely = OTUtils.createOptimizely(datafileName: "empty_datafile", | ||
clearUserProfileService: true) | ||
self.config = self.optimizely.config! | ||
self.bucketer = ((optimizely.decisionService as! DefaultDecisionService).bucketer as! DefaultBucketer) | ||
|
||
// Initialize holdout | ||
holdout = try! OTUtils.model(from: sampleHoldoutData) | ||
} | ||
|
||
// MARK: - Tests for bucketToVariation | ||
|
||
func testBucketToVariation_ValidBucketingWithinAllocation() { | ||
// Test users that should bucket into the single variation (within 0-1000 range) | ||
let testCases = [ | ||
["userId": "user1", "expectedVariation": kVariationKeyA], // Buckets to variation A | ||
["userId": "testuser", "expectedVariation": kVariationKeyA] // Buckets to variation A | ||
] | ||
|
||
for (index, test) in testCases.enumerated() { | ||
// Mock bucket value to ensure it falls within 0-1000 | ||
let mockBucketValue = 500 // Within 10% allocation | ||
let mockBucketer = MockBucketer(mockBucketValue: mockBucketValue) | ||
let response = mockBucketer.bucketToVariation(experiment: holdout, bucketingId: test["userId"]!) | ||
XCTAssertNotNil(response.result, "Variation should not be nil for test case \(index)") | ||
XCTAssertEqual(response.result?.key, test["expectedVariation"], "Wrong variation for test case \(index)") | ||
} | ||
} | ||
|
||
func testBucketToVariation_BucketValueOutsideAllocation() { | ||
// Test users that fall outside the 10% allocation (bucket value > 1000) | ||
let testCases = [ | ||
["userId": "user2"], | ||
["userId": "anotheruser"] | ||
] | ||
|
||
for (index, test) in testCases.enumerated() { | ||
// Mock bucket value to ensure it falls outside 0-1000 | ||
let mockBucketValue = 1500 // Outside 10% allocation | ||
let mockBucketer = MockBucketer(mockBucketValue: mockBucketValue) | ||
let response = mockBucketer.bucketToVariation(experiment: holdout, bucketingId: test["userId"]!) | ||
XCTAssertNil(response.result, "Variation should be nil for test case \(index) when outside allocation") | ||
} | ||
} | ||
|
||
func testBucketToVariation_NoTrafficAllocation() { | ||
// Create a holdout with empty traffic allocation | ||
var modifiedHoldoutData = sampleHoldoutData | ||
modifiedHoldoutData["trafficAllocation"] = [] | ||
let modifiedHoldout = try! OTUtils.model(from: modifiedHoldoutData) as Holdout | ||
|
||
let response = bucketer.bucketToVariation(experiment: modifiedHoldout, bucketingId: kUserId) | ||
|
||
XCTAssertNil(response.result, "Variation should be nil when no traffic allocation") | ||
} | ||
|
||
func testBucketToVariation_InvalidVariationId() { | ||
// Create a holdout with invalid variation ID in traffic allocation | ||
var modifiedHoldoutData = sampleHoldoutData | ||
modifiedHoldoutData["trafficAllocation"] = [ | ||
["entityId": "invalid_variation_id", "endOfRange": 1000] | ||
] | ||
let modifiedHoldout = try! OTUtils.model(from: modifiedHoldoutData) as Holdout | ||
|
||
let response = bucketer.bucketToVariation(experiment: modifiedHoldout, bucketingId: kUserId) | ||
|
||
XCTAssertNil(response.result, "Variation should be nil for invalid variation ID") | ||
} | ||
|
||
func testBucketToVariation_EmptyBucketingId() { | ||
// Test with empty bucketing ID, still within allocation | ||
let mockBucketValue = 500 | ||
let mockBucketer = MockBucketer(mockBucketValue: mockBucketValue) | ||
let response = mockBucketer.bucketToVariation(experiment: holdout, bucketingId: "") | ||
|
||
XCTAssertNotNil(response.result, "Should still bucket with empty bucketing ID") | ||
XCTAssertEqual(response.result?.key, kVariationKeyA, "Should bucket to variation A") | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Are we adding new signature to this public protocol? This may break existing custom implementations on client side.
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.
You are right that adding a new method signature could potentially break clients' custom implementations. To mitigate this, we could consider either making the method optional or removing the new signature altogether.
Which approach do you think would better align with our clients' needs and use cases?