summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/user_activities_spec.rb
blob: 187d88c8c588d36b325a565f365c8c1967607707 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require 'spec_helper'

describe Gitlab::UserActivities, :redis, lib: true do
  let(:now) { Time.now }

  describe '.record' do
    context 'with no time given' do
      it 'uses Time.now and records an activity in Redis' do
        Timecop.freeze do
          now # eager-load now
          described_class.record(42)
        end

        Gitlab::Redis.with do |redis|
          expect(redis.hscan(described_class::KEY, 0)).to eq(['0', [['42', now.to_i.to_s]]])
        end
      end
    end

    context 'with a time given' do
      it 'uses the given time and records an activity in Redis' do
        described_class.record(42, now)

        Gitlab::Redis.with do |redis|
          expect(redis.hscan(described_class::KEY, 0)).to eq(['0', [['42', now.to_i.to_s]]])
        end
      end
    end
  end

  describe '.delete' do
    context 'with a single key' do
      context 'and key exists' do
        it 'removes the pair from Redis' do
          described_class.record(42, now)

          Gitlab::Redis.with do |redis|
            expect(redis.hscan(described_class::KEY, 0)).to eq(['0', [['42', now.to_i.to_s]]])
          end

          subject.delete(42)

          Gitlab::Redis.with do |redis|
            expect(redis.hscan(described_class::KEY, 0)).to eq(['0', []])
          end
        end
      end

      context 'and key does not exist' do
        it 'removes the pair from Redis' do
          Gitlab::Redis.with do |redis|
            expect(redis.hscan(described_class::KEY, 0)).to eq(['0', []])
          end

          subject.delete(42)

          Gitlab::Redis.with do |redis|
            expect(redis.hscan(described_class::KEY, 0)).to eq(['0', []])
          end
        end
      end
    end

    context 'with multiple keys' do
      context 'and all keys exist' do
        it 'removes the pair from Redis' do
          described_class.record(41, now)
          described_class.record(42, now)

          Gitlab::Redis.with do |redis|
            expect(redis.hscan(described_class::KEY, 0)).to eq(['0', [['41', now.to_i.to_s], ['42', now.to_i.to_s]]])
          end

          subject.delete(41, 42)

          Gitlab::Redis.with do |redis|
            expect(redis.hscan(described_class::KEY, 0)).to eq(['0', []])
          end
        end
      end

      context 'and some keys does not exist' do
        it 'removes the existing pair from Redis' do
          described_class.record(42, now)

          Gitlab::Redis.with do |redis|
            expect(redis.hscan(described_class::KEY, 0)).to eq(['0', [['42', now.to_i.to_s]]])
          end

          subject.delete(41, 42)

          Gitlab::Redis.with do |redis|
            expect(redis.hscan(described_class::KEY, 0)).to eq(['0', []])
          end
        end
      end
    end
  end

  describe 'Enumerable' do
    before do
      described_class.record(40, now)
      described_class.record(41, now)
      described_class.record(42, now)
    end

    it 'allows to read the activities sequentially' do
      expected = { '40' => now.to_i.to_s, '41' => now.to_i.to_s, '42' => now.to_i.to_s }

      actual = described_class.new.each_with_object({}) do |(key, time), actual|
        actual[key] = time
      end

      expect(actual).to eq(expected)
    end

    context 'with many records' do
      before do
        1_000.times { |i| described_class.record(i, now) }
      end

      it 'is possible to loop through all the records' do
        expect(described_class.new.count).to eq(1_000)
      end
    end
  end
end