Browse Source

Maintenance: Stabilize background services tests.

Martin Gruner 2 years ago
parent
commit
c873b03808

+ 1 - 2
spec/lib/background_services/service/process_delayed_jobs_spec.rb

@@ -33,11 +33,10 @@ RSpec.describe BackgroundServices::Service::ProcessDelayedJobs, ensure_threads_e
       it 'runs loop multiple times' do # rubocop:disable RSpec/MultipleExpectations
         allow(instance).to receive(:process_results)
 
-        thread = ensure_block_keeps_running_in_thread { instance.run }
+        ensure_block_keeps_running { instance.run }
 
         expect(instance).to have_received(:process_results).with([1, 0], any_args).once
         expect(instance).to have_received(:process_results).with([0, 0], any_args).at_least(1)
-        thread.join
       end
     end
   end

+ 2 - 2
spec/lib/background_services/service/process_scheduled_jobs/manager_spec.rb

@@ -89,7 +89,7 @@ RSpec.describe BackgroundServices::Service::ProcessScheduledJobs::Manager do
     end
 
     it 'skip if alive thread', ensure_threads_exited: true do
-      thread = Thread.new { sleep 0.1 }
+      thread = Thread.new { sleep 1000 } # will be stopped by ensure_threads_exited
       container[job.id] = thread
       expect(instance.send(:skip_already_running?)).to be_truthy
     end
@@ -150,7 +150,7 @@ RSpec.describe BackgroundServices::Service::ProcessScheduledJobs::Manager do
   describe '#start', ensure_threads_exited: true do
     it 'starts a thread' do
       container[job.id] = :thread
-      allow(instance).to receive(:start_in_thread).and_invoke(-> { sleep 0.1 })
+      allow(instance).to receive(:start_in_thread).and_invoke(-> { sleep 1000 }) # will be stopped by ensure_threads_exited
 
       thread = instance.send(:start)
       expect(thread).to be_alive

+ 1 - 2
spec/lib/background_services/service/process_scheduled_jobs_spec.rb

@@ -18,9 +18,8 @@ RSpec.describe BackgroundServices::Service::ProcessScheduledJobs do
   describe '#run' do
     it 'keeps running jobs', ensure_threads_exited: true do
       allow(instance).to receive(:run_jobs)
-      thread = ensure_block_keeps_running_in_thread { instance.run }
+      ensure_block_keeps_running { instance.run }
       expect(instance).to have_received(:run_jobs).at_least(2)
-      thread.join
     end
   end
 

+ 3 - 6
spec/lib/background_services_spec.rb

@@ -49,9 +49,8 @@ RSpec.describe BackgroundServices do
 
     it 'runs given services' do
       allow(instance).to receive(:run_service)
-      thread = ensure_block_keeps_running_in_thread { instance.run }
+      ensure_block_keeps_running { instance.run }
       expect(instance).to have_received(:run_service).with(config)
-      thread.join
     end
   end
 
@@ -120,11 +119,9 @@ RSpec.describe BackgroundServices do
 
       it 'runs Service#run' do
         instance.send(:start_as_forks, ProcessService, 1)
-        sleep 0.1
-
-        f = File.open ProcessService.path, 'r'
+        sleep 0.1 until File.exist? ProcessService.path
 
-        expect(f.read).to eq('run')
+        expect(File.read(ProcessService.path)).to eq('run')
       end
     end
 

+ 0 - 6
spec/support/threads.rb

@@ -24,12 +24,6 @@ module ThreadsHelper
     # Default case: process started fine and kept running, interrupted by timeout.
     true
   end
-
-  def ensure_block_keeps_running_in_thread(timeout: 2.seconds, sleep_duration: 0.1.seconds, &block)
-    thread = Thread.new { ensure_block_keeps_running(&block) }
-    sleep sleep_duration
-    thread
-  end
 end
 
 RSpec.configure do |config|