about summary refs log tree commit homepage
path: root/test/test_server.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2013-10-22 22:46:25 +0000
committerEric Wong <e@80x24.org>2013-10-22 23:08:46 +0000
commit28163a34ceff8334b2af4c5c30ec1e642efb9be1 (patch)
tree6219d20af729502cd20b942fb8700bffea741f16 /test/test_server.rb
parentb79ba40ff559f9676c7bad530eeab6b7eb6a91f1 (diff)
downloadyahns-28163a34ceff8334b2af4c5c30ec1e642efb9be1.tar.gz
These can be used to disconnect/reconnect to databases
and other external connections.  These are named to match
the documentation of pthread_atfork(3)
Diffstat (limited to 'test/test_server.rb')
-rw-r--r--test/test_server.rb46
1 files changed, 46 insertions, 0 deletions
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