summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/phabricator_import/worker_state_spec.rb
blob: b6f2524a9d06865327e8d5047861292ff04bbbd8 (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::PhabricatorImport::WorkerState, :clean_gitlab_redis_shared_state do
  subject(:state) { described_class.new('weird-project-id') }
  let(:key) { 'phabricator-import/jobs/project-weird-project-id/job-count' }

  describe '#add_job' do
    it 'increments the counter for jobs' do
      set_value(3)

      expect { state.add_job }.to change { get_value }.from('3').to('4')
    end
  end

  describe '#remove_job' do
    it 'decrements the counter for jobs' do
      set_value(3)

      expect { state.remove_job }.to change { get_value }.from('3').to('2')
    end
  end

  describe '#running_count' do
    it 'reads the value' do
      set_value(9)

      expect(state.running_count).to eq(9)
    end

    it 'returns 0 when nothing was set' do
      expect(state.running_count).to eq(0)
    end
  end

  def set_value(value)
    redis.with { |r| r.set(key, value) }
  end

  def get_value
    redis.with { |r| r.get(key) }
  end

  def redis
    Gitlab::Redis::SharedState
  end
end