2023-02-22 01:55:31 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-19 13:14:57 +01:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-12-01 12:00:41 +01:00
|
|
|
describe Account::Counters do
|
2021-03-19 13:14:57 +01:00
|
|
|
let!(:account) { Fabricate(:account) }
|
|
|
|
|
|
|
|
describe '#increment_count!' do
|
2024-02-07 15:53:29 +01:00
|
|
|
let(:increment_by) { 15 }
|
|
|
|
|
2021-03-19 13:14:57 +01:00
|
|
|
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
|
2024-02-07 15:53:29 +01:00
|
|
|
multi_threaded_execution(increment_by) do
|
|
|
|
account.increment_count!(:statuses_count)
|
2021-03-19 13:14:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(account.statuses_count).to eq increment_by
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#decrement_count!' do
|
2024-02-07 15:53:29 +01:00
|
|
|
let(:decrement_by) { 10 }
|
|
|
|
|
2021-03-19 13:14:57 +01:00
|
|
|
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
|
|
|
|
account.statuses_count = 15
|
|
|
|
account.save!
|
|
|
|
|
2024-02-07 15:53:29 +01:00
|
|
|
multi_threaded_execution(decrement_by) do
|
|
|
|
account.decrement_count!(:statuses_count)
|
2021-03-19 13:14:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(account.statuses_count).to eq 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|