about summary refs log tree commit homepage
path: root/test
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2013-10-30 21:59:37 +0000
committerEric Wong <normalperson@yhbt.net>2013-10-31 05:04:36 +0000
commit602432714cd436b6f255e6ba6bc6fffbf5eca051 (patch)
tree079520490f4c8cc89320658f2e83b597ec376091 /test
parent5544418af3fb810ef90f33bcf88a434cd0349dc8 (diff)
downloadyahns-602432714cd436b6f255e6ba6bc6fffbf5eca051.tar.gz
It's usually given as a block, so Ruby won't care about arity there.
Users will get the worker ID number as the first arg, making it
possible to isolate some things to certain processes (e.g. for A/B
testing).
Diffstat (limited to 'test')
-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"