From 9cbab2847297b7b8013e7f7541fca69281ca15b5 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Thu, 19 Sep 2024 10:34:42 +0200 Subject: [PATCH 1/6] fix: update ruby SDK to increase the possible random numbers used for stickiness id Same fix as done in some other sdks, such as the node one at https://github.com/Unleash/unleash-client-node/pull/417o Fixes an issue where 1% rollout would not yield any results with random rollout for certain group ids --- lib/unleash/strategy/flexible_rollout.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/unleash/strategy/flexible_rollout.rb b/lib/unleash/strategy/flexible_rollout.rb index e464d406..6e1172d2 100644 --- a/lib/unleash/strategy/flexible_rollout.rb +++ b/lib/unleash/strategy/flexible_rollout.rb @@ -40,7 +40,7 @@ def context_invalid?(stickiness, context) end def random - Random.rand(0..100) + Random.rand(0..10000) end def resolve_stickiness(stickiness, context) From fa1b1f2e441df44641fb0c0a401381fa55e0ae36 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Thu, 19 Sep 2024 10:36:37 +0200 Subject: [PATCH 2/6] fix: formatting --- lib/unleash/strategy/flexible_rollout.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/unleash/strategy/flexible_rollout.rb b/lib/unleash/strategy/flexible_rollout.rb index 6e1172d2..254615f0 100644 --- a/lib/unleash/strategy/flexible_rollout.rb +++ b/lib/unleash/strategy/flexible_rollout.rb @@ -40,7 +40,7 @@ def context_invalid?(stickiness, context) end def random - Random.rand(0..10000) + Random.rand(0..10_000) end def resolve_stickiness(stickiness, context) From 69ac5c3b8b5122dfdd50ffed3bd98b6fe4087107 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Thu, 19 Sep 2024 15:00:32 +0200 Subject: [PATCH 3/6] Test: stub out test from node --- .../unleash/strategy/flexible_rollout_spec.rb | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/unleash/strategy/flexible_rollout_spec.rb b/spec/unleash/strategy/flexible_rollout_spec.rb index 1e569663..3860693b 100644 --- a/spec/unleash/strategy/flexible_rollout_spec.rb +++ b/spec/unleash/strategy/flexible_rollout_spec.rb @@ -99,5 +99,33 @@ expect(strategy.is_enabled?(params, custom_context)).to be_falsey expect(strategy.is_enabled?(params, nil)).to be_falsey end + + it 'should deviate at most one percentage point from the rollout percentage' do + percentage = 25 + params = { + 'groupId' => 'groupId', + 'rollout' => percentage, + 'stickiness' => 'default' + } + + rounds = 200_000 + enabled_count = 0 + + rounds.times do |i| + params = { percentage: percentage, group_id: group_id } + context = { session_id: i } + + if strategy.is_enabled?(params, context) + enabled_count += 1 + end + end + + actual_percentage = ((enabled_count.to_f / rounds) * 100).round + high_mark = percentage + 1 + low_mark = percentage - 1 + + assert low_mark <= actual_percentage + assert high_mark >= actual_percentage + end end end From 95245680d308061f2343417119ccb5d915614180 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Fri, 20 Sep 2024 10:11:39 +0200 Subject: [PATCH 4/6] fix: update test --- spec/unleash/strategy/flexible_rollout_spec.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spec/unleash/strategy/flexible_rollout_spec.rb b/spec/unleash/strategy/flexible_rollout_spec.rb index 3860693b..cb85323e 100644 --- a/spec/unleash/strategy/flexible_rollout_spec.rb +++ b/spec/unleash/strategy/flexible_rollout_spec.rb @@ -102,8 +102,9 @@ it 'should deviate at most one percentage point from the rollout percentage' do percentage = 25 + group_id = 'groupId' params = { - 'groupId' => 'groupId', + 'groupId' => group_id, 'rollout' => percentage, 'stickiness' => 'default' } @@ -124,8 +125,8 @@ high_mark = percentage + 1 low_mark = percentage - 1 - assert low_mark <= actual_percentage - assert high_mark >= actual_percentage + expect(low_mark <= actual_percentage).to be_truthy + expect(high_mark >= actual_percentage).to be_truthy end end end From d6eb6c75e56924bf19e66cc06565379c96fd3893 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Fri, 20 Sep 2024 10:38:19 +0200 Subject: [PATCH 5/6] fix: fix test --- spec/unleash/strategy/flexible_rollout_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/unleash/strategy/flexible_rollout_spec.rb b/spec/unleash/strategy/flexible_rollout_spec.rb index cb85323e..8bda97ff 100644 --- a/spec/unleash/strategy/flexible_rollout_spec.rb +++ b/spec/unleash/strategy/flexible_rollout_spec.rb @@ -102,9 +102,8 @@ it 'should deviate at most one percentage point from the rollout percentage' do percentage = 25 - group_id = 'groupId' params = { - 'groupId' => group_id, + 'groupId' => 'groupId', 'rollout' => percentage, 'stickiness' => 'default' } @@ -113,7 +112,6 @@ enabled_count = 0 rounds.times do |i| - params = { percentage: percentage, group_id: group_id } context = { session_id: i } if strategy.is_enabled?(params, context) From 47b61f4fd1a5908fac7120d8709084c9be9fda36 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Fri, 20 Sep 2024 10:40:54 +0200 Subject: [PATCH 6/6] fix: formatting --- spec/unleash/strategy/flexible_rollout_spec.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/spec/unleash/strategy/flexible_rollout_spec.rb b/spec/unleash/strategy/flexible_rollout_spec.rb index 8bda97ff..94223cc7 100644 --- a/spec/unleash/strategy/flexible_rollout_spec.rb +++ b/spec/unleash/strategy/flexible_rollout_spec.rb @@ -113,10 +113,7 @@ rounds.times do |i| context = { session_id: i } - - if strategy.is_enabled?(params, context) - enabled_count += 1 - end + enabled_count += 1 if strategy.is_enabled?(params, context) end actual_percentage = ((enabled_count.to_f / rounds) * 100).round