spec/pause/action_spec.rb in pause-0.0.3 vs spec/pause/action_spec.rb in pause-0.0.4
- old
+ new
@@ -4,12 +4,12 @@
describe Pause::Action do
include Pause::Helper::Timing
class MyNotification < Pause::Action
scope "ipn:follow"
- check 20, 5, 40
- check 40, 7, 40
+ check period_seconds: 20, max_allowed: 5, block_ttl: 40
+ check period_seconds: 40, max_allowed: 7, block_ttl: 40
end
let(:resolution) { 10 }
let(:history) { 60 }
let(:configuration) { Pause::Configuration.new }
@@ -45,79 +45,51 @@
action.ok?.should be_false
end
end
it "should successfully consider different period checks" do
- time = period_marker(resolution, Time.now.to_i + 1)
+ time = period_marker(resolution, Time.now.to_i)
- Timecop.freeze Time.at(time - 35) do
- 4.times do
- action.increment!
- action.ok?.should be_true
- end
- end
+ action.increment! 4, time - 25
+ action.ok?.should be_true
- Timecop.freeze Time.at(time - 5) do
- 2.times do
- action.increment!
- action.ok?.should be_true
- end
- action.increment!
- action.ok?.should be_false
- end
- end
+ action.increment! 2, time - 3
+ action.ok?.should be_true
- context "action is disabled" do
+ action.increment! 1, time
- it "should be true if action is disabled, even if blocked" do
- 10.times { action.increment! }
- action.ok?.should be_false
+ action.ok?.should be_false
- MyNotification.disable
-
- action.ok?.should be_true
- end
end
end
describe "#analyze" do
- context "action should not be blocked" do
+ context "action should not be rate limited" do
it "returns nil" do
action.analyze.should be_nil
end
end
- context "action should be blocked" do
- it "returns a BlockedAction object" do
+ context "action should be rate limited" do
+ it "returns a RateLimitedEvent object" do
time = Time.now
- blocked_action = nil
+ rate_limit = nil
Timecop.freeze time do
7.times { action.increment! }
- blocked_action = action.analyze
+ rate_limit = action.analyze
end
- expected_blocked_action = Pause::BlockedAction.new(action, action.checks[0], 7, time.to_i)
+ expected_rate_limit = Pause::RateLimitedEvent.new(action, action.checks[0], 7, time.to_i)
- blocked_action.should be_a(Pause::BlockedAction)
- blocked_action.identifier.should == expected_blocked_action.identifier
- blocked_action.sum.should == expected_blocked_action.sum
- blocked_action.period_check.should == expected_blocked_action.period_check
- blocked_action.timestamp.should == expected_blocked_action.timestamp
+ rate_limit.should be_a(Pause::RateLimitedEvent)
+ rate_limit.identifier.should == expected_rate_limit.identifier
+ rate_limit.sum.should == expected_rate_limit.sum
+ rate_limit.period_check.should == expected_rate_limit.period_check
+ rate_limit.timestamp.should == expected_rate_limit.timestamp
end
end
-
- context "action is disabled" do
- it "return nil, even if blocked" do
- 10.times { action.increment! }
- action.should_not be_ok
-
- MyNotification.disable
-
- action.analyze.should be_nil
- end
- end
end
describe "#tracked_identifiers" do
it "should return all the identifiers tracked (but not blocked) so far" do
action.increment!
@@ -129,20 +101,20 @@
MyNotification.tracked_identifiers.should include(action.identifier)
MyNotification.tracked_identifiers.should include(other_action.identifier)
end
end
- describe "#blocked_identifiers" do
+ describe "#rate_limited_identifiers" do
it "should return all the identifiers blocked" do
- action.increment!(Time.now.to_i, 100)
- other_action.increment!(Time.now.to_i, 100)
+ action.increment!(100, Time.now.to_i)
+ other_action.increment!(100, Time.now.to_i)
action.ok?
other_action.ok?
- MyNotification.blocked_identifiers.should include(action.identifier)
- MyNotification.blocked_identifiers.should include(other_action.identifier)
+ MyNotification.rate_limited_identifiers.should include(action.identifier)
+ MyNotification.rate_limited_identifiers.should include(other_action.identifier)
end
end
describe "#unblock_all" do
it "should unblock all the identifiers for a scope" do
@@ -151,15 +123,15 @@
action.ok?
other_action.ok?
MyNotification.tracked_identifiers.should include(action.identifier, other_action.identifier)
- MyNotification.blocked_identifiers.should == [action.identifier]
+ MyNotification.rate_limited_identifiers.should == [action.identifier]
MyNotification.unblock_all
- MyNotification.blocked_identifiers.should be_empty
+ MyNotification.rate_limited_identifiers.should be_empty
MyNotification.tracked_identifiers.should == [other_action.identifier]
end
end
end
@@ -172,20 +144,30 @@
check 100, 150, 200
check 200, 150, 200
check 300, 150, 200
end
+ class ActionWithHashChecks < Pause::Action
+ check period_seconds: 50, block_ttl: 60, max_allowed: 100
+ end
+
it "should define a period check on new instances" do
ActionWithCheck.new("id").checks.should == [
- Pause::PeriodCheck.new(100, 150, 200),
+ Pause::PeriodCheck.new(100, 150, 200)
]
end
it "should define a period check on new instances" do
ActionWithMultipleChecks.new("id").checks.should == [
Pause::PeriodCheck.new(100, 150, 200),
Pause::PeriodCheck.new(200, 150, 200),
Pause::PeriodCheck.new(300, 150, 200)
+ ]
+ end
+
+ it "should accept hash arguments" do
+ ActionWithHashChecks.new("id").checks.should == [
+ Pause::PeriodCheck.new(50, 100, 60)
]
end
end