2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe WaitableWorker do
|
2018-03-27 19:54:05 +05:30
|
|
|
let(:worker) do
|
|
|
|
Class.new do
|
|
|
|
def self.name
|
|
|
|
'Gitlab::Foo::Bar::DummyWorker'
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
cattr_accessor(:counter) { 0 }
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
include ApplicationWorker
|
|
|
|
prepend WaitableWorker
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def perform(count = 0)
|
|
|
|
self.class.counter += count
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject(:job) { worker.new }
|
|
|
|
|
|
|
|
describe '.bulk_perform_and_wait' do
|
2022-08-13 15:12:31 +05:30
|
|
|
context '1 job' do
|
2022-08-27 11:52:29 +05:30
|
|
|
it 'runs the jobs asynchronously' do
|
|
|
|
arguments = [[1]]
|
|
|
|
|
|
|
|
expect(worker).to receive(:bulk_perform_async).with(arguments)
|
|
|
|
|
|
|
|
worker.bulk_perform_and_wait(arguments)
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
context 'between 2 and 3 jobs' do
|
|
|
|
it 'runs the jobs asynchronously' do
|
|
|
|
arguments = [[1], [2], [3]]
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(worker).to receive(:bulk_perform_async).with(arguments)
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
worker.bulk_perform_and_wait(arguments)
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
context '>= 4 jobs' do
|
|
|
|
it 'runs jobs using sidekiq' do
|
|
|
|
arguments = 1.upto(5).map { |i| [i] }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(worker).to receive(:bulk_perform_async).with(arguments)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
worker.bulk_perform_and_wait(arguments)
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
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
|