Skip to content

Instantly share code, notes, and snippets.

@eric
Created July 5, 2011 18:49
Show Gist options
  • Save eric/1065553 to your computer and use it in GitHub Desktop.
Save eric/1065553 to your computer and use it in GitHub Desktop.
diff --git a/lib/rollout.rb b/lib/rollout.rb
index 4727b97..fe469f8 100644
--- a/lib/rollout.rb
+++ b/lib/rollout.rb
@@ -1,7 +1,10 @@
class Rollout
- def initialize(redis)
- @redis = redis
- @groups = {"all" => lambda { |user| true }}
+ attr_accessor :population
+
+ def initialize(redis, population = 1.0)
+ @redis = redis
+ @groups = {"all" => lambda { |user| true }}
+ @population = population
end
def activate_group(feature, group)
@@ -72,6 +75,6 @@ class Rollout
def user_within_active_percentage?(feature, user)
percentage = @redis.get(percentage_key(feature))
return false if percentage.nil?
- user.id % 100 < percentage.to_i
+ user.id * @population % 100 < percentage.to_i
end
end
diff --git a/spec/rollout_spec.rb b/spec/rollout_spec.rb
index 0941965..a5a3ccd 100644
--- a/spec/rollout_spec.rb
+++ b/spec/rollout_spec.rb
@@ -116,7 +116,7 @@ describe "Rollout" do
end
end
- describe "activating a feature for a percentage of users" do
+ describe "activating a feature for a bigger percentage of users" do
before do
@rollout.activate_percentage(:chat, 20)
end
@@ -126,6 +126,24 @@ describe "Rollout" do
end
end
+ describe "activating a feature for a percentage of users with holes in the id space" do
+ before do
+ @standard_poulation, @rollout.population = @rollout.population, 0.1
+ @rollout.activate_percentage(:chat, 3)
+ end
+
+ after do
+ @rollout.population = @standard_population
+ end
+
+ it "activates the feature for that percentage of the users" do
+ id = 3
+ step = 10
+
+ 100.times.select { @rollout.active?(:chat, stub(:id => id += step)) }.length.should == 3
+ end
+ end
+
describe "activating a feature for a percentage of users" do
before do
@rollout.activate_percentage(:chat, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment