about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-10-18 17:41:47 +0000
committerEric Wong <normalperson@yhbt.net>2013-10-18 17:42:37 +0000
commita3221be0cd53c4b5ca6546f1d06ab7ca94feff23 (patch)
treef94962d787b8a0cd02cb9da407a249fec93ef1ba
parentee6b1150abc9ee0c429ac88b839214e7933c4f92 (diff)
downloadyahns-a3221be0cd53c4b5ca6546f1d06ab7ca94feff23.tar.gz
This should help prevent us from running out of FDs prematurely
as our test suite becomes more multi-threaded.  We can also use
plain TCPSocket in tests where we inherit the file descriptor
from the parent (w/o accept).
-rw-r--r--test/server_helper.rb3
-rw-r--r--test/test_bin.rb1
-rw-r--r--test/test_client_expire.rb2
-rw-r--r--test/test_output_buffering.rb7
-rw-r--r--test/test_rack.rb2
-rw-r--r--test/test_server.rb15
6 files changed, 19 insertions, 11 deletions
diff --git a/test/server_helper.rb b/test/server_helper.rb
index 78d2f94..97a175a 100644
--- a/test/server_helper.rb
+++ b/test/server_helper.rb
@@ -11,7 +11,7 @@ module ServerHelper
     err.rewind
     lines = err.readlines.delete_if { |l| l =~ /INFO/ }
     assert lines.empty?, lines.join("\n")
-    err.close
+    err.close! if err == @err
   end
 
   def poke_until_dead(pid)
@@ -40,6 +40,7 @@ module ServerHelper
     raise
   end
 
+  # only use for newly bound sockets
   def get_tcp_client(host, port, tries = 500)
     begin
       c = TCPSocket.new(host, port)
diff --git a/test/test_bin.rb b/test/test_bin.rb
index 4a47a93..aab4616 100644
--- a/test/test_bin.rb
+++ b/test/test_bin.rb
@@ -33,7 +33,6 @@ class TestBin < Testcase
     @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
diff --git a/test/test_client_expire.rb b/test/test_client_expire.rb
index 525c2e8..269d487 100644
--- a/test/test_client_expire.rb
+++ b/test/test_client_expire.rb
@@ -66,12 +66,14 @@ class TestClientExpire < Testcase
     abe = tmpfile(%w(abe .err))
     ab_res = `ab -c #{nr} -n 10000 -k http://#{host}:#{port}/ 2>#{abe.path}`
     assert $?.success?, $?.inspect << abe.read
+    abe.close!
     assert_match(/Complete requests:\s+10000\n/, ab_res)
 
     [ f, s ].each do |io|
       assert_raises(Errno::EPIPE,Errno::ECONNRESET) do
         req.each_byte { |b| io.write(b.chr) }
       end
+      io.close
     end
   rescue => e
     Yahns::Log.exception(Logger.new($stderr), "test", e)
diff --git a/test/test_output_buffering.rb b/test/test_output_buffering.rb
index 6fe22ba..1c6612e 100644
--- a/test/test_output_buffering.rb
+++ b/test/test_output_buffering.rb
@@ -84,6 +84,7 @@ class TestOutputBuffering < Testcase
       md5in[0].close
       md5out[1].close
       assert_equal(NR * RAND.size, IO.copy_stream(c, md5in[1]))
+      c.shutdown
       c.close
       md5in[1].close
       _, status = Timeout.timeout(10) { Process.waitpid2(md5pid) }
@@ -261,7 +262,7 @@ class TestOutputBuffering < Testcase
     end
     threads = []
     threads << Thread.new do
-      c = get_tcp_client(host, port)
+      c = TCPSocket.new(host, port)
       c.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
       sleep(5) # wait for timeout
       assert_operator c.nread, :>, 0
@@ -269,7 +270,7 @@ class TestOutputBuffering < Testcase
     end
 
     threads << Thread.new do
-      c = get_tcp_client(host, port)
+      c = TCPSocket.new(host, port)
       c.write("GET /bh HTTP/1.1\r\nHost: example.com\r\n\r\n")
       sleep(5) # wait for timeout
       assert_operator c.nread, :>, 0
@@ -281,7 +282,9 @@ class TestOutputBuffering < Testcase
     msg = File.readlines(apperr.path)
     msg = msg.grep(/timeout on :wait_writable after 3s$/)
     assert_equal 2, msg.size
+    threads.each { |t| t.value.close }
   ensure
+    apperr.close! if apperr
     quit_wait(pid)
   end
 end if `which curl 2>/dev/null`.strip =~ /curl/ &&
diff --git a/test/test_rack.rb b/test/test_rack.rb
index bd0d5b5..3f7a261 100644
--- a/test/test_rack.rb
+++ b/test/test_rack.rb
@@ -13,6 +13,7 @@ class TestRack < Testcase
     assert_kind_of Rack::Lobster, GTL.synchronize { rapp.app_after_fork }
     defaults = rapp.app_defaults
     assert_kind_of Hash, defaults
+    tmp.close!
   end
 
   def test_rack_preload
@@ -22,5 +23,6 @@ class TestRack < Testcase
     assert_kind_of Rack::Lobster, rapp.instance_variable_get(:@app)
     defaults = rapp.app_defaults
     assert_kind_of Hash, defaults
+    tmp.close!
   end
 end
diff --git a/test/test_server.rb b/test/test_server.rb
index 5c86268..958b4f0 100644
--- a/test/test_server.rb
+++ b/test/test_server.rb
@@ -24,7 +24,7 @@ class TestServer < Testcase
       srv.start.join
     end
     run_client(host, port) { |res| assert_equal "HI", res.body }
-    c = get_tcp_client(host, port)
+    c = TCPSocket.new(host, port)
 
     # test pipelining
     r = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
@@ -81,7 +81,7 @@ class TestServer < Testcase
       ENV["YAHNS_FD"] = @srv.fileno.to_s
       srv.start.join
     end
-    c = get_tcp_client(host, port)
+    c = TCPSocket.new(host, port)
     buf = "PUT / HTTP/1.0\r\nContent-Length: 2\r\n\r\nHI"
     c.write(buf)
     IO.select([c], nil, nil, 5)
@@ -93,7 +93,7 @@ class TestServer < Testcase
 
     # pipelined oneshot
     buf = "PUT / HTTP/1.1\r\nContent-Length: 2\r\n\r\nHI"
-    c = get_tcp_client(host, port)
+    c = TCPSocket.new(host, port)
     c.write(buf + buf)
     buf = ""
     Timeout.timeout(10) do
@@ -163,7 +163,7 @@ class TestServer < Testcase
       ENV["YAHNS_FD"] = @srv.fileno.to_s
       srv.start.join
     end
-    c = get_tcp_client(host, port)
+    c = TCPSocket.new(host, port)
     buf = "PUT / HTTP/1.0\r\nTrailer:xbt\r\nTransfer-Encoding: chunked\r\n\r\n"
     c.write(buf)
     xbt = btype.to_s
@@ -238,8 +238,8 @@ class TestServer < Testcase
       srv.start.join
     end
     bpipe[0].close
-    a = get_tcp_client(host, port)
-    b = get_tcp_client(host, port)
+    a = TCPSocket.new(host, port)
+    b = TCPSocket.new(host, port)
     a.write("GET /sleep HTTP/1.0\r\n\r\n")
     r = IO.select([a], nil, nil, 4)
     assert r, "nothing ready"
@@ -262,6 +262,7 @@ class TestServer < Testcase
 
     # do things still work?
     run_client(host, port) { |res| assert_equal "HI", res.body }
+    a.close
   ensure
     quit_wait(pid)
   end
@@ -340,7 +341,7 @@ class TestServer < Testcase
   end
 
   def run_client(host, port)
-    c = get_tcp_client(host, port)
+    c = TCPSocket.new(host, port)
     Net::HTTP.start(host, port) do |http|
       res = http.request(Net::HTTP::Get.new("/"))
       assert_equal 200, res.code.to_i