Test case: LogService throttle

pull/1529/head
Rick Carlino 2019-10-24 08:18:56 -05:00
parent 93d0428e7f
commit 39d461296e
2 changed files with 17 additions and 3 deletions

View File

@ -19,9 +19,9 @@ class ThrottlePolicy
def is_throttled(unique_id)
rules
.map do |rule|
is_violation = rule.time_period.usage_count_for(unique_id) > rule.limit
is_violation ? Violation.new(rule) : nil
end
is_violation = rule.time_period.usage_count_for(unique_id) > rule.limit
is_violation ? Violation.new(rule) : nil
end
.compact
.max
end

View File

@ -54,6 +54,20 @@ describe LogService do
LogService.new.warn_user(data, time)
end
it "triggers a throttle" do
tp = LogService::THROTTLE_POLICY
ls = LogService.new
data = AmqpLogParser::DeliveryInfo.new
data.device_id = FactoryBot.create(:device).id
violation = ThrottlePolicy::Violation.new(Object.new)
allow(ls).to receive(:deliver)
expect(ls).to receive(:warn_user)
expect(tp).to receive(:is_throttled)
.with(data.device_id)
.and_return(violation)
ls.maybe_deliver(data)
end
it "handles bad params" do
expect do
LogService.new.process(fake_delivery_info, {})