-
Notifications
You must be signed in to change notification settings - Fork 87
/
daemon_spec.rb
184 lines (146 loc) · 4.6 KB
/
daemon_spec.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
describe ServerEngine::Daemon do
include_context 'test server and worker'
before do
@log_path = "tmp/multi-worker-test-#{SecureRandom.hex(10)}.log"
end
after do
FileUtils.rm_rf(@log_path)
end
it 'run and graceful stop by signal' do
skip "not supported signal base commands on Windows" if ServerEngine.windows?
dm = Daemon.new(TestServer, TestWorker, daemonize: true, pid_path: "tmp/pid", command_sender: "signal")
dm.main
wait_for_fork
test_state(:server_initialize).should == 1
begin
dm.stop(true)
wait_for_stop
test_state(:server_stop_graceful).should == 1
test_state(:worker_stop).should == 1
test_state(:server_after_run).should == 1
ensure
dm.stop(false) rescue nil
end
end
it 'signals' do
skip "not supported signal base commands on Windows" if ServerEngine.windows?
dm = Daemon.new(TestServer, TestWorker, daemonize: true, pid_path: "tmp/pid", command_sender: "signal")
dm.main
wait_for_fork
begin
dm.reload
wait_for_stop
test_state(:server_reload).should == 1
dm.restart(true)
wait_for_stop
test_state(:server_restart_graceful).should == 1
dm.restart(false)
wait_for_stop
test_state(:server_restart_immediate).should == 1
dm.dump
wait_for_stop
test_state(:server_dump).should == 1
dm.stop(false)
wait_for_stop
test_state(:server_stop_immediate).should == 1
ensure
dm.stop(true) rescue nil
end
end
it 'run and graceful stop by pipe' do
dm = Daemon.new(TestServer, TestWorker, daemonize: true, pid_path: "tmp/pid", windows_daemon_cmdline: windows_daemon_cmdline, command_sender: "pipe")
dm.main
wait_for_fork
test_state(:server_initialize).should == 1
begin
dm.stop(true)
wait_for_stop
test_state(:server_stop_graceful).should == 1
test_state(:worker_stop).should == 1
test_state(:server_after_run).should == 1
ensure
dm.stop(false) rescue nil
end
end
it 'recieve commands from pipe' do
dm = Daemon.new(TestServer, TestWorker, daemonize: true, pid_path: "tmp/pid", windows_daemon_cmdline: windows_daemon_cmdline, command_sender: "pipe")
dm.main
wait_for_fork
begin
dm.reload
wait_for_stop
test_state(:server_reload).should == 1
dm.restart(true)
wait_for_stop
test_state(:server_restart_graceful).should == 1
dm.restart(false)
wait_for_stop
test_state(:server_restart_immediate).should == 1
dm.stop(false)
wait_for_stop
test_state(:server_stop_immediate).should == 1
ensure
dm.stop(true) rescue nil
end
end
it 'exits with status 0 when it was stopped normally' do
skip "worker type process(fork) cannot be used in Windows" if ServerEngine.windows?
dm = Daemon.new(
TestServer,
TestWorker,
daemonize: false,
supervisor: false,
pid_path: "tmp/pid",
logger: ServerEngine::DaemonLogger.new(@log_path),
log_stdout: false,
log_stderr: false,
unrecoverable_exit_codes: [3,4,5],
)
exit_code = nil
t = Thread.new { exit_code = dm.main }
sleep 0.1 until dm.instance_eval{ @pid }
dm.stop(true)
t.join
exit_code.should == 0
end
it 'exits with status of workers if worker exits with status specified in unrecoverable_exit_codes, without supervisor' do
skip "worker type process(fork) cannot be used in Windows" if ServerEngine.windows?
dm = Daemon.new(
TestServer,
TestExitWorker,
daemonize: false,
supervisor: false,
worker_type: 'process',
pid_path: "tmp/pid",
logger: ServerEngine::DaemonLogger.new(@log_path),
log_stdout: false,
log_stderr: false,
unrecoverable_exit_codes: [3,4,5],
)
exit_code = nil
t = Thread.new { exit_code = dm.main }
sleep 0.1 until dm.instance_eval{ @pid }
t.join
exit_code.should == 5
end
it 'exits with status of workers if worker exits with status specified in unrecoverable_exit_codes, with supervisor' do
skip "worker type process(fork) cannot be used in Windows" if ServerEngine.windows?
dm = Daemon.new(
TestServer,
TestExitWorker,
daemonize: false,
supervisor: true,
worker_type: 'process',
pid_path: "tmp/pid",
logger: ServerEngine::DaemonLogger.new(@log_path),
log_stdout: false,
log_stderr: false,
unrecoverable_exit_codes: [3,4,5],
)
exit_code = nil
t = Thread.new { exit_code = dm.main }
sleep 0.1 until dm.instance_eval{ @pid }
t.join
exit_code.should == 5
end
end