Skip to content

Commit 4b8bac0

Browse files
committed
handle term signal same as int signal
1 parent 84dab0c commit 4b8bac0

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

lib/parallel_tests.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def pid_file_path
5656
ENV.fetch('PARALLEL_PID_FILE')
5757
end
5858

59-
def stop_all_processes
60-
pids.all.each { |pid| Process.kill(:INT, pid) }
59+
def stop_all_processes(signal)
60+
pids.all.each { |pid| Process.kill(signal, pid) }
6161
rescue Errno::ESRCH, Errno::EPERM
6262
# Process already terminated, do nothing
6363
end

lib/parallel_tests/cli.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
module ParallelTests
99
class CLI
10+
SIGNALS = %w[INT TERM]
1011
def run(argv)
11-
Signal.trap("INT") { handle_interrupt }
12+
SIGNALS.each do |signal|
13+
Signal.trap(signal) { handle_interrupt(signal) }
14+
end
1215

1316
options = parse_options!(argv)
1417

@@ -28,7 +31,7 @@ def run(argv)
2831

2932
private
3033

31-
def handle_interrupt
34+
def handle_interrupt(signal)
3235
@graceful_shutdown_attempted ||= false
3336
Kernel.exit if @graceful_shutdown_attempted
3437

@@ -46,7 +49,7 @@ def handle_interrupt
4649
# using Thread workaround https://github.com/ddollar/foreman/issues/332
4750
Thread.new do
4851
if Gem.win_platform? || ((child_pid = ParallelTests.pids.all.first) && Process.getpgid(child_pid) != Process.pid)
49-
ParallelTests.stop_all_processes
52+
ParallelTests.stop_all_processes(signal)
5053
end
5154
end
5255

spec/integration_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,21 @@ class A < Spinach::FeatureSteps
722722
expect(result).to_not include("Should not get here either")
723723
end
724724

725+
it "passes on term signal to child processes", unless: Gem.win_platform? do
726+
timeout = 2
727+
write(
728+
"spec/test_spec.rb",
729+
"sleep #{timeout}; describe { specify { p 'Should not get here' }; specify { p 'Should not get here either'} }"
730+
)
731+
pid = nil
732+
Thread.new { sleep timeout - 1.0; Process.kill("TERM", pid) }
733+
result = run_tests(["spec"], processes: 2, type: 'rspec', fail: true) { |io| pid = io.pid }
734+
735+
expect(result).to include("RSpec is shutting down")
736+
expect(result).to_not include("Should not get here")
737+
expect(result).to_not include("Should not get here either")
738+
end
739+
725740
# Process.kill on Windows doesn't work as expected. It kills all process group instead of just one process.
726741
it "exits immediately if another int signal is received", unless: Gem.win_platform? do
727742
timeout = 2

0 commit comments

Comments
 (0)