forked from puma/puma
/
test_integration_pumactl.rb
145 lines (108 loc) · 3.96 KB
/
test_integration_pumactl.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
require_relative "helper"
require_relative "helpers/integration"
class TestIntegrationPumactl < TestIntegration
parallelize_me!
def setup
super
@state_path = "test/#{name}_puma.state"
@control_path = "test/#{name}_control.sock"
end
def teardown
super
refute File.exist?(@control_path), "Control path must be removed after stop"
ensure
[@state_path, @control_path].each { |p| File.unlink(p) rescue nil }
end
def test_stop_tcp
skip_on :jruby, :truffleruby # Undiagnose thread race. TODO fix
@control_tcp_port = UniquePort.call
cli_server "-q test/rackup/sleep.ru --control-url tcp://#{HOST}:#{@control_tcp_port} --control-token #{TOKEN} -S #{@state_path}"
cli_pumactl "stop"
_, status = Process.wait2(@pid)
assert_equal 0, status
@server = nil
end
def test_stop_unix
ctl_unix
end
def test_halt_unix
ctl_unix 'halt'
end
def ctl_unix(signal='stop')
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
stderr = Tempfile.new(%w(stderr .log))
cli_server "-q test/rackup/sleep.ru --control-url unix://#{@control_path} --control-token #{TOKEN} -S #{@state_path}",
config: "stdout_redirect nil, '#{stderr.path}'",
unix: true
cli_pumactl signal, unix: true
_, status = Process.wait2(@pid)
assert_equal 0, status
refute_match 'error', File.read(stderr.path)
@server = nil
end
def test_phased_restart_cluster
skip NO_FORK_MSG unless HAS_FORK
start = Time.now
cli_server "-q -w #{WORKERS} test/rackup/sleep.ru --control-url unix://#{@control_path} --control-token #{TOKEN} -S #{@state_path}", unix: true
s = UNIXSocket.new @bind_path
@ios_to_close << s
s << "GET /sleep1 HTTP/1.0\r\n\r\n"
# Get the PIDs of the phase 0 workers.
phase0_worker_pids = get_worker_pids 0
assert File.exist? @bind_path
# Phased restart
cli_pumactl "phased-restart", unix: true
# Get the PIDs of the phase 1 workers.
phase1_worker_pids = get_worker_pids 1
msg = "phase 0 pids #{phase0_worker_pids.inspect} phase 1 pids #{phase1_worker_pids.inspect}"
assert_equal WORKERS, phase0_worker_pids.length, msg
assert_equal WORKERS, phase1_worker_pids.length, msg
assert_empty phase0_worker_pids & phase1_worker_pids, "#{msg}\nBoth workers should be replaced with new"
assert File.exist?(@bind_path), "Bind path must exist after phased restart"
cli_pumactl "stop", unix: true
_, status = Process.wait2(@pid)
assert_equal 0, status
assert_operator Time.now - start, :<, 5
@server = nil
end
def test_prune_bundler_with_multiple_workers
skip NO_FORK_MSG unless HAS_FORK
cli_server "-q -C test/config/prune_bundler_with_multiple_workers.rb --control-url unix://#{@control_path} --control-token #{TOKEN} -S #{@state_path}", unix: true
s = UNIXSocket.new @bind_path
@ios_to_close << s
s << "GET / HTTP/1.0\r\n\r\n"
body = s.read
assert_match "200 OK", body
assert_match "embedded app", body
cli_pumactl "stop", unix: true
_, status = Process.wait2(@pid)
@server = nil
end
def test_kill_unknown
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
Puma::ControlCLI.new(%W!-p #{safe_pid} stop!, sout).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
private
def cli_pumactl(argv, unix: false)
if unix
pumactl = IO.popen("#{BASE} bin/pumactl -C unix://#{@control_path} -T #{TOKEN} #{argv}", "r")
else
pumactl = IO.popen("#{BASE} bin/pumactl -C tcp://#{HOST}:#{@control_tcp_port} -T #{TOKEN} #{argv}", "r")
end
@ios_to_close << pumactl
Process.wait pumactl.pid
pumactl
end
end