Skip to content

Commit 518a754

Browse files
Add scheduler context container tests. (#64)
1 parent 83f7b0c commit 518a754

6 files changed

Lines changed: 109 additions & 0 deletions

File tree

fixtures/async/container/a_container.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,71 @@ module Container
7373
end
7474
end
7575

76+
with "Async{}" do
77+
it "can wait inside an Async task after spawning outside Async" do
78+
input, output = IO.pipe
79+
80+
container.spawn do
81+
output.write(".")
82+
end
83+
84+
Async do
85+
container.wait
86+
end.wait
87+
88+
output.close
89+
expect(input.read).to be == "."
90+
end
91+
92+
it "can spawn and wait inside the same Async task" do
93+
input, output = IO.pipe
94+
95+
Async do
96+
container.spawn do
97+
output.write(".")
98+
end
99+
100+
container.wait
101+
end.wait
102+
103+
output.close
104+
expect(input.read).to be == "."
105+
end
106+
107+
it "can wait while a health monitor is active" do
108+
container.spawn(health_check_timeout: 10.0) do |instance|
109+
instance.ready!
110+
end
111+
112+
Async do |task|
113+
task.with_timeout(2.0) do
114+
container.wait
115+
end
116+
end.wait
117+
118+
expect(container.statistics).to have_attributes(failures: be == 0)
119+
end
120+
121+
it "can start, wait until ready, and stop inside Sync" do
122+
Sync do
123+
begin
124+
2.times do |i|
125+
container.spawn(name: "worker #{i}") do |instance|
126+
instance.ready!
127+
sleep
128+
end
129+
end
130+
131+
container.wait_until_ready
132+
133+
expect(container.group.running).to have_attributes(size: be == 2)
134+
ensure
135+
container.stop if container.running?
136+
end
137+
end
138+
end
139+
end
140+
76141
it "should be blocking" do
77142
skip "Fiber.blocking? is not supported!" unless Fiber.respond_to?(:blocking?)
78143

gems.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
group :test do
2323
gem "sus"
24+
gem "sus-fixtures-async"
2425
gem "covered"
2526

2627
gem "rubocop"

test/async/container/forked.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
require "async/container/best"
88
require "async/container/forked"
99
require "async/container/a_container"
10+
require "sus/fixtures/async/scheduler_context"
1011

1112
describe Async::Container::Forked do
13+
include Sus::Fixtures::Async::SchedulerContext
14+
1215
let(:container) {subject.new}
1316

1417
it_behaves_like Async::Container::AContainer

test/async/container/hybrid.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
require "async/container/hybrid"
77
require "async/container/best"
88
require "async/container/a_container"
9+
require "sus/fixtures/async/scheduler_context"
910

1011
describe Async::Container::Hybrid do
12+
include Sus::Fixtures::Async::SchedulerContext
13+
1114
it_behaves_like Async::Container::AContainer
1215

1316
it "should be multiprocess" do

test/async/container/policy.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
require "async/container/policy"
77
require "async/container/best"
8+
require "sus/fixtures/async/scheduler_context"
89

910
describe Async::Container::Policy do
11+
include Sus::Fixtures::Async::SchedulerContext
12+
1013
let(:policy) {subject.new}
1114

1215
with "interface" do
@@ -234,6 +237,37 @@ def child_exit(container, child, status, name:, key:, **options)
234237
expect(container.statistics.failures).to be == 1
235238
end
236239

240+
it "can stop the container from child_exit" do
241+
stop_policy = Class.new(Async::Container::Policy) do
242+
def initialize
243+
@stop_count = 0
244+
end
245+
246+
attr :stop_count
247+
248+
def child_exit(container, child, status, name:, key:, **options)
249+
unless container.stopping?
250+
@stop_count += 1
251+
container.stop
252+
end
253+
end
254+
end.new
255+
256+
container = Async::Container.best_container_class.new(policy: stop_policy)
257+
258+
3.times do |i|
259+
container.spawn(name: "worker-#{i}") do |instance|
260+
instance.ready!
261+
exit(1)
262+
end
263+
end
264+
265+
container.wait
266+
267+
expect(stop_policy.stop_count).to be == 1
268+
expect(container).not.to be(:running?)
269+
end
270+
237271
it "invokes callbacks for multiple children" do
238272
container = Async::Container.best_container_class.new(policy: tracking_policy)
239273

test/async/container/threaded.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
require "async/container/threaded"
77
require "async/container/a_container"
8+
require "sus/fixtures/async/scheduler_context"
89

910
describe Async::Container::Threaded do
11+
include Sus::Fixtures::Async::SchedulerContext
12+
1013
it_behaves_like Async::Container::AContainer
1114

1215
it "should not be multiprocess" do

0 commit comments

Comments
 (0)