闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.8 KiB

  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