account_stat_spec.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. require 'rails_helper'
  2. RSpec.describe AccountStat, type: :model do
  3. describe '#increment_count!' do
  4. it 'increments the count' do
  5. account_stat = AccountStat.create(account: Fabricate(:account))
  6. expect(account_stat.followers_count).to eq 0
  7. account_stat.increment_count!(:followers_count)
  8. expect(account_stat.followers_count).to eq 1
  9. end
  10. it 'increments the count in multi-threaded an environment' do
  11. account_stat = AccountStat.create(account: Fabricate(:account), statuses_count: 0)
  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. AccountStat.find(account_stat.id).increment_count!(:statuses_count)
  18. end
  19. end
  20. wait_for_start = false
  21. threads.each(&:join)
  22. expect(account_stat.reload.statuses_count).to eq increment_by
  23. end
  24. end
  25. describe '#decrement_count!' do
  26. it 'decrements the count' do
  27. account_stat = AccountStat.create(account: Fabricate(:account), followers_count: 15)
  28. expect(account_stat.followers_count).to eq 15
  29. account_stat.decrement_count!(:followers_count)
  30. expect(account_stat.followers_count).to eq 14
  31. end
  32. it 'decrements the count in multi-threaded an environment' do
  33. account_stat = AccountStat.create(account: Fabricate(:account), statuses_count: 15)
  34. decrement_by = 10
  35. wait_for_start = true
  36. threads = Array.new(decrement_by) do
  37. Thread.new do
  38. true while wait_for_start
  39. AccountStat.find(account_stat.id).decrement_count!(:statuses_count)
  40. end
  41. end
  42. wait_for_start = false
  43. threads.each(&:join)
  44. expect(account_stat.reload.statuses_count).to eq 5
  45. end
  46. end
  47. end