Skip to content

Commit ea24015

Browse files
Run container tests in async scheduler context
1 parent b3758cd commit ea24015

5 files changed

Lines changed: 99 additions & 56 deletions

File tree

fixtures/container_context.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require "async"
4+
require "async/container/group"
5+
6+
module ContainerContext
7+
def container_context(&block)
8+
# Newer async-container supervises containers using Async tasks, while
9+
# released versions can fail when forking under a scheduler on Ruby 3.x.
10+
if Async::Container::Group.method_defined?(:supervise)
11+
Sync(&block)
12+
else
13+
block.call
14+
end
15+
end
16+
end

test/falcon/command/serve.rb

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
# Copyright, 2019, by Sho Ito.
66

77
require "falcon/command/serve"
8+
require "container_context"
89
require "sus/fixtures/console/captured_logger"
910

1011
ServeCommand = Sus::Shared("falcon serve") do
12+
include ContainerContext
13+
1114
let(:command) do
1215
subject[
1316
"--port", port,
@@ -16,23 +19,25 @@
1619
end
1720

1821
it "can listen on specified port" do
19-
configuration = command.configuration
20-
controller = configuration.make_controller
21-
22-
controller.start
23-
24-
begin
25-
Async do
26-
client = command.client
27-
28-
response = client.get("/")
29-
expect(response).to be(:success?)
30-
31-
response.finish
32-
client.close
22+
container_context do
23+
configuration = command.configuration
24+
controller = configuration.make_controller
25+
26+
controller.start
27+
28+
begin
29+
Async do
30+
client = command.client
31+
32+
response = client.get("/")
33+
expect(response).to be(:success?)
34+
35+
response.finish
36+
client.close
37+
end
38+
ensure
39+
controller.stop
3340
end
34-
ensure
35-
controller.stop
3641
end
3742
end
3843
end

test/falcon/command/top.rb

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
# Copyright, 2018-2026, by Samuel Williams.
55

66
require "falcon/command"
7+
require "container_context"
78
require "sus/fixtures/console/captured_logger"
89

910
describe Falcon::Command::Top do
11+
include ContainerContext
1012
include Sus::Fixtures::Console::CapturedLogger
1113

1214
with "basic server configuration" do
@@ -19,20 +21,25 @@
1921
]
2022

2123
serve = top.command
22-
controller = serve.configuration.make_controller
23-
controller.start
2424

25-
Async do
26-
client = serve.client
25+
container_context do
26+
controller = serve.configuration.make_controller
27+
controller.start
2728

28-
response = client.get("/")
29-
expect(response).to be(:success?)
30-
31-
response.finish
32-
client.close
29+
begin
30+
Async do
31+
client = serve.client
32+
33+
response = client.get("/")
34+
expect(response).to be(:success?)
35+
36+
response.finish
37+
client.close
38+
end
39+
ensure
40+
controller.stop
41+
end
3342
end
34-
35-
controller.stop
3643
end
3744
end
3845

test/falcon/command/virtual.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Copyright, 2019-2026, by Samuel Williams.
55

66
require "falcon/command/virtual"
7+
require "async"
8+
require "container_context"
79
require "sus/fixtures/console/captured_logger"
810

911
require "async/http"
@@ -12,6 +14,8 @@
1214
require "async/websocket/client"
1315

1416
VirtualCommand = Sus::Shared("falcon virtual") do
17+
include ContainerContext
18+
1519
let(:paths) {[
1620
File.expand_path("hello/falcon.rb", examples_root),
1721
File.expand_path("beer/falcon.rb", examples_root),
@@ -32,15 +36,17 @@
3236
end
3337

3438
def around
35-
configuration = command.configuration
36-
controller = configuration.make_controller
37-
38-
controller.start
39-
40-
begin
41-
yield
42-
ensure
43-
controller.stop
39+
container_context do
40+
configuration = command.configuration
41+
controller = configuration.make_controller
42+
43+
controller.start
44+
45+
begin
46+
yield
47+
ensure
48+
controller.stop
49+
end
4450
end
4551
end
4652

test/falcon/service/server.rb

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
require "falcon/service/server"
77
require "falcon/environment/server"
88
require "falcon/environment/rackup"
9+
require "async/container"
10+
require "async/service/environment"
11+
require "container_context"
912

1013
describe Falcon::Service::Server do
14+
include ContainerContext
15+
1116
let(:environment) do
1217
Async::Service::Environment.new(Falcon::Environment::Server).with(
1318
Falcon::Environment::Rackup,
@@ -26,16 +31,18 @@
2631
end
2732

2833
it "can start and stop server" do
29-
container = Async::Container.new
30-
31-
server.start
32-
server.setup(container)
33-
container.wait_until_ready
34-
35-
expect(container.group.running).to have_attributes(size: be == Etc.nprocessors)
36-
37-
server.stop
38-
container.stop
34+
container_context do
35+
container = Async::Container.new
36+
37+
server.start
38+
server.setup(container)
39+
container.wait_until_ready
40+
41+
expect(container.group.running).to have_attributes(size: be == Etc.nprocessors)
42+
ensure
43+
server.stop
44+
container&.stop
45+
end
3946
end
4047

4148
with "a limited count" do
@@ -50,16 +57,18 @@
5057
end
5158

5259
it "can start and stop server" do
53-
container = Async::Container.new
54-
55-
server.start
56-
server.setup(container)
57-
container.wait_until_ready
58-
59-
expect(container.group.running).to have_attributes(size: be == 1)
60-
61-
server.stop
62-
container.stop
60+
container_context do
61+
container = Async::Container.new
62+
63+
server.start
64+
server.setup(container)
65+
container.wait_until_ready
66+
67+
expect(container.group.running).to have_attributes(size: be == 1)
68+
ensure
69+
server.stop
70+
container&.stop
71+
end
6372
end
6473
end
6574

0 commit comments

Comments
 (0)