features/support/env.rb in httpthumbnailer-0.0.2 vs features/support/env.rb in httpthumbnailer-0.0.3
- old
+ new
@@ -14,10 +14,11 @@
require 'timeout'
require 'httpclient'
require 'httpthumbnailer/multipart_response'
require "open3"
require "thread"
+require 'RMagick'
def gem_dir
Pathname.new(__FILE__).dirname + '..' + '..'
end
@@ -31,55 +32,61 @@
def script(file)
gem_dir + 'bin' + file
end
-def server_get(uri)
- HTTPClient.new.get_content("http://localhost:3100#{uri}")
+def part_no(part)
+ case part
+ when 'first' then 0
+ when 'second' then 1
+ when 'third' then 2
+ else fail "add more parts?"
+ end
end
-def server_request(method, uri, query = nil, body = nil)
- HTTPClient.new.request(method, "http://localhost:3100#{uri}", query, body)
+
+def get(url)
+ HTTPClient.new.get_content(url)
end
-def server_start
- File.exist?("/tmp/httpthumbnailer.pid") and server_stop
+def start_server(cmd, pid_file, log_file, test_url)
+ stop_server(pid_file)
+
fork do
- Daemon.daemonize("/tmp/httpthumbnailer.pid", support_dir + 'server.log')
- exec("bundle exec #{script('httpthumbnailer')} -p 3100")
+ Daemon.daemonize(pid_file, log_file)
+ exec(cmd)
end
+ Process.wait
+ ppid = Process.pid
+ at_exit do
+ stop_server(pid_file) if Process.pid == ppid
+ end
+
Timeout.timeout(10) do
begin
- server_get '/'
+ get test_url
rescue Errno::ECONNREFUSED
sleep 0.1
retry
end
end
end
-def server_stop
- File.open("/tmp/httpthumbnailer.pid") do |pidf|
- pid = pidf.read
+def stop_server(pid_file)
+ pid_file = Pathname.new(pid_file)
+ return unless pid_file.exist?
- Timeout.timeout(10) do
- begin
- loop do
- ret = Process.kill("TERM", pid.strip.to_i)
- sleep 0.1
- end
- rescue Errno::ESRCH
+ pid = pid_file.read.strip.to_i
+
+ Timeout.timeout(20) do
+ begin
+ loop do
+ Process.kill("TERM", pid)
+ sleep 0.1
end
+ rescue Errno::ESRCH
+ pid_file.unlink
end
- end
-end
-
-def part_no(part)
- case part
- when 'first' then 0
- when 'second' then 1
- when 'third' then 2
- else fail "add more parts?"
end
end