2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Daemon do
|
2017-09-10 17:25:29 +05:30
|
|
|
subject { described_class.new }
|
|
|
|
|
|
|
|
before do
|
2019-12-21 20:55:43 +05:30
|
|
|
allow(subject).to receive(:run_thread)
|
2017-09-10 17:25:29 +05:30
|
|
|
allow(subject).to receive(:stop_working)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.instance' do
|
|
|
|
before do
|
|
|
|
allow(Kernel).to receive(:at_exit)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
after do
|
2017-09-10 17:25:29 +05:30
|
|
|
described_class.instance_variable_set(:@instance, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'provides instance of Daemon' do
|
|
|
|
expect(described_class.instance).to be_instance_of(described_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'subsequent invocations provide the same instance' do
|
|
|
|
expect(described_class.instance).to eq(described_class.instance)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates at_exit hook when instance is created' do
|
|
|
|
expect(described_class.instance).not_to be_nil
|
|
|
|
|
|
|
|
expect(Kernel).to have_received(:at_exit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when Daemon is enabled' do
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
|
|
|
allow(subject).to receive(:enabled?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when Daemon is stopped' do
|
2017-09-10 17:25:29 +05:30
|
|
|
describe '#start' do
|
|
|
|
it 'starts the Daemon' do
|
|
|
|
expect { subject.start.join }.to change { subject.thread? }.from(false).to(true)
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(subject).to have_received(:run_thread)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
context '@synchronous' do
|
|
|
|
context 'when @synchronous is set to true' do
|
|
|
|
subject { described_class.instance(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.
|
|
|
|
expect_any_instance_of(Thread).to receive(:join) # rubocop:disable RSpec/AnyInstanceOf
|
|
|
|
|
|
|
|
subject.start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when @synchronous is not set to a truthy value' do
|
|
|
|
subject { described_class.instance }
|
|
|
|
|
|
|
|
it 'does not call join on the thread' do
|
|
|
|
# Thread has to be run in a block, expect_next_instance_of does not support this.
|
|
|
|
expect_any_instance_of(Thread).not_to receive(:join) # rubocop:disable RSpec/AnyInstanceOf
|
|
|
|
|
|
|
|
subject.start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#stop' do
|
|
|
|
it "doesn't shutdown stopped Daemon" do
|
|
|
|
expect { subject.stop }.not_to change { subject.thread? }
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(subject).not_to have_received(:run_thread)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#start_working' do
|
|
|
|
context 'when start_working fails' do
|
|
|
|
before do
|
|
|
|
expect(subject).to receive(:start_working) { false }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not start thread' do
|
|
|
|
expect(subject).not_to receive(:run_thread)
|
|
|
|
|
|
|
|
expect(subject.start).to eq(nil)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when Daemon is running' do
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
subject.start
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#start' do
|
|
|
|
it "doesn't start running Daemon" do
|
2019-12-04 20:38:33 +05:30
|
|
|
expect { subject.start.join }.not_to change { subject.thread }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(subject).to have_received(:run_thread).once
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#stop' do
|
|
|
|
it 'shutdowns Daemon' do
|
|
|
|
expect { subject.stop }.to change { subject.thread? }.from(true).to(false)
|
|
|
|
|
|
|
|
expect(subject).to have_received(:stop_working)
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
context 'when stop_working raises exception' do
|
|
|
|
before do
|
2019-12-21 20:55:43 +05:30
|
|
|
allow(subject).to receive(:run_thread) do
|
2019-12-04 20:38:33 +05:30
|
|
|
sleep(1000)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shutdowns Daemon' do
|
|
|
|
expect(subject).to receive(:stop_working) do
|
|
|
|
subject.thread.raise(Interrupt)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(subject.thread).to be_alive
|
|
|
|
expect { subject.stop }.not_to raise_error
|
|
|
|
expect(subject.thread).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when Daemon is disabled' do
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
|
|
|
allow(subject).to receive(:enabled?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#start' do
|
|
|
|
it "doesn't start working" do
|
|
|
|
expect(subject.start).to be_nil
|
|
|
|
expect { subject.start }.not_to change { subject.thread? }
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(subject).not_to have_received(:run_thread)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#stop' do
|
|
|
|
it "doesn't stop working" do
|
|
|
|
expect { subject.stop }.not_to change { subject.thread? }
|
|
|
|
|
|
|
|
expect(subject).not_to have_received(:stop_working)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|