Skip to content

Commit 818bfd3

Browse files
Adopt async-signals graceful defaults. (#68)
1 parent 9913c30 commit 818bfd3

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

async-container.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
2525
spec.required_ruby_version = ">= 3.3"
2626

2727
spec.add_dependency "async", "~> 2.22"
28+
spec.add_dependency "async-signals", "~> 0.6"
2829
end

lib/async/container/controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
require_relative "notify"
1111
require_relative "policy"
1212

13+
# This sets up graceful handling of SIGINT and SIGTERM.
14+
require "async/signals/graceful"
15+
1316
module Async
1417
module Container
1518
# The default graceful stop policy for controllers.

test/async/container.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@
44
# Copyright, 2017-2025, by Samuel Williams.
55

66
require "async/container"
7+
require "open3"
8+
require "rbconfig"
79

810
describe Async::Container do
11+
def ruby(script)
12+
::Open3.capture3(::RbConfig.ruby, "-Ilib", "-e", script)
13+
end
14+
15+
def expect_success(script)
16+
stdout, stderr, status = ruby(script)
17+
18+
expect(status).to be(:success?)
19+
expect(stderr).to be == ""
20+
21+
return stdout
22+
end
23+
924
with ".processor_count" do
1025
it "can get processor count" do
1126
expect(Async::Container.processor_count).to be >= 1
@@ -35,6 +50,46 @@
3550
end
3651
end
3752

53+
with "graceful signal defaults" do
54+
it "installs graceful SIGINT handling" do
55+
stdout = expect_success(<<~RUBY)
56+
require "async/container"
57+
58+
begin
59+
::Thread.handle_interrupt(::Interrupt => :never) do
60+
::Process.kill(:INT, ::Process.pid)
61+
puts "inner"
62+
end
63+
64+
sleep 1
65+
rescue ::Interrupt
66+
puts "outer"
67+
end
68+
RUBY
69+
70+
expect(stdout).to be == "inner\nouter\n"
71+
end
72+
73+
it "installs graceful SIGTERM handling" do
74+
stdout = expect_success(<<~RUBY)
75+
require "async/container"
76+
77+
begin
78+
::Thread.handle_interrupt(::Interrupt => :never) do
79+
::Process.kill(:TERM, ::Process.pid)
80+
puts "inner"
81+
end
82+
83+
sleep 1
84+
rescue ::Interrupt
85+
puts "outer"
86+
end
87+
RUBY
88+
89+
expect(stdout).to be == "inner\nouter\n"
90+
end
91+
end
92+
3893
with ".best" do
3994
it "can get the best container class" do
4095
expect(Async::Container.best_container_class).not.to be_nil

0 commit comments

Comments
 (0)