/
test_integration.rb
521 lines (403 loc) · 13.2 KB
/
test_integration.rb
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
require_relative "helper"
require "puma/cli"
require "puma/control_cli"
require "open3"
# These don't run on travis because they're too fragile
# TODO: Remove over-utilization of @instance variables
# TODO: remove stdout logging, get everything out of my rainbow dots
class TestIntegration < Minitest::Test
HOST = '127.0.0.1'
def setup
@state_path = "test/test_puma.state"
@bind_path = "test/test_server.sock"
@control_path = "test/test_control.sock"
@token = "xxyyzz"
@server = nil
@wait, @ready = IO.pipe
@events = Puma::Events.strings
@events.on_booted { @ready << "!" }
end
def teardown
File.unlink @state_path rescue nil
File.unlink @bind_path rescue nil
File.unlink @control_path rescue nil
@wait.close
@ready.close
if @server
Process.kill "INT", @server.pid
begin
Process.wait @server.pid
rescue Errno::ECHILD
end
@server.close
end
end
def server_cmd(argv)
@tcp_port = next_port
base = "#{Gem.ruby} -Ilib bin/puma"
base = "bundle exec #{base}" if defined?(Bundler)
"#{base} -b tcp://127.0.0.1:#{@tcp_port} #{argv}"
end
def server(argv)
@server = IO.popen(server_cmd(argv), "r")
wait_for_server_to_boot(@server)
@server
end
def start_forked_server(argv)
servercmd = server_cmd(argv)
pid = fork do
exec servercmd
end
sleep 5
pid
end
def stop_forked_server(pid)
Process.kill(:TERM, pid)
sleep 1
Process.wait2(pid)
end
def restart_server_and_listen(argv)
server(argv)
connection = connect
initial_reply = read_body(connection)
restart_server(@server, connection)
[initial_reply, read_body(connect)]
end
def signal(which)
Process.kill which, @server.pid
end
def wait_booted
@wait.sysread 1
end
# reuses an existing connection to make sure that works
def restart_server(server, connection)
signal :USR2
connection.write "GET / HTTP/1.1\r\n\r\n" # trigger it to start by sending a new request
wait_for_server_to_boot(server)
end
def connect
s = TCPSocket.new "localhost", @tcp_port
s << "GET / HTTP/1.1\r\n\r\n"
true until s.gets == "\r\n"
s
end
def wait_for_server_to_boot(server)
true while server.gets !~ /Ctrl-C/ # wait for server to say it booted
end
def read_body(connection)
Timeout.timeout(10) do
loop do
response = connection.readpartial(1024)
body = response.split("\r\n\r\n", 2).last
return body if body && !body.empty?
sleep 0.01
end
end
end
def test_stop_via_pumactl
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
conf = Puma::Configuration.new do |c|
c.quiet
c.state_path @state_path
c.bind "unix://#{@bind_path}"
c.activate_control_app "unix://#{@control_path}", :auth_token => @token
c.rackup "test/rackup/hello.ru"
end
l = Puma::Launcher.new conf, :events => @events
t = Thread.new do
Thread.current.abort_on_exception = true
l.run
end
wait_booted
s = UNIXSocket.new @bind_path
s << "GET / HTTP/1.0\r\n\r\n"
assert_equal "Hello World", read_body(s)
sout = StringIO.new
ccli = Puma::ControlCLI.new %W!-S #{@state_path} stop!, sout
ccli.run
assert_kind_of Thread, t.join, "server didn't stop"
end
def test_phased_restart_via_pumactl
skip NO_FORK_MSG unless HAS_FORK
# hello-stuck-ci uses sleep 10, hello-stuck uses sleep 60
rackup = "test/rackup/hello-stuck#{ ENV['CI'] ? '-ci' : '' }.ru"
conf = Puma::Configuration.new do |c|
c.quiet
c.state_path @state_path
c.bind "unix://#{@bind_path}"
c.activate_control_app "unix://#{@control_path}", :auth_token => @token
c.workers 2
c.worker_shutdown_timeout 1
c.rackup rackup
end
l = Puma::Launcher.new conf, :events => @events
t = Thread.new do
Thread.current.abort_on_exception = true
l.run
end
wait_booted
s = UNIXSocket.new @bind_path
s << "GET / HTTP/1.0\r\n\r\n"
sout = StringIO.new
# Phased restart
ccli = Puma::ControlCLI.new ["-S", @state_path, "phased-restart"], sout
ccli.run
done = false
until done
@events.stdout.rewind
log = @events.stdout.readlines.join("")
if log =~ /- Worker \d \(pid: \d+\) booted, phase: 1/
assert_match(/TERM sent/, log)
assert_match(/- Worker \d \(pid: \d+\) booted, phase: 1/, log)
done = true
end
end
# Stop
ccli = Puma::ControlCLI.new ["-S", @state_path, "stop"], sout
ccli.run
assert_kind_of Thread, t.join, "server didn't stop"
assert File.exist? @bind_path
end
def test_kill_unknown_via_pumactl
skip_on :jruby
# we run ls to get a 'safe' pid to pass off as puma in cli stop
# do not want to accidentally kill a valid other process
io = IO.popen(windows? ? "dir" : "ls")
safe_pid = io.pid
Process.wait safe_pid
sout = StringIO.new
e = assert_raises SystemExit do
ccli = Puma::ControlCLI.new %W!-p #{safe_pid} stop!, sout
ccli.run
end
sout.rewind
# windows bad URI(is not URI?)
assert_match(/No pid '\d+' found|bad URI\(is not URI\?\)/, sout.readlines.join(""))
assert_equal(1, e.status)
end
def test_restart_closes_keepalive_sockets
skip_unless_signal_exist? :USR2
_, new_reply = restart_server_and_listen("-q test/rackup/hello.ru")
assert_equal "Hello World", new_reply
end
def test_restart_closes_keepalive_sockets_workers
skip NO_FORK_MSG unless HAS_FORK
_, new_reply = restart_server_and_listen("-q -w 2 test/rackup/hello.ru")
assert_equal "Hello World", new_reply
end
def test_sigterm_closes_listeners_on_forked_servers
skip NO_FORK_MSG unless HAS_FORK
pid = start_forked_server("-w 2 -q test/rackup/1second.ru")
threads = []
initial_reply = nil
next_replies = []
condition_variable = ConditionVariable.new
mutex = Mutex.new
threads << Thread.new do
s = connect
mutex.synchronize { condition_variable.broadcast }
initial_reply = read_body(s)
end
threads << Thread.new do
mutex.synchronize {
condition_variable.wait(mutex, 1)
Process.kill("SIGTERM", pid)
}
end
10.times.each do |i|
threads << Thread.new do
mutex.synchronize { condition_variable.wait(mutex, 1.5) }
begin
s = connect
read_body(s)
next_replies << :success
rescue Errno::ECONNRESET
# connection was accepted but then closed
# client would see an empty response
next_replies << :connection_reset
rescue Errno::ECONNREFUSED
# connection was was never accepted
# it can therefore be re-tried before the
# client receives an empty response
next_replies << :connection_refused
end
end
end
threads.map(&:join)
assert_equal "Hello World", initial_reply
assert_includes next_replies, :connection_refused
refute_includes next_replies, :connection_reset
end
# It does not share environments between multiple generations, which would break Dotenv
def test_restart_restores_environment
# jruby has a bug where setting `nil` into the ENV or `delete` do not change the
# next workers ENV
skip_on :jruby
skip_unless_signal_exist? :USR2
initial_reply, new_reply = restart_server_and_listen("-q test/rackup/hello-env.ru")
assert_includes initial_reply, "Hello RAND"
assert_includes new_reply, "Hello RAND"
refute_equal initial_reply, new_reply
end
def test_term_signal_exit_code_in_single_mode
skip NO_FORK_MSG unless HAS_FORK
pid = start_forked_server("test/rackup/hello.ru")
_, status = stop_forked_server(pid)
assert_equal 15, status
end
def test_term_signal_exit_code_in_clustered_mode
skip NO_FORK_MSG unless HAS_FORK
pid = start_forked_server("-w 2 test/rackup/hello.ru")
_, status = stop_forked_server(pid)
assert_equal 15, status
end
def test_term_signal_suppress_in_single_mode
skip NO_FORK_MSG unless HAS_FORK
pid = start_forked_server("-C test/config/suppress_exception.rb test/rackup/hello.ru")
_, status = stop_forked_server(pid)
assert_equal 0, status
end
def test_term_signal_suppress_in_clustered_mode
skip NO_FORK_MSG unless HAS_FORK
server("-w 2 -C test/config/suppress_exception.rb test/rackup/hello.ru")
Process.kill(:TERM, @server.pid)
begin
Process.wait @server.pid
rescue Errno::ECHILD
end
status = $?.exitstatus
assert_equal 0, status
@server = nil # prevent `#teardown` from killing already killed server
end
def test_not_accepts_new_connections_after_term_signal
skip_on :jruby, :windows
server('test/rackup/10seconds.ru')
_stdin, curl_stdout, _stderr, curl_wait_thread = Open3.popen3("curl 127.0.0.1:#{@tcp_port}")
sleep 1 # ensure curl send a request
Process.kill(:TERM, @server.pid)
true while @server.gets !~ /Gracefully stopping/ # wait for server to begin graceful shutdown
# Invoke a request which must be rejected
_stdin, _stdout, rejected_curl_stderr, rejected_curl_wait_thread = Open3.popen3("curl 127.0.0.1:#{@tcp_port}")
assert nil != Process.getpgid(@server.pid) # ensure server is still running
assert nil != Process.getpgid(rejected_curl_wait_thread[:pid]) # ensure first curl invokation still in progress
curl_wait_thread.join
rejected_curl_wait_thread.join
assert_match(/Hello World/, curl_stdout.read)
assert_match(/Connection refused/, rejected_curl_stderr.read)
Process.wait(@server.pid)
@server = nil # prevent `#teardown` from killing already killed server
end
def test_no_zombie_children
skip NO_FORK_MSG unless HAS_FORK
worker_pids = []
server = server("-w 2 test/rackup/hello.ru")
# Get the PIDs of the child workers.
while worker_pids.size < 2
next unless line = server.gets.match(/pid: (\d+)/)
worker_pids << line.captures.first.to_i
end
# Signal the workers to terminate, and wait for them to die.
worker_pids.each { |pid| Process.kill :TERM, pid }
sleep 2
# Should return nil if Puma has correctly cleaned up
assert_nil Process.waitpid(-1, Process::WNOHANG)
end
def test_worker_spawn_external_term
worker_respawn { |l, old_pids|
old_pids.each { |p| Process.kill :TERM, p }
}
end
def test_worker_phased_restart
worker_respawn { |l, old_pids| l.phased_restart }
end
private
def worker_respawn
skip NO_FORK_MSG unless HAS_FORK
port = UniquePort.call
workers_booted = 0
conf = Puma::Configuration.new do |c|
c.bind "tcp://#{HOST}:#{port}"
c.threads 1, 1
c.workers 2
c.worker_shutdown_timeout 2
c.app TestApps::SLEEP
c.after_worker_fork { |idx| workers_booted += 1 }
end
# start Puma via launcher
thr, launcher, _e = run_launcher conf
# make sure two workers have booted
time = 0
until workers_booted >= 2 || time >= 10
sleep 2
time += 2
end
cluster = launcher.instance_variable_get :@runner
http0 = Net::HTTP.new HOST, port
http1 = Net::HTTP.new HOST, port
body0 = nil
body1 = nil
worker0 = Thread.new do
begin
req0 = Net::HTTP::Get.new "/sleep35", {}
http0.start.request(req0) { |rep0| body0 = rep0.body }
rescue
end
end
worker1 = Thread.new do
begin
req1 = Net::HTTP::Get.new "/sleep40", {}
http1.start.request(req1) { |rep1| body1 = rep1.body }
rescue
end
end
old_pids = cluster.instance_variable_get(:@workers).map(&:pid)
start_time = Time.now.to_f
# below should 'cancel' the phase 0 workers, either via phased_restart or
# externally SIGTERM'ing them
yield launcher, old_pids
# make sure four workers have booted
time = 0
until workers_booted >= 4 || time >= 45
sleep 2
time += 2
end
new_pids = cluster.instance_variable_get(:@workers).map(&:pid)
# should be empty if all old workers removed
old_waited = old_pids.map { |pid|
begin
Process.wait(pid, Process::WNOHANG)
pid
rescue Errno::ECHILD
nil # child is already terminated
end
}.compact
Thread.kill worker0
Thread.kill worker1
launcher.stop
assert_kind_of Thread, thr.join, "server didn't stop"
refute_equal 'Slept 35', body0
refute_equal 'Slept 40', body1
# Since 35 is the shorter of the two requests, server should restart
# and cancel both requests
assert_operator (Time.now.to_f - start_time).round(2), :<, 35
msg = "old_pids #{old_pids.inspect} new_pids #{new_pids.inspect} old_waited #{old_waited.inspect}"
assert_equal 2, new_pids.length, msg
assert_equal 2, old_pids.length, msg
assert_empty new_pids & old_pids, "#{msg}\nBoth workers should be replaced with new"
assert_empty old_waited, msg
end
def run_launcher(conf)
# below for future PR
#@wait, @ready = IO.pipe
# @ios_to_close << @wait << @ready
#@events = Puma::Events.strings
#@events.on_booted { @ready << "!" }
launcher = Puma::Launcher.new conf, :events => @events
thr = Thread.new do
launcher.run
end
# wait for boot from #@events.on_booted
@wait.sysread 1
[thr, launcher, @events]
end
end