fix device throttle update bug
parent
ae273593d2
commit
549c97deb3
|
@ -111,18 +111,18 @@ class Device < ApplicationRecord
|
|||
Rails.cache.write(CACHE_KEY % self.id, Device.new(self.as_json))
|
||||
end
|
||||
|
||||
# Sets the `throttled_at` field, but only if it is unpopulated.
|
||||
# Performs no-op if `throttled_at` was already set.
|
||||
# Sets the `throttled_until` and `throttled_at` fields if unpopulated or
|
||||
# the throttle time period increases. Notifies user of cooldown period.
|
||||
def maybe_throttle(violation)
|
||||
end_t = violation.ends_at
|
||||
# Some log validation errors will result in until_time being `nil`.
|
||||
if (violation && throttled_until.nil?)
|
||||
et = violation.ends_at
|
||||
reload.update_attributes!(throttled_until: et,
|
||||
if (violation && (throttled_until.nil? || end_t > throttled_until))
|
||||
reload.update_attributes!(throttled_until: end_t,
|
||||
throttled_at: Time.now)
|
||||
refresh_cache
|
||||
cooldown = et.in_time_zone(self.timezone || "UTC").strftime("%I:%M%p")
|
||||
cooldown = end_t.in_time_zone(self.timezone || "UTC").strftime("%I:%M%p")
|
||||
info = [violation.explanation, cooldown]
|
||||
cooldown_notice(THROTTLE_ON % info, et, "warn")
|
||||
cooldown_notice(THROTTLE_ON % info, end_t, "warn")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -79,6 +79,18 @@ describe Device do
|
|||
expect(device.throttled_until).to eq(violation.ends_at)
|
||||
end
|
||||
|
||||
it "increases a device throttle time period" do
|
||||
expect(device).to receive(:tell).and_return(Log.new)
|
||||
previous_throttle = Time.now - 1.minute
|
||||
device.update_attributes!(throttled_until: previous_throttle)
|
||||
expect(device.throttled_until).to eq(previous_throttle)
|
||||
five_minutes = ThrottlePolicy::TimePeriod.new(5.minutes, Time.now + 1.minute)
|
||||
rule = ThrottlePolicy::Rule.new(five_minutes, 500)
|
||||
violation = ThrottlePolicy::Violation.new(rule)
|
||||
device.maybe_throttle(violation)
|
||||
expect(device.throttled_until).to eq(violation.ends_at)
|
||||
end
|
||||
|
||||
it "unthrottles a runaway device" do
|
||||
expect(device).to receive(:tell).and_return(Log.new)
|
||||
example = Time.now - 1.minute
|
||||
|
|
Loading…
Reference in New Issue