From c60ff81f1ed1efe7f03fc31cf4c566264f3e314a Mon Sep 17 00:00:00 2001 From: evanweaver Date: Sun, 21 Oct 2007 02:54:53 +0000 Subject: [PATCH] minor test cleanups git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@710 19e92222-5c0b-0410-8929-a290d50e31e9 --- lib/mongrel.rb | 31 +++++++----- test/testhelp.rb | 112 ++++++++++++++++++++++--------------------- tools/trickletest.rb | 4 +- 3 files changed, 79 insertions(+), 68 deletions(-) diff --git a/lib/mongrel.rb b/lib/mongrel.rb index 5b5de59b..2e4a9882 100644 --- a/lib/mongrel.rb +++ b/lib/mongrel.rb @@ -266,9 +266,9 @@ module Mongrel update_request_progress(remain, total) end - rescue Object - STDERR.puts "#{Time.now}: Error reading HTTP body: #$!" - STDERR.puts $!.backtrace.join("\n") + rescue Object => e + STDERR.puts "#{Time.now}: Error reading HTTP body: #{e.inspect}" + STDERR.puts e.backtrace.join("\n") # any errors means we should delete the file, including if the file is dumped @socket.close rescue nil @body.delete if @body.class == Tempfile @@ -657,16 +657,23 @@ module Mongrel end rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF client.close rescue nil - rescue HttpParserError - STDERR.puts "#{Time.now}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #$!" + rescue HttpParserError => e + STDERR.puts "#{Time.now}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}" STDERR.puts "#{Time.now}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n" rescue Errno::EMFILE reap_dead_workers('too many files') - rescue Object - STDERR.puts "#{Time.now}: Error: #$!" - STDERR.puts $!.backtrace.join("\n") + rescue Object => e + STDERR.puts "#{Time.now}: Read error: #{e.inspect}" + STDERR.puts e.backtrace.join("\n") ensure - client.close rescue nil + begin + client.close + rescue IOError + # Already closed + rescue Object => e + STDERR.puts "#{Time.now}: Client error: #{e.inspect}" + STDERR.puts e.backtrace.join("\n") + end request.body.delete if request and request.body.class == Tempfile end end @@ -763,9 +770,9 @@ module Mongrel rescue Errno::ECONNABORTED # client closed the socket even before accept client.close rescue nil - rescue Object => exc - STDERR.puts "** Unhandled exception #{exc.inspect}." - STDERR.puts $!.backtrace.join("\n") + rescue Object => e + STDERR.puts "#{Time.now}: Unhandled listen loop exception #{e.inspect}." + STDERR.puts e.backtrace.join("\n") end end graceful_shutdown diff --git a/test/testhelp.rb b/test/testhelp.rb index 40e6a79b..5d89c11c 100644 --- a/test/testhelp.rb +++ b/test/testhelp.rb @@ -4,63 +4,67 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -HERE = File.dirname(__FILE__) -%w(lib ext bin test).each do |dir| - $LOAD_PATH.unshift "#{HERE}/../#{dir}" -end +unless defined? $require_once -require 'rubygems' -require 'test/unit' -require 'net/http' -require 'timeout' -require 'cgi/session' -require 'fileutils' -require 'benchmark' -require 'digest/sha1' -require 'uri' -require 'stringio' + $require_once = true -require 'mongrel' -require 'mongrel/stats' - -if ENV['DEBUG'] - require 'ruby-debug' - Debugger.start -end - -def redirect_test_io - orig_err = STDERR.dup - orig_out = STDOUT.dup - STDERR.reopen("test_stderr.log") - STDOUT.reopen("test_stdout.log") - - begin - yield - ensure - STDERR.reopen(orig_err) - STDOUT.reopen(orig_out) + HERE = File.dirname(__FILE__) + %w(lib ext bin test).each do |dir| + $LOAD_PATH.unshift "#{HERE}/../#{dir}" end -end - - -# Either takes a string to do a get request against, or a tuple of [URI, HTTP] where -# HTTP is some kind of Net::HTTP request object (POST, HEAD, etc.) -def hit(uris) - results = [] - uris.each do |u| - res = nil - - if u.kind_of? String - res = Net::HTTP.get(URI.parse(u)) - else - url = URI.parse(u[0]) - res = Net::HTTP.new(url.host, url.port).start {|h| h.request(u[1]) } + + require 'rubygems' + require 'test/unit' + require 'net/http' + require 'timeout' + require 'cgi/session' + require 'fileutils' + require 'benchmark' + require 'digest/sha1' + require 'uri' + require 'stringio' + + require 'mongrel' + require 'mongrel/stats' + + if ENV['DEBUG'] + require 'ruby-debug' + Debugger.start + end + + def redirect_test_io + orig_err = STDERR.dup + orig_out = STDOUT.dup + STDERR.reopen("test_stderr.log") + STDOUT.reopen("test_stdout.log") + + begin + yield + ensure + STDERR.reopen(orig_err) + STDOUT.reopen(orig_out) end - - assert res != nil, "Didn't get a response: #{u}" - results << res end - - return results + + # Either takes a string to do a get request against, or a tuple of [URI, HTTP] where + # HTTP is some kind of Net::HTTP request object (POST, HEAD, etc.) + def hit(uris) + results = [] + uris.each do |u| + res = nil + + if u.kind_of? String + res = Net::HTTP.get(URI.parse(u)) + else + url = URI.parse(u[0]) + res = Net::HTTP.new(url.host, url.port).start {|h| h.request(u[1]) } + end + + assert res != nil, "Didn't get a response: #{u}" + results << res + end + + return results + end + end - diff --git a/tools/trickletest.rb b/tools/trickletest.rb index 52fcfacc..e19ed718 100644 --- a/tools/trickletest.rb +++ b/tools/trickletest.rb @@ -18,8 +18,8 @@ def do_test(st, chunk) break end end - rescue Object - STDERR.puts "ERROR: #$!" + rescue Object => e + STDERR.puts "ERROR: #{e}" ensure s.close end