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 @@ ...@@ -2,16 +2,16 @@
module Gitlab module Gitlab
class Daemon class Daemon
def self.initialize_instance(*args) def self.initialize_instance(...)
raise "#{name} singleton instance already initialized" if @instance raise "#{name} singleton instance already initialized" if @instance
@instance = new(*args) @instance = new(...)
Kernel.at_exit(&@instance.method(:stop)) Kernel.at_exit(&@instance.method(:stop))
@instance @instance
end end
def self.instance(*args) def self.instance(...)
@instance ||= initialize_instance(*args) @instance ||= initialize_instance(...)
end end
attr_reader :thread attr_reader :thread
...@@ -20,7 +20,8 @@ module Gitlab ...@@ -20,7 +20,8 @@ module Gitlab
!thread.nil? !thread.nil?
end end
def initialize def initialize(**options)
@synchronous = options[:synchronous]
@mutex = Mutex.new @mutex = Mutex.new
end end
...@@ -43,6 +44,10 @@ module Gitlab ...@@ -43,6 +44,10 @@ module Gitlab
Thread.current.name = thread_name Thread.current.name = thread_name
run_thread run_thread
end end
@thread.join if @synchronous
@thread
end end
end end
end end
......
...@@ -46,6 +46,30 @@ RSpec.describe Gitlab::Daemon do ...@@ -46,6 +46,30 @@ RSpec.describe Gitlab::Daemon do
expect(subject).to have_received(:run_thread) expect(subject).to have_received(:run_thread)
end 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 end
describe '#stop' do 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