Skip to content

Commit 42e33d4

Browse files
committed
Refactor slightly how to wait for things during tests
And wait until all semaphores have been signalled, as it might happen that jobs finish and before they've signaled the semaphore, we try to wait on it, having the test fail, if the timing aligns.
1 parent 808d9fb commit 42e33d4

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

test/integration/concurrency_controls_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class ConcurrencyControlsTest < ActiveSupport::TestCase
8989
wait_for_jobs_to_finish_for(3.seconds)
9090
assert_no_pending_jobs
9191

92+
wait_while_with_timeout(1.second) { SolidQueue::Semaphore.where(value: 0).any? }
9293
# Lock the semaphore so we can enqueue jobs and leave them blocked
9394
skip_active_record_query_cache do
9495
assert SolidQueue::Semaphore.wait(job)
@@ -121,6 +122,7 @@ class ConcurrencyControlsTest < ActiveSupport::TestCase
121122
wait_for_jobs_to_finish_for(3.seconds)
122123
assert_no_pending_jobs
123124

125+
wait_while_with_timeout(1.second) { SolidQueue::Semaphore.where(value: 0).any? }
124126
# Lock the semaphore so we can enqueue jobs and leave them blocked
125127
skip_active_record_query_cache do
126128
assert SolidQueue::Semaphore.wait(job)

test/integration/processes_lifecycle_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ProcessLifecycleTest < ActiveSupport::TestCase
88
setup do
99
@pid = run_supervisor_as_fork
1010

11-
wait_for_registered_processes(3, timeout: 0.2.second)
11+
wait_for_registered_processes(3, timeout: 1.second)
1212
assert_registered_workers_for(:background, :default)
1313
end
1414

test/test_helper.rb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,7 @@ class ActiveSupport::TestCase
3636

3737
private
3838
def wait_for_jobs_to_finish_for(timeout = 1.second)
39-
skip_active_record_query_cache do
40-
Timeout.timeout(timeout) do
41-
while SolidQueue::Job.where(finished_at: nil).any? do
42-
sleep 0.05
43-
end
44-
end
45-
end
46-
rescue Timeout::Error
39+
wait_while_with_timeout(timeout) { SolidQueue::Job.where(finished_at: nil).any? }
4740
end
4841

4942
def assert_no_pending_jobs
@@ -59,12 +52,7 @@ def run_supervisor_as_fork(**options)
5952
end
6053

6154
def wait_for_registered_processes(count, timeout: 1.second)
62-
Timeout.timeout(timeout) do
63-
while SolidQueue::Process.count != count do
64-
sleep 0.05
65-
end
66-
end
67-
rescue Timeout::Error
55+
wait_while_with_timeout(timeout) { SolidQueue::Process.count != count }
6856
end
6957

7058
def assert_no_registered_processes
@@ -86,14 +74,27 @@ def terminate_process(pid, timeout: 10, signal: :TERM)
8674

8775
def wait_for_process_termination_with_timeout(pid, timeout: 10, exitstatus: 0)
8876
Timeout.timeout(timeout) do
89-
Process.waitpid(pid)
90-
assert exitstatus, $?.exitstatus
77+
if process_exists?(pid)
78+
Process.waitpid(pid)
79+
assert exitstatus, $?.exitstatus
80+
end
9181
end
9282
rescue Timeout::Error
9383
signal_process(pid, :KILL)
9484
raise
9585
end
9686

87+
def wait_while_with_timeout(timeout, &block)
88+
Timeout.timeout(timeout) do
89+
skip_active_record_query_cache do
90+
while block.call
91+
sleep 0.05
92+
end
93+
end
94+
end
95+
rescue Timeout::Error
96+
end
97+
9798
def signal_process(pid, signal, wait: nil)
9899
Thread.new do
99100
sleep(wait) if wait

0 commit comments

Comments
 (0)