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
| | # Copyright (C) 2013-2016 all contributors <yahns-public@yhbt.net>
# License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
# frozen_string_literal: true
require_relative 'server_helper'
class TestBin < Testcase
ENV["N"].to_i > 1 and parallelize_me!
include ServerHelper
alias teardown server_helper_teardown
def setup
server_helper_setup
@cmd = %W(#{RbConfig.ruby} -I lib bin/yahns)
end
def test_listen_fd3
return unless RUBY_VERSION.to_f > 2.3 # Fixed in ruby/trunk r51209, actually
@srv.setsockopt(:SOL_SOCKET, :SO_KEEPALIVE, 0)
host, port = @srv.addr[3], @srv.addr[1]
ru = tmpfile(%w(test_bin_daemon .ru))
ru.write("require 'rack/lobster'; run Rack::Lobster.new\n")
cmd = %W(#{RbConfig.ruby} -I lib bin/yahns-rackup
-E none -p #{port} -o #{host} #{ru.path})
pid = fork do # emulate a systemd environment
env = {
'LISTEN_PID' => $$.to_s,
'LISTEN_FDS' => '1',
}
exec env, *cmd, 3 => @srv, err: @err.path
end
Net::HTTP.start(host, port) do |http|
req = Net::HTTP::Get.new("/")
res = http.request(req)
assert_equal 200, res.code.to_i
assert_equal "keep-alive", res["Connection"]
end
assert @srv.getsockopt(:SOL_SOCKET, :SO_KEEPALIVE).bool,
'ensure the inheriting process applies TCP socket options'
ensure
if pid
Process.kill(:QUIT, pid)
_, status = Process.waitpid2(pid)
assert status.success?, status.inspect
end
ru.close! if ru
end
def test_bin_daemon_noworker_inherit
bin_daemon(false, true)
end
def test_bin_daemon_worker_inherit
bin_daemon(true, true)
end
def test_bin_daemon_noworker_bind
bin_daemon(false, false)
end
def test_bin_daemon_worker_bind
bin_daemon(true, false)
end
def bin_daemon(worker, inherit)
@srv.close unless inherit
@pid = tmpfile(%w(test_bin_daemon .pid))
@ru = tmpfile(%w(test_bin_daemon .ru))
@ru.write("require 'rack/lobster'; run Rack::Lobster.new\n")
cfg = tmpfile(%w(test_bin_daemon_conf .rb))
cfg.puts "pid '#{@pid.path}'"
cfg.puts "stderr_path '#{@err.path}'"
cfg.puts "worker_processes 1" if worker
cfg.puts "app(:rack, '#{@ru.path}', preload: false) do"
cfg.puts " listen ENV['YAHNS_TEST_LISTEN']"
cfg.puts "end"
@cmd.concat(%W(-D -c #{cfg.path}))
addr = cloexec_pipe
pid = fork do
opts = { close_others: true }
addr[0].close
if inherit
opts[@srv.fileno] = @srv
ENV["YAHNS_FD"] = @srv.fileno.to_s
else
# we must create the socket inside the child and tell the parent
# about it to avoid sharing
@srv = TCPServer.new(ENV["TEST_HOST"] || "127.0.0.1", 0)
end
@cmd << opts
host, port = @srv.addr[3], @srv.addr[1]
listen = ENV["YAHNS_TEST_LISTEN"] = "#{host}:#{port}"
addr[1].write(listen)
addr[1].close
# close/FD_CLOEXEC may be insufficient since the socket could be
# released asynchronously, leading to occasional test failures.
# Even with a synchronous FD_CLOEXEC, there's a chance of a race
# because the server does not bind right away.
unless inherit
@srv.shutdown
@srv.close
end
exec(*@cmd)
end
addr[1].close
listen = Timeout.timeout(10) { addr[0].read }
addr[0].close
host, port = listen.split(/:/, 2)
port = port.to_i
assert_operator port, :>, 0
unless inherit
# daemon_pipe guarantees socket will be usable after this:
Timeout.timeout(10) do # Ruby startup is slow!
_, status = Process.waitpid2(pid)
assert status.success?, status.inspect
end
end
Net::HTTP.start(host, port) do |http|
req = Net::HTTP::Get.new("/")
res = http.request(req)
assert_equal 200, res.code.to_i
assert_equal "keep-alive", res["Connection"]
end
rescue => e
warn "#{e.message} (#{e.class})"
e.backtrace.each { |l| warn "#{l}" }
raise
ensure
cfg.close! if cfg
pid = File.read(@pid.path)
pid = pid.to_i
assert_operator pid, :>, 0
Process.kill(:QUIT, pid)
if inherit
_, status = Timeout.timeout(10) { Process.waitpid2(pid) }
assert status.success?, status.inspect
else
poke_until_dead pid
end
@pid.close! if @pid
end
def test_usr2_preload_noworker; usr2(true, false); end
def test_usr2_preload_worker; usr2(true, true); end
def test_usr2_nopreload_worker; usr2(false, true); end
def test_usr2_nopreload_noworker; usr2(false, false); end
def usr2(preload, worker)
yahns_mktmpdir { |tmpdir| usr2_dir(tmpdir, preload, worker) }
end
def usr2_dir(tmpdir, preload, worker)
exe = "#{tmpdir}/yahns"
# need to fork here since tests are MT and the FD can leak out and go to
# other processes which fork (but do not exec), causing ETXTBUSY on
# Process.spawn
pid = fork do
File.open(exe, "w") { |y|
lines = File.readlines("bin/yahns")
lines[0] = "#!#{RbConfig.ruby}\n"
y.chmod(0755)
y.syswrite(lines.join)
}
end
_, status = Process.waitpid2(pid)
assert status.success?, status.inspect
@pid = tmpfile(%w(test_bin_daemon .pid))
host, port = @srv.addr[3], @srv.addr[1]
@ru = tmpfile(%w(test_bin_daemon .ru))
@ru.puts("use Rack::ContentLength")
@ru.puts("use Rack::ContentType, 'text/plain'")
@ru.puts("run lambda { |_| [ 200, {}, [ Process.pid.to_s ] ] }")
cfg = tmpfile(%w(test_bin_daemon_conf .rb))
cfg.puts "pid '#{@pid.path}'"
cfg.puts "stderr_path '#{@err.path}'"
cfg.puts "worker_processes 1" if worker
cfg.puts "app(:rack, '#{@ru.path}', preload: #{preload}) do"
cfg.puts " listen '#{host}:#{port}'"
cfg.puts "end"
env = {
"YAHNS_FD" => @srv.fileno.to_s,
"PATH" => "#{tmpdir}:#{ENV['PATH']}",
"RUBYLIB" => "#{Dir.pwd}/lib",
}
cmd = %W(#{exe} -D -c #{cfg.path})
cmd << { @srv => @srv, close_others: true }
pid = GTL.synchronize { Process.spawn(env, *cmd) }
res = Net::HTTP.start(host, port) { |h| h.get("/") }
assert_equal 200, res.code.to_i
orig = res.body
Process.kill(:USR2, pid)
newpid = pid
Timeout.timeout(10) do
begin
newpid = File.read(@pid.path)
rescue Errno::ENOENT
end while newpid.to_i == pid && sleep(0.01)
end
Process.kill(:QUIT, pid)
_, status = Timeout.timeout(10) { Process.waitpid2(pid) }
assert status.success?, status
res = Net::HTTP.start(host, port) { |h| h.get("/") }
assert_equal 200, res.code.to_i
second = res.body
refute_equal orig, second
newpid = newpid.to_i
assert_operator newpid, :>, 0
Process.kill(:HUP, newpid)
third = second
Timeout.timeout(10) do
begin
third = Net::HTTP.start(host, port) { |h| h.get("/") }.body
end while third == second && sleep(0.01)
end
if worker
Process.kill(0, newpid) # nothing should raise
else
poke_until_dead newpid
end
ensure
File.unlink(exe) if exe
cfg.close! if cfg
pid = File.read(@pid.path)
pid = pid.to_i
assert_operator pid, :>, 0
Process.kill(:QUIT, pid)
poke_until_dead pid
@pid.close!
end
end
|