Skip to content

Commit 4d5da78

Browse files
Add Async::Container::Generic#stopping?.
1 parent 6931c71 commit 4d5da78

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

fixtures/async/container/a_container.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
module Async
99
module Container
1010
AContainer = Sus::Shared("a container") do
11-
let(:container) {subject.new}
11+
let(:policy) {Async::Container::Policy::DEFAULT}
12+
let(:container) {subject.new(policy: policy)}
1213

1314
with "#run" do
1415
it "can run several instances concurrently" do
@@ -124,6 +125,21 @@ module Container
124125
end
125126

126127
with "#stop" do
128+
let(:policy) do
129+
Class.new(Async::Container::Policy) do
130+
def initialize
131+
@events = []
132+
end
133+
134+
attr :events
135+
136+
def child_exit(container, child, status, name:, key:, **options)
137+
# Capture the state of `stopping?`:
138+
@events << [:child_exit, container.stopping?]
139+
end
140+
end.new
141+
end
142+
127143
it "can gracefully stop the child process" do
128144
container.spawn do
129145
sleep(1)
@@ -132,13 +148,15 @@ module Container
132148
end
133149

134150
expect(container).to be(:running?)
151+
expect(container).not.to be(:stopping?)
135152

136153
# See above.
137154
sleep 0.001
138155

139156
container.stop(true)
140157

141158
expect(container).not.to be(:running?)
159+
expect(policy.events).to be == [[:child_exit, true]]
142160
end
143161

144162
it "can forcefully stop the child process" do
@@ -156,6 +174,7 @@ module Container
156174
container.stop(false)
157175

158176
expect(container).not.to be(:running?)
177+
expect(policy.events).to be == [[:child_exit, true]]
159178
end
160179

161180
it "can stop an uncooperative child process" do

lib/async/container/generic.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ def running?
9797
@group.running?
9898
end
9999

100+
# Whether the container is currently stopping.
101+
# @returns [Boolean]
102+
def stopping?
103+
!@running
104+
end
105+
100106
# Sleep until some state change occurs or the specified duration elapses.
101107
#
102108
# @parameter duration [Numeric] the maximum amount of time to sleep for.

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Add `Async::Container::Generic#stopping?` so that policies can more accurately track the state of the container.
6+
37
## v0.33.0
48

59
- Add `Policy#make_statistics` to allow policies to customize statistics initialization.

0 commit comments

Comments
 (0)