Commit 61034e85 authored by Roy Zwambag's avatar Roy Zwambag

Allow daemon to receive a synchronous option that joins threads

parent cc2f3a8c
......@@ -2,16 +2,16 @@
module Gitlab
class Daemon
def self.initialize_instance(*args)
def self.initialize_instance(...)
raise "#{name} singleton instance already initialized" if @instance
@instance = new(*args)
@instance = new(...)
Kernel.at_exit(&@instance.method(:stop))
@instance
end
def self.instance(*args)
@instance ||= initialize_instance(*args)
def self.instance(...)
@instance ||= initialize_instance(...)
end
attr_reader :thread
......@@ -20,7 +20,8 @@ module Gitlab
!thread.nil?
end
def initialize
def initialize(**options)
@synchronous = options[:synchronous]
@mutex = Mutex.new
end
......@@ -43,6 +44,10 @@ module Gitlab
Thread.current.name = thread_name
run_thread
end
@thread.join if @synchronous
@thread
end
end
end
......
......@@ -46,6 +46,30 @@ RSpec.describe Gitlab::Daemon do
expect(subject).to have_received(:run_thread)
end
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
end
describe '#stop' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment