|
4 | 4 | # Copyright, 2017-2025, by Samuel Williams. |
5 | 5 |
|
6 | 6 | require "async/container" |
| 7 | +require "open3" |
| 8 | +require "rbconfig" |
7 | 9 |
|
8 | 10 | 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 | + |
9 | 24 | with ".processor_count" do |
10 | 25 | it "can get processor count" do |
11 | 26 | expect(Async::Container.processor_count).to be >= 1 |
|
35 | 50 | end |
36 | 51 | end |
37 | 52 |
|
| 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 | + |
38 | 93 | with ".best" do |
39 | 94 | it "can get the best container class" do |
40 | 95 | expect(Async::Container.best_container_class).not.to be_nil |
|
0 commit comments