Skip to content
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

fix: increase the possible random numbers used for stickiness id #241

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Unleash/Strategies/FlexibleRolloutStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class FlexibleRolloutStrategy : IStrategy
public FlexibleRolloutStrategy()
{
var random = new Random();
randomGenerator = () => (random.Next() * 100).ToString();
randomGenerator = () => random.Next(1, 10001).ToString();
}

public FlexibleRolloutStrategy(Func<string> randomGenerator)
Expand Down
38 changes: 38 additions & 0 deletions tests/Unleash.Tests/Strategy/FlexibleRolloutStrategyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,43 @@ public void Should_be_enabled_for_empty_userid()
// Assert
enabled.Should().BeTrue();
}

[Test]
public void Should_only_at_most_miss_by_one_percent()
{
// Arrange
var strategy = new FlexibleRolloutStrategy();
var percentage = 25;
var groupId = "groupId";
var rounds = 200_000;
var enabledCount = 0;

// Act
for (int i = 0; i < rounds; i++)
{
var parameters = new Dictionary<string, string>
{
{ "rollout", percentage.ToString() },
{ "groupId", groupId }
};
var context = new UnleashContext
{
SessionId = i.ToString()
};

if (strategy.IsEnabled(parameters, context))
{
enabledCount++;
}
}

var actualPercentage = (int)Math.Round((enabledCount / (double)rounds) * 100);
var highMark = percentage + 1;
var lowMark = percentage - 1;

// Assert
actualPercentage.Should().BeGreaterOrEqualTo(lowMark);
actualPercentage.Should().BeLessOrEqualTo(highMark);
}
}
}
Loading