From 33ebb628f5c29bca39d053b3f94d88960eb10d0e Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Thu, 31 Oct 2019 15:53:09 -0500 Subject: [PATCH] Tests for new throttle policy --- app/lib/throttle_policy.rb | 7 +++---- spec/lib/log_service_support_spec.rb | 8 ++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/lib/throttle_policy.rb b/app/lib/throttle_policy.rb index 97d6ef4e6..617660a8e 100644 --- a/app/lib/throttle_policy.rb +++ b/app/lib/throttle_policy.rb @@ -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 diff --git a/spec/lib/log_service_support_spec.rb b/spec/lib/log_service_support_spec.rb index b46993ac3..f109e0a6d 100644 --- a/spec/lib/log_service_support_spec.rb +++ b/spec/lib/log_service_support_spec.rb @@ -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