debian-mirror-gitlab/spec/lib/gitlab/job_waiter_spec.rb

78 lines
2.4 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2021-01-03 14:25:43 +05:30
RSpec.describe Gitlab::JobWaiter, :redis do
2018-03-17 18:26:18 +05:30
describe '.notify' do
it 'pushes the jid to the named queue' do
2021-01-03 14:25:43 +05:30
key = described_class.new.key
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
described_class.notify(key, 123)
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
Gitlab::Redis::SharedState.with do |redis|
expect(redis.ttl(key)).to be > 0
end
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
end
describe '#wait' do
let(:waiter) { described_class.new(2) }
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
before do
allow_any_instance_of(described_class).to receive(:wait).and_call_original
end
2018-03-17 18:26:18 +05:30
it 'returns when all jobs have been completed' do
described_class.notify(waiter.key, 'a')
described_class.notify(waiter.key, 'b')
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
result = nil
expect { Timeout.timeout(1) { result = waiter.wait(2) } }.not_to raise_error
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
expect(result).to contain_exactly('a', 'b')
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
it 'times out if not all jobs complete' do
described_class.notify(waiter.key, 'a')
result = nil
expect { Timeout.timeout(2) { result = waiter.wait(1) } }.not_to raise_error
expect(result).to contain_exactly('a')
2017-08-17 22:00:37 +05:30
end
2020-04-08 14:13:33 +05:30
context 'when a label is provided' do
let(:waiter) { described_class.new(2, worker_label: 'Foo') }
let(:started_total) { double(:started_total) }
let(:timeouts_total) { double(:timeouts_total) }
before do
allow(Gitlab::Metrics).to receive(:counter)
.with(described_class::STARTED_METRIC, anything)
.and_return(started_total)
allow(Gitlab::Metrics).to receive(:counter)
.with(described_class::TIMEOUTS_METRIC, anything)
.and_return(timeouts_total)
end
it 'increments just job_waiter_started_total when all jobs complete' do
expect(started_total).to receive(:increment).with(worker: 'Foo')
expect(timeouts_total).not_to receive(:increment)
described_class.notify(waiter.key, 'a')
described_class.notify(waiter.key, 'b')
2020-10-24 23:57:45 +05:30
expect { Timeout.timeout(1) { waiter.wait(2) } }.not_to raise_error
2020-04-08 14:13:33 +05:30
end
it 'increments job_waiter_started_total and job_waiter_timeouts_total when it times out' do
expect(started_total).to receive(:increment).with(worker: 'Foo')
expect(timeouts_total).to receive(:increment).with(worker: 'Foo')
2020-10-24 23:57:45 +05:30
expect { Timeout.timeout(2) { waiter.wait(1) } }.not_to raise_error
2020-04-08 14:13:33 +05:30
end
end
2017-08-17 22:00:37 +05:30
end
end