Skip to content

Commit deb0d9f

Browse files
wuputahhone
authored andcommitted
add test case for SIGTERM
big credit here to @yaauie
1 parent 8c6450f commit deb0d9f

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

test/worker_test.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,4 +437,65 @@ def self.perform
437437
@worker.work(0)
438438
assert_not_equal original_connection, Resque.redis.client.connection.instance_variable_get("@sock")
439439
end
440+
441+
442+
test 'SIGTERM' do
443+
begin
444+
class LongRunningJob
445+
@queue = :long_running_job
446+
def self.perform(time)
447+
$child_pid_writer.write Process.pid
448+
$child_pid_writer.close
449+
sleep(time)
450+
rescue SignalException => e
451+
$child_worker_message_writer.write %Q(SignalException caught! #{e.inspect})
452+
$child_worker_message_writer.close
453+
end
454+
end
455+
456+
$child_pid_reader, $child_pid_writer = IO.pipe
457+
$child_worker_message_reader, $child_worker_message_writer = IO.pipe
458+
459+
Resque.enqueue(LongRunningJob, 2)
460+
461+
worker_pid = Kernel.fork do
462+
# don't hold up the read-end of the pipe
463+
$child_pid_reader.close
464+
# ensure we actually fork
465+
$TESTING = false
466+
# reconnect since we just forked
467+
Resque.redis.client.reconnect
468+
469+
worker = Resque::Worker.new(:long_running_job)
470+
worker.term_timeout = 1
471+
472+
worker.work(0)
473+
exit!
474+
end
475+
476+
# close writers in parent now that we've forked
477+
$child_pid_writer.close
478+
$child_worker_message_writer.close
479+
480+
# give it a moment to start up the worker
481+
sleep(0.2)
482+
483+
# send signal to abort the worker
484+
Process.kill('TERM', worker_pid)
485+
Process.waitpid(worker_pid)
486+
487+
# try to get the child pid
488+
child_pid = Timeout.timeout(1.0) { $child_pid_reader.read.to_i }
489+
assert child_pid > 0
490+
491+
child_still_running = !(`ps -p #{child_pid.to_s} -o pid=`).empty?
492+
assert !child_still_running
493+
494+
child_exception = $child_worker_message_reader.read
495+
assert_not_equal '', child_exception, 'child did not raise SignalException'
496+
ensure
497+
$child_pid_reader, $child_pid_writer, $child_worker_message_reader, $child_worker_message_writer = nil, nil, nil, nil
498+
end
499+
end
500+
440501
end

0 commit comments

Comments
 (0)