-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcontroller.rb
More file actions
333 lines (241 loc) · 7.43 KB
/
Copy pathcontroller.rb
File metadata and controls
333 lines (241 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2018-2026, by Samuel Williams.
require "async/container/controller"
require "async/container/controllers"
require "async/container/notify/server"
require "async/container/notify/socket"
describe Async::Container::Controller do
let(:controller) {subject.new}
with "#to_s" do
it "can generate string representation" do
expect(controller.to_s).to be == "Async::Container::Controller stopped"
end
end
with "#graceful_stop" do
def read_graceful_stop(value)
command = [
"bundle", "exec", "ruby", "-Ilib", "-e",
"require 'async/container/controller'; puts Async::Container::Controller.new(notify: nil).graceful_stop.inspect"
]
output = IO.popen({"ASYNC_CONTAINER_GRACEFUL_STOP" => value}, command, &:read)
expect($?.success?).to be == true
return output
end
it "uses the configured graceful timeout by default" do
output = read_graceful_stop("0.001")
expect(output).to be == "0.001\n"
end
it "can disable graceful stop by default" do
output = read_graceful_stop("false")
expect(output).to be == "false\n"
end
end
with "#reload" do
it "can reuse keyed child" do
input, output = IO.pipe
controller.instance_variable_set(:@output, output)
def controller.setup(container)
container.spawn(key: "test") do |instance|
instance.ready!
@output.write(".")
@output.flush
sleep(0.2)
end
container.spawn do |instance|
instance.ready!
sleep(0.1)
@output.write(",")
@output.flush
end
end
controller.start
expect(controller.state_string).to be == "running"
expect(input.read(2)).to be == ".,"
controller.reload
expect(input.read(1)).to be == ","
controller.wait
end
it "spawns a newly configured keyed child on reload" do
keys = ["a"]
controller.define_singleton_method(:setup) do |container|
container.reload do
keys.each do |key|
container.spawn(key: key) do |instance|
instance.ready!
sleep
end
end
end
end
controller.start
expect(controller.container["a"]).not.to be_nil
expect(controller.container["b"]).to be_nil
# The configuration now includes an additional keyed child:
keys << "b"
controller.reload
expect(controller.container["a"]).not.to be_nil
expect(controller.container["b"]).not.to be_nil
ensure
controller.stop(false)
end
it "stops a keyed child that is no longer configured on reload" do
keys = ["a", "b"]
controller.define_singleton_method(:setup) do |container|
container.reload do
keys.each do |key|
container.spawn(key: key) do |instance|
instance.ready!
sleep
end
end
end
end
controller.start
expect(controller.container["a"]).not.to be_nil
expect(controller.container["b"]).not.to be_nil
# The configuration no longer includes "b", so reload should stop it:
keys.delete("b")
controller.reload
expect(controller.container["a"]).not.to be_nil
expect(controller.container["b"]).to be_nil
ensure
controller.stop(false)
end
end
with "notify" do
before do
@notify_server = Async::Container::Notify::Server.open
@notify_client = Async::Container::Notify::Socket.new(@notify_server.path)
@notify = @notify_server.bind
end
after do
@notify&.close
end
let(:controller) {subject.new(notify: @notify_client)}
it "sends status with ready notification on reload" do
def controller.setup(container)
container.spawn do |instance|
instance.ready!
sleep(0.1)
end
end
controller.start
# Drain the start ready message:
@notify.wait_until_ready
controller.reload
# Capture messages until we find the reload ready notification:
while message = @notify.receive
break if message[:ready]
end
expect(message).to have_keys(
ready: be == true,
status: be =~ /Running/
)
end
end
with "#start" do
it "can start up a container" do
expect(controller).to receive(:setup)
controller.start
expect(controller).to be(:running?)
expect(controller.container).not.to be_nil
controller.stop
expect(controller).not.to be(:running?)
expect(controller.container).to be_nil
end
it "doesn't restart a container if it's already running" do
expect(controller).to receive(:setup)
expect(controller.start).to be == true
expect(controller).to be(:running?)
container = controller.container
# Starting again is idempotent: it returns false and the container is not replaced.
expect(controller.start).to be == false
expect(controller.container).to be_equal(container)
ensure
controller.stop
end
it "can spawn a reactor" do
def controller.setup(container)
container.async do |task|
task.sleep 0.001
end
end
controller.start
statistics = controller.container.statistics
expect(statistics.spawns).to be == 1
controller.stop
end
it "propagates exceptions" do
def controller.setup(container)
raise "Boom!"
end
expect do
controller.run
end.to raise_exception(Async::Container::SetupError)
end
end
with "graceful controller" do
include_context Async::Container::AController, "graceful"
it "has graceful shutdown" do
expect(input.gets).to be == "Ready...\n"
wait_until_ready
Process.kill(:INT, process_id)
expect(input.gets).to be == "Exiting...\n"
end
it "sends status with ready notification on start" do
expect(input.gets).to be == "Ready...\n"
# Capture messages until we receive the ready notification:
while message = @notify.receive
break if message[:ready]
end
expect(message).to have_keys(
ready: be == true,
status: be =~ /Running/
)
end
end
with "bad controller" do
include_context Async::Container::AController, "bad"
it "fails to start" do
expect(input.gets).to be == "Ready...\n"
Process.kill(:INT, process_id)
# It was killed:
expect(input.gets).to be_nil
end
end
with "signals" do
include_context Async::Container::AController, "dots"
it "restarts children when receiving SIGHUP" do
expect(input.read(1)).to be == "."
wait_until_ready
Process.kill(:HUP, process_id)
# The ordering between the old child writing "I" and the new child writing "." is timing-dependent (blue-green restart starts the new container before stopping the old one). Accept either order.
expect(input.read(2)).to (be == "I.").or(be == ".I")
end
it "exits gracefully when receiving SIGINT" do
expect(input.read(1)).to be == "."
wait_until_ready
Process.kill(:INT, process_id)
expect(input.read).to be == "I"
end
it "exits gracefully when receiving SIGTERM" do
expect(input.read(1)).to be == "."
wait_until_ready
Process.kill(:TERM, process_id)
# SIGTERM now behaves like SIGINT (graceful)
expect(input.read).to be == "I"
end
end
with "working directory" do
let(:controller_path) {Async::Container::Controllers.path_for("working_directory")}
it "can change working directory" do
pipe = IO.pipe
pid = Process.spawn("bundle", "exec", controller_path, out: pipe.last)
pipe.last.close
expect(pipe.first.gets(chomp: true)).to be == "/"
ensure
Process.kill(:INT, pid) if pid
end
end
end