about summary refs log tree commit homepage
path: root/test/test_bin.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-10-18 10:28:18 +0000
committerEric Wong <normalperson@yhbt.net>2013-10-18 10:28:18 +0000
commitab067831e707b191d6dfdcd01de1f1d85fc90d05 (patch)
treeb02861eb1521fb325ee4e1d91e1a194ca73e7a9e /test/test_bin.rb
downloadyahns-ab067831e707b191d6dfdcd01de1f1d85fc90d05.tar.gz
Diffstat (limited to 'test/test_bin.rb')
-rw-r--r--test/test_bin.rb98
1 files changed, 98 insertions, 0 deletions
diff --git a/test/test_bin.rb b/test/test_bin.rb
new file mode 100644
index 0000000..4a47a93
--- /dev/null
+++ b/test/test_bin.rb
@@ -0,0 +1,98 @@
+# 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))
+    pid = tmpfile(%w(daemon .pid))
+    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)
+      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
+end