Tests for new throttle policy

pull/1555/head
Rick Carlino 2019-10-31 15:53:09 -05:00
parent fa6c6196bb
commit 33ebb628f5
2 changed files with 11 additions and 4 deletions

View File

@ -3,6 +3,9 @@ class ThrottlePolicy
TTL = { min: 60,
hour: 60 * 60,
day: 60 * 60 * 24 }
ROUNDING_HELPERS = { min: :beginning_of_minute,
hour: :beginning_of_hour,
day: :beginning_of_day }
TIME_UNITS = TTL.keys
VIOLATION_TPL = "more than %{limit} / %{period}"
@ -56,10 +59,6 @@ class ThrottlePolicy
(redis.get(cache_key(id, period)) || "0").to_i
end
ROUNDING_HELPERS = { min: :beginning_of_minute,
hour: :beginning_of_hour,
day: :beginning_of_day }
def next_window(period, now = Time.now)
helper = ROUNDING_HELPERS.fetch(period)
offset = TTL.fetch(period).seconds

View File

@ -74,6 +74,14 @@ describe LogService do
end.to raise_error(Mutations::ValidationException)
end
it "throttles a device that sends too many logs" do
violation = ThrottlePolicy::Violation.new(Time.now, "whatever")
return_error = receive(:violation_for).with(any_args).and_return(violation)
expect(LogService::THROTTLE_POLICY).to(return_error)
j = normal_hash[].to_json
LogService.new.process(fake_delivery_info, j)
end
it "does not save `fun`, `debug` or `nil` logs" do
["fun", "debug", nil].map do |type|
Log.destroy_all