about summary refs log tree commit diff
path: root/spec/models/account_stat_spec.rb
blob: 8adc0d1d638d8bf46dbb025754d4759753f3dc7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require 'rails_helper'

RSpec.describe AccountStat, type: :model do
  describe '#increment_count!' do
    it 'increments the count' do
      account_stat = AccountStat.create(account: Fabricate(:account))
      expect(account_stat.followers_count).to eq 0
      account_stat.increment_count!(:followers_count)
      expect(account_stat.followers_count).to eq 1
    end

    it 'increments the count in multi-threaded an environment' do
      account_stat   = AccountStat.create(account: Fabricate(:account), statuses_count: 0)
      increment_by   = 15
      wait_for_start = true

      threads = Array.new(increment_by) do
        Thread.new do
          true while wait_for_start
          AccountStat.find(account_stat.id).increment_count!(:statuses_count)
        end
      end

      wait_for_start = false
      threads.each(&:join)

      expect(account_stat.reload.statuses_count).to eq increment_by
    end
  end

  describe '#decrement_count!' do
    it 'decrements the count' do
      account_stat = AccountStat.create(account: Fabricate(:account), followers_count: 15)
      expect(account_stat.followers_count).to eq 15
      account_stat.decrement_count!(:followers_count)
      expect(account_stat.followers_count).to eq 14
    end

    it 'decrements the count in multi-threaded an environment' do
      account_stat   = AccountStat.create(account: Fabricate(:account), statuses_count: 15)
      decrement_by   = 10
      wait_for_start = true

      threads = Array.new(decrement_by) do
        Thread.new do
          true while wait_for_start
          AccountStat.find(account_stat.id).decrement_count!(:statuses_count)
        end
      end

      wait_for_start = false
      threads.each(&:join)

      expect(account_stat.reload.statuses_count).to eq 5
    end
  end
end