Welcome to mirror list, hosted at ThFree Co, Russian Federation.

job_tracker_spec.rb « limited_capacity « concerns « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c79f347903c2284b22c38384b1ad6cae10dedac (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe LimitedCapacity::JobTracker, :clean_gitlab_redis_queues do
  let(:job_tracker) do
    described_class.new('namespace')
  end

  describe '#register' do
    it 'adds jid to the set' do
      job_tracker.register('a-job-id')

      expect(job_tracker.running_jids).to contain_exactly('a-job-id')
    end

    it 'updates the counter' do
      expect { job_tracker.register('a-job-id') }
        .to change { job_tracker.count }
        .from(0)
        .to(1)
    end

    it 'does it in only one Redis call' do
      expect(job_tracker).to receive(:with_redis).once.and_call_original

      job_tracker.register('a-job-id')
    end
  end

  describe '#remove' do
    before do
      job_tracker.register(%w[a-job-id other-job-id])
    end

    it 'removes jid from the set' do
      job_tracker.remove('other-job-id')

      expect(job_tracker.running_jids).to contain_exactly('a-job-id')
    end

    it 'updates the counter' do
      expect { job_tracker.remove('other-job-id') }
        .to change { job_tracker.count }
        .from(2)
        .to(1)
    end

    it 'does it in only one Redis call' do
      expect(job_tracker).to receive(:with_redis).once.and_call_original

      job_tracker.remove('other-job-id')
    end
  end

  describe '#clean_up' do
    before do
      job_tracker.register('a-job-id')
    end

    context 'with running jobs' do
      before do
        expect(Gitlab::SidekiqStatus).to receive(:completed_jids)
          .with(%w[a-job-id])
          .and_return([])
      end

      it 'does not remove the jid from the set' do
        expect { job_tracker.clean_up }
          .not_to change { job_tracker.running_jids.include?('a-job-id') }
      end

      it 'does only one Redis call to get the job ids' do
        expect(job_tracker).to receive(:with_redis).once.and_call_original

        job_tracker.clean_up
      end
    end

    context 'with completed jobs' do
      it 'removes the jid from the set' do
        expect { job_tracker.clean_up }
          .to change { job_tracker.running_jids.include?('a-job-id') }
      end

      it 'updates the counter' do
        expect { job_tracker.clean_up }
          .to change { job_tracker.count }
          .from(1)
          .to(0)
      end

      it 'gets the job ids, removes them, and updates the counter with only two Redis calls' do
        expect(job_tracker).to receive(:with_redis).twice.and_call_original

        job_tracker.clean_up
      end
    end
  end
end