account_counters_spec.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe AccountCounters do
  4. let!(:account) { Fabricate(:account) }
  5. describe '#increment_count!' do
  6. it 'increments the count' do
  7. expect(account.followers_count).to eq 0
  8. account.increment_count!(:followers_count)
  9. expect(account.followers_count).to eq 1
  10. end
  11. it 'increments the count in multi-threaded an environment' do
  12. increment_by = 15
  13. wait_for_start = true
  14. threads = Array.new(increment_by) do
  15. Thread.new do
  16. true while wait_for_start
  17. account.increment_count!(:statuses_count)
  18. end
  19. end
  20. wait_for_start = false
  21. threads.each(&:join)
  22. expect(account.statuses_count).to eq increment_by
  23. end
  24. end
  25. describe '#decrement_count!' do
  26. it 'decrements the count' do
  27. account.followers_count = 15
  28. account.save!
  29. expect(account.followers_count).to eq 15
  30. account.decrement_count!(:followers_count)
  31. expect(account.followers_count).to eq 14
  32. end
  33. it 'decrements the count in multi-threaded an environment' do
  34. decrement_by = 10
  35. wait_for_start = true
  36. account.statuses_count = 15
  37. account.save!
  38. threads = Array.new(decrement_by) do
  39. Thread.new do
  40. true while wait_for_start
  41. account.decrement_count!(:statuses_count)
  42. end
  43. end
  44. wait_for_start = false
  45. threads.each(&:join)
  46. expect(account.statuses_count).to eq 5
  47. end
  48. end
  49. end