about summary refs log tree commit diff
path: root/spec/models/concerns/account_counters_spec.rb
blob: 4350496e79ef909f7ff646063d3709af178c3c44 (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
58
59
60
require 'rails_helper'

describe AccountCounters do
  let!(:account) { Fabricate(:account) }

  describe '#increment_count!' do
    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
      increment_by   = 15
      wait_for_start = true

      threads = Array.new(increment_by) do
        Thread.new do
          true while wait_for_start
          account.increment_count!(:statuses_count)
        end
      end

      wait_for_start = false
      threads.each(&:join)

      expect(account.statuses_count).to eq increment_by
    end
  end

  describe '#decrement_count!' do
    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
      decrement_by   = 10
      wait_for_start = true

      account.statuses_count = 15
      account.save!

      threads = Array.new(decrement_by) do
        Thread.new do
          true while wait_for_start
          account.decrement_count!(:statuses_count)
        end
      end

      wait_for_start = false
      threads.each(&:join)

      expect(account.statuses_count).to eq 5
    end
  end
end