about summary refs log tree commit homepage
path: root/test/test_server.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_server.rb')
-rw-r--r--test/test_server.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/test/test_server.rb b/test/test_server.rb
index 6059e03..e6ddefc 100644
--- a/test/test_server.rb
+++ b/test/test_server.rb
@@ -440,6 +440,51 @@ class TestServer < Testcase
     quit_wait(pid)
   end
 
+  def test_mp_hooks_worker_nr
+    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 { |nr| puts "af.#{nr} #$$ worker is running" }
+          atfork_prepare { |nr| puts "af.#{nr} #$$ parent about to spawn" }
+          atfork_parent { |nr| puts "af.#{nr} #$$ this is probably not useful" }
+        end
+      }
+      stderr_path err.path
+      stdout_path out.path
+    end
+    pid = mkserver(cfg)
+    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.0 #{pid} parent about to spawn", lines.shift)
+
+    # child/parent ordering is not guaranteed
+    assert_equal 1,
+        lines.grep(/\Aaf\.0 #{pid} this is probably not useful\z/).size
+    assert_equal 1,
+        lines.grep(/\Aaf\.0 #{worker_pid} worker is running\z/).size
+  ensure
+    quit_wait(pid)
+  end
+
   def test_pidfile_usr2
     tmpdir = Dir.mktmpdir
     pidf = "#{tmpdir}/pid"