forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve account counters handling (mastodon#15913)
* Improve account counters handling * Use ActiveRecord::Base::sanitize_sql to pass values instead of interpolating them Keep using string interpolation for `key` as it is safe and using “ActiveRecord::Base::sanitize_sql_hash_for_assignment” would require stitching bits of SQL in a way that is not more easily checked for safety. * Add migration hook to catch PostgreSQL versions earlier than 9.5
- Loading branch information
1 parent
474375c
commit abe87fb
Showing
5 changed files
with
125 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'rails_helper' | ||
|
||
describe AccountCounters do | ||
let!(:account) { Fabricate(:account) } | ||
|
||
describe '#increment_count!' do | ||
it 'increments the count' do | ||
expect(account.followers_count).to eq 0 | ||
account.increment_count!(:followers_count) | ||
expect(account.followers_count).to eq 1 | ||
end | ||
|
||
it 'increments the count in multi-threaded an environment' do | ||
increment_by = 15 | ||
wait_for_start = true | ||
|
||
threads = Array.new(increment_by) do | ||
Thread.new do | ||
true while wait_for_start | ||
account.increment_count!(:statuses_count) | ||
end | ||
end | ||
|
||
wait_for_start = false | ||
threads.each(&:join) | ||
|
||
expect(account.statuses_count).to eq increment_by | ||
end | ||
end | ||
|
||
describe '#decrement_count!' do | ||
it 'decrements the count' do | ||
account.followers_count = 15 | ||
account.save! | ||
expect(account.followers_count).to eq 15 | ||
account.decrement_count!(:followers_count) | ||
expect(account.followers_count).to eq 14 | ||
end | ||
|
||
it 'decrements the count in multi-threaded an environment' do | ||
decrement_by = 10 | ||
wait_for_start = true | ||
|
||
account.statuses_count = 15 | ||
account.save! | ||
|
||
threads = Array.new(decrement_by) do | ||
Thread.new do | ||
true while wait_for_start | ||
account.decrement_count!(:statuses_count) | ||
end | ||
end | ||
|
||
wait_for_start = false | ||
threads.each(&:join) | ||
|
||
expect(account.statuses_count).to eq 5 | ||
end | ||
end | ||
end |