Skip to content

Commit

Permalink
fix: patch random stickiness calculations for rollout strategies (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre authored Apr 4, 2023
1 parent fe8be13 commit 4baecad
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
32 changes: 32 additions & 0 deletions internal/strategies/flexible_rollout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package strategies

import (
"testing"

"github.com/Unleash/unleash-client-go/v3/context"
"github.com/Unleash/unleash-client-go/v3/strategy"
"github.com/stretchr/testify/assert"
)

func TestFlexibleRolloutStrategy_IsWellDistributed(t *testing.T) {
s := NewFlexibleRolloutStrategy()

enabledCount := 0
rounds := 200000

for i := 0; i < rounds; i++ {
params := map[string]interface{}{
strategy.ParamStickiness: "random",
strategy.ParamRollout: 50,
strategy.ParamGroupId: "test51",
}
enabled := s.IsEnabled(params, &context.Context{})
if enabled {
enabledCount++
}
}

actualPercentage := round(100.0 * float64(enabledCount) / float64(rounds))

assert.InDelta(t, 50, actualPercentage, 1.0)
}
5 changes: 4 additions & 1 deletion internal/strategies/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func (r *rng) float() float64 {
}

func (r *rng) string() string {
return strconv.Itoa(r.int())
r.Lock()
n := r.random.Intn(10000) + 1
r.Unlock()
return strconv.Itoa(n)
}

// newRng creates a new random number generator for numbers between 1-100
Expand Down
2 changes: 1 addition & 1 deletion internal/strategies/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestNewRng(t *testing.T) {
assert.True(t, randomInt >= 0 && randomInt <= 100)

randomString := rng.string()
assert.True(t, len(randomString) <= 3)
assert.True(t, len(randomString) <= 5)

randomFloat := rng.float()
assert.True(t, randomFloat > 0.0 && randomFloat <= 100.0)
Expand Down

0 comments on commit 4baecad

Please sign in to comment.