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

waitable_worker_spec.rb « concerns « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8cc12b0cb3016b2d520796e9fb761efa051e724c (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WaitableWorker do
  let(:worker) do
    Class.new do
      def self.name
        'Gitlab::Foo::Bar::DummyWorker'
      end

      cattr_accessor(:counter) { 0 }

      include ApplicationWorker
      prepend WaitableWorker

      # This is a workaround for a Ruby 2.3.7 bug. rspec-mocks cannot restore
      # the visibility of prepended modules. See
      # https://github.com/rspec/rspec-mocks/issues/1231 for more details.
      def self.bulk_perform_inline(args_list)
      end

      def perform(count = 0)
        self.class.counter += count
      end
    end
  end

  subject(:job) { worker.new }

  describe '.bulk_perform_and_wait' do
    context '1 job' do
      it 'inlines the job' do
        args_list = [[1]]
        expect(worker).to receive(:bulk_perform_inline).with(args_list).and_call_original
        expect(Gitlab::AppJsonLogger).to(
          receive(:info).with(a_hash_including('message' => 'running inline',
                                               'class' => 'Gitlab::Foo::Bar::DummyWorker',
                                               'job_status' => 'running',
                                               'queue' => 'foo_bar_dummy'))
                        .once)

        worker.bulk_perform_and_wait(args_list)

        expect(worker.counter).to eq(1)
      end
    end

    context 'between 2 and 3 jobs' do
      it 'runs the jobs asynchronously' do
        arguments = [[1], [2], [3]]

        expect(worker).to receive(:bulk_perform_async).with(arguments)

        worker.bulk_perform_and_wait(arguments)
      end

      context 'when the feature flag `inline_project_authorizations_refresh_only_for_single_element` is turned off' do
        before do
          stub_feature_flags(inline_project_authorizations_refresh_only_for_single_element: false)
        end

        it 'inlines the jobs' do
          args_list = [[1], [2], [3]]
          expect(worker).to receive(:bulk_perform_inline).with(args_list).and_call_original
          expect(Gitlab::AppJsonLogger).to(
            receive(:info).with(a_hash_including('message' => 'running inline',
                                                 'class' => 'Gitlab::Foo::Bar::DummyWorker',
                                                 'job_status' => 'running',
                                                 'queue' => 'foo_bar_dummy'))
                          .exactly(3).times)

          worker.bulk_perform_and_wait(args_list)
        end
      end
    end

    context '>= 4 jobs' do
      it 'runs jobs using sidekiq and no waiter key' do
        arguments = 1.upto(5).map { |i| [i] }

        expect(worker).to receive(:bulk_perform_async).with(arguments)

        worker.bulk_perform_and_wait(arguments, timeout: 2)
      end

      it 'runs > 10 * timeout jobs using sidekiq and no waiter key' do
        arguments = 1.upto(21).map { |i| [i] }

        expect(worker).to receive(:bulk_perform_async).with(arguments)

        worker.bulk_perform_and_wait(arguments, timeout: 2)
      end

      context 'when the feature flag `async_only_project_authorizations_refresh` is turned off' do
        before do
          stub_feature_flags(async_only_project_authorizations_refresh: false)
        end

        it 'runs > 3 jobs using sidekiq and a waiter key' do
          expect(worker).to receive(:bulk_perform_async)
                              .with([[1, anything], [2, anything], [3, anything], [4, anything]])

          worker.bulk_perform_and_wait([[1], [2], [3], [4]])
        end
      end
    end
  end

  describe '.bulk_perform_inline' do
    it 'runs the jobs inline' do
      expect(worker).not_to receive(:bulk_perform_async)

      worker.bulk_perform_inline([[1], [2]])

      expect(worker.counter).to eq(3)
    end

    it 'enqueues jobs if an error is raised' do
      expect(worker).to receive(:bulk_perform_async).with([['foo']])

      worker.bulk_perform_inline([[1], ['foo']])
    end
  end

  describe '#perform' do
    shared_examples 'perform' do
      it 'notifies the JobWaiter when done if the key is provided' do
        key = Gitlab::JobWaiter.new.key
        expect(Gitlab::JobWaiter).to receive(:notify).with(key, job.jid)

        job.perform(*args, key)
      end

      it 'does not notify the JobWaiter when done if no key is provided' do
        expect(Gitlab::JobWaiter).not_to receive(:notify)

        job.perform(*args)
      end
    end

    context 'when the worker takes arguments' do
      let(:args) { [1] }

      it_behaves_like 'perform'
    end

    context 'when the worker takes no arguments' do
      let(:args) { [] }

      it_behaves_like 'perform'
    end
  end
end