summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_task_spec.rb
blob: 102556b6b2f84e2c041c2d05d3d83f583a8a2e77 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# frozen_string_literal: true

require 'fast_spec_helper'

# We need to capture task state from a closure, which requires instance variables.
# rubocop: disable RSpec/InstanceVariable
RSpec.describe Gitlab::BackgroundTask do
  let(:options) { {} }
  let(:task) do
    proc do
      @task_run = true
      @task_thread = Thread.current
    end
  end

  subject(:background_task) { described_class.new(task, **options) }

  def expect_condition
    Timeout.timeout(3) do
      sleep 0.1 until yield
    end
  end

  context 'when stopped' do
    it 'is not running' do
      expect(background_task).not_to be_running
    end

    describe '#start' do
      it 'runs the given task on a background thread' do
        test_thread = Thread.current

        background_task.start

        expect_condition { @task_run == true }
        expect_condition { @task_thread != test_thread }
        expect(background_task).to be_running
      end

      it 'returns self' do
        expect(background_task.start).to be(background_task)
      end

      context 'when installing exit handler' do
        it 'stops a running background task' do
          expect(background_task).to receive(:at_exit).and_yield

          background_task.start

          expect(background_task).not_to be_running
        end
      end

      context 'when task responds to start' do
        let(:task_class) do
          Struct.new(:started, :start_retval, :run) do
            def start
              self.started = true
              self.start_retval
            end

            def call
              self.run = true
            end
          end
        end

        let(:task) { task_class.new }

        it 'calls start' do
          background_task.start

          expect_condition { task.started == true }
        end

        context 'when start returns true' do
          it 'runs the task' do
            task.start_retval = true

            background_task.start

            expect_condition { task.run == true }
          end
        end

        context 'when start returns false' do
          it 'does not run the task' do
            task.start_retval = false

            background_task.start

            expect_condition { task.run.nil? }
          end
        end
      end

      context 'when synchronous is set to true' do
        let(:options) { { synchronous: true } }

        it 'calls join on the thread' do
          # Thread has to be run in a block, expect_next_instance_of does not support this.
          allow_any_instance_of(Thread).to receive(:join) # rubocop:disable RSpec/AnyInstanceOf

          background_task.start

          expect_condition { @task_run == true }
          expect(@task_thread).to have_received(:join)
        end
      end
    end

    describe '#stop' do
      it 'is a no-op' do
        expect { background_task.stop }.not_to change { subject.running? }
        expect_condition { @task_run.nil? }
      end
    end
  end

  context 'when running' do
    before do
      background_task.start
    end

    describe '#start' do
      it 'raises an error' do
        expect { background_task.start }.to raise_error(described_class::AlreadyStartedError)
      end
    end

    describe '#stop' do
      it 'stops running' do
        expect { background_task.stop }.to change { subject.running? }.from(true).to(false)
      end

      context 'when task responds to stop' do
        let(:task_class) do
          Struct.new(:stopped, :call) do
            def stop
              self.stopped = true
            end
          end
        end

        let(:task) { task_class.new }

        it 'calls stop' do
          background_task.stop

          expect_condition { task.stopped == true }
        end
      end

      context 'when task stop raises an error' do
        let(:error) { RuntimeError.new('task error') }
        let(:options) { { name: 'test_background_task' } }

        let(:task_class) do
          Struct.new(:call, :error, keyword_init: true) do
            def stop
              raise error
            end
          end
        end

        let(:task) { task_class.new(error: error) }

        it 'stops gracefully' do
          expect { background_task.stop }.not_to raise_error
          expect(background_task).not_to be_running
        end

        it 'reports the error' do
          expect(Gitlab::ErrorTracking).to receive(:track_exception).with(
            error, { extra: { reported_by: 'test_background_task' } }
          )

          background_task.stop
        end
      end
    end

    context 'when task run raises exception' do
      let(:error) { RuntimeError.new('task error') }
      let(:options) { { name: 'test_background_task' } }
      let(:task) do
        proc do
          @task_run = true
          raise error
        end
      end

      it 'stops gracefully' do
        expect_condition { @task_run == true }
        expect { background_task.stop }.not_to raise_error
        expect(background_task).not_to be_running
      end

      it 'reports the error' do
        expect(Gitlab::ErrorTracking).to receive(:track_exception).with(
          error, { extra: { reported_by: 'test_background_task' } }
        )

        background_task.stop
      end
    end
  end
end
# rubocop: enable RSpec/InstanceVariable