From 28163a34ceff8334b2af4c5c30ec1e642efb9be1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 22 Oct 2013 22:46:25 +0000 Subject: config: implement atfork handlers These can be used to disconnect/reconnect to databases and other external connections. These are named to match the documentation of pthread_atfork(3) --- test/test_server.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'test/test_server.rb') diff --git a/test/test_server.rb b/test/test_server.rb index 25f4b44..c277896 100644 --- a/test/test_server.rb +++ b/test/test_server.rb @@ -379,4 +379,50 @@ class TestServer < Testcase ensure quit_wait(pid) end + + def test_mp_hooks + err = @err + out = tmpfile(%w(mp_hooks .out)) + cfg = Yahns::Config.new + host, port = @srv.addr[3], @srv.addr[1] + cfg.instance_eval do + ru = lambda {|_|x="#$$";[200,{'Content-Length'=>x.size.to_s },[x]]} + GTL.synchronize { + app(:rack, ru) { + listen "#{host}:#{port}" + persistent_connections false + } + worker_processes(1) do + atfork_child { puts "af #$$ worker is running" } + atfork_prepare { puts "af #$$ parent about to spawn" } + atfork_parent { puts "af #$$ this is probably not useful" } + end + } + stderr_path err.path + stdout_path out.path + end + pid = fork do + ENV["YAHNS_FD"] = @srv.fileno.to_s + Yahns::Server.new(cfg).start.join + end + c = get_tcp_client(host, port) + c.write("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n") + buf = Timeout.timeout(10) { c.read } + c.close + head, body = buf.split(/\r\n\r\n/) + assert_match(/200 OK/, head) + assert_match(/\A\d+\z/, body) + worker_pid = body.to_i + lines = out.readlines.map!(&:chomp!) + out.close! + + assert_equal 3, lines.size + assert_equal("af #{pid} parent about to spawn", lines.shift) + + # child/parent ordering is not guaranteed + assert_equal 1, lines.grep(/\Aaf #{pid} this is probably not useful\z/).size + assert_equal 1, lines.grep(/\Aaf #{worker_pid} worker is running\z/).size + ensure + quit_wait(pid) + end end -- cgit v1.2.3-24-ge0c7