-
Notifications
You must be signed in to change notification settings - Fork 39
/
command_line_spec.rb
102 lines (85 loc) · 2.29 KB
/
command_line_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
require 'spec_helper'
require 'net/http'
require 'childprocess'
require 'fileutils'
class MizunoRunner
MIN_TIMEOUT = 1
MAX_TIMEOUT = 30
WAIT_INTERVAL = 0.5
PIDFILE = "tmp/mizuno.pid"
attr_reader :process, :args
def start(*args)
@args = args.flatten
jruby_command = %w(jruby -Ilib/ -Ispec/support
-J-Djruby.compile.mode=OFF
-J-Djruby.launch.inproc=false)
mizuno_command = %w(bin/mizuno
--log tmp/mizuno.log
--host 127.0.0.1
--port 9201
--pidfile #{PIDFILE})
command = jruby_command + mizuno_command + @args
@process = ChildProcess.build(*command)
process.io.inherit!
process.start
self
end
def stop
return unless running?
Process.kill("KILL", process.pid)
wait
raise("Runner failed to stop.") if online?
FileUtils.rm(PIDFILE) if File.exists?(PIDFILE)
self
end
def wait(timeout = MAX_TIMEOUT)
timeout_at = current_time + timeout
while(current_time < timeout_at)
sleep(WAIT_INTERVAL)
return(true) unless running?
end
false
end
def running?
begin
Process.getpgid(process.pid)
true
rescue Errno::ESRCH
false
end
end
def online?
response = get
return(false) if response.nil?
status = response.code.to_i
(status >= 200) and (status <= 400)
end
def get(path = "/", timeout = MAX_TIMEOUT)
timeout_at = current_time + timeout
begin
Net::HTTP.start("127.0.0.1", 9201) do |http|
http.read_timeout = timeout
return(http.get(path))
end
rescue Errno::ECONNREFUSED, Errno::ECONNRESET
return if (current_time > timeout_at)
sleep(WAIT_INTERVAL)
retry
end
end
private
def pid
File.read(PIDFILE).to_i
end
def current_time
Time.now.to_f
end
end
describe "daemonization" do
let(:daemon) { MizunoRunner.new }
it "starts from a rackup file without daemonizing" do
daemon.start("spec/support/success_app.ru")
expect(daemon).to be_online
daemon.stop
end
end