Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Infinity exceptions in hot score calculator #3678

Merged
merged 2 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix Infinity exceptions in hot score calculator
There was a rare case we've found on some Travis builds where the system
time suddenly moved back to the past a couple of tenths of a second,
meaning `Time.current - resource.created_at` returned a negative number,
which lead to a division by zero.
  • Loading branch information
javierm committed Sep 10, 2019
commit 53abc57578737b5f1ae444d0ba1e250cd82393e9
2 changes: 1 addition & 1 deletion lib/score_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module ScoreCalculator
def self.hot_score(resource)
return 0 unless resource.created_at

period = [max_period, resource_age(resource)].min
period = [1, [max_period, resource_age(resource)].min].max

votes_total = resource.votes_for.where("created_at >= ?", period.days.ago).count
votes_up = resource.get_upvotes.where("created_at >= ?", period.days.ago).count
Expand Down
29 changes: 29 additions & 0 deletions spec/lib/score_calculator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "rails_helper"

describe ScoreCalculator do
describe ".hot_score" do
let(:resource) { create(:debate) }

before do
resource.vote_by(voter: create(:user), vote: "yes")
end

it "ignores small time leaps", :with_frozen_time do
resource.created_at = Time.current + 0.01

expect(ScoreCalculator.hot_score(resource)).to eq 1
end

it "ignores setting with negative value " do
Setting["hot_score_period_in_days"] = -1

expect(ScoreCalculator.hot_score(resource)).to eq 1
end

it "ignores setting with zero value" do
Setting["hot_score_period_in_days"] = 0

expect(ScoreCalculator.hot_score(resource)).to eq 1
end
end
end