BigW Consortium Gitlab

authorized_projects_worker_spec.rb 3.09 KB
Newer Older
1 2 3
require 'spec_helper'

describe AuthorizedProjectsWorker do
4
  let(:project) { create(:project) }
5

6 7 8 9 10
  def build_args_list(*ids, multiply: 1)
    args_list = ids.map { |id| [id] }
    args_list * multiply
  end

11 12
  describe '.bulk_perform_and_wait' do
    it 'schedules the ids and waits for the jobs to complete' do
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
      args_list = build_args_list(project.owner.id)

      project.owner.project_authorizations.delete_all
      described_class.bulk_perform_and_wait(args_list)

      expect(project.owner.project_authorizations.count).to eq(1)
    end

    it 'inlines workloads <= 3 jobs' do
      args_list = build_args_list(project.owner.id, multiply: 3)
      expect(described_class).to receive(:bulk_perform_inline).with(args_list)

      described_class.bulk_perform_and_wait(args_list)
    end

    it 'runs > 3 jobs using sidekiq' do
      project.owner.project_authorizations.delete_all

      expect(described_class).to receive(:bulk_perform_async).and_call_original

      args_list = build_args_list(project.owner.id, multiply: 4)
      described_class.bulk_perform_and_wait(args_list)

      expect(project.owner.project_authorizations.count).to eq(1)
    end
  end

  describe '.bulk_perform_inline' do
    it 'refreshes the authorizations inline' do
42 43
      project.owner.project_authorizations.delete_all

44 45 46
      expect_any_instance_of(described_class).to receive(:perform).and_call_original

      described_class.bulk_perform_inline(build_args_list(project.owner.id))
47 48 49

      expect(project.owner.project_authorizations.count).to eq(1)
    end
50 51 52 53 54 55 56 57 58 59 60

    it 'enqueues jobs if an error is raised' do
      invalid_id = -1
      args_list = build_args_list(project.owner.id, invalid_id)

      allow_any_instance_of(described_class).to receive(:perform).with(project.owner.id)
      allow_any_instance_of(described_class).to receive(:perform).with(invalid_id).and_raise(ArgumentError)
      expect(described_class).to receive(:bulk_perform_async).with(build_args_list(invalid_id))

      described_class.bulk_perform_inline(args_list)
    end
61 62
  end

63 64
  describe '.bulk_perform_async' do
    it "uses it's respective sidekiq queue" do
65
      args_list = build_args_list(project.owner.id)
66 67 68
      push_bulk_args = {
        'class' => described_class,
        'queue' => described_class.sidekiq_options['queue'],
69
        'args' => args_list
70 71 72 73
      }

      expect(Sidekiq::Client).to receive(:push_bulk).with(push_bulk_args).once

74
      described_class.bulk_perform_async(args_list)
75 76 77
    end
  end

78
  describe '#perform' do
79
    let(:user) { create(:user) }
80

81
    subject(:job) { described_class.new }
82

83
    it "refreshes user's authorized projects" do
84
      expect_any_instance_of(User).to receive(:refresh_authorized_projects)
85

86 87 88 89 90 91 92
      job.perform(user.id)
    end

    it 'notifies the JobWaiter when done if the key is provided' do
      expect(Gitlab::JobWaiter).to receive(:notify).with('notify-key', job.jid)

      job.perform(user.id, 'notify-key')
93 94
    end

95
    context "when the user is not found" do
96
      it "does nothing" do
97
        expect_any_instance_of(User).not_to receive(:refresh_authorized_projects)
98

99
        job.perform(-1)
100 101 102 103
      end
    end
  end
end