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
| | # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require_relative 'server_helper'
class TestBin < Testcase
parallelize_me!
include ServerHelper
alias teardown server_helper_teardown
def setup
server_helper_setup
@cmd = %W(ruby -I lib bin/yahns)
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 = IO.pipe
pid = fork do
if inherit
@cmd << { @srv.fileno => @srv }
ENV["YAHNS_FD"] = @srv.fileno.to_s
else
@srv = TCPServer.new(ENV["TEST_HOST"] || "127.0.0.1", 0)
@srv.close_on_exec = true # needed for 1.9.3
end
host, port = @srv.addr[3], @srv.addr[1]
listen = ENV["YAHNS_TEST_LISTEN"] = "#{host}:#{port}"
addr[1].write(listen)
addr[1].close
addr[0].close
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)
Dir.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
ruby = "#!#{`which ruby`}"
File.open(exe, "w") { |y|
lines = File.readlines("bin/yahns")
lines[0] = ruby
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
rescue => e
Yahns::Log.exception(Logger.new($stderr), "test", e)
raise
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
|