ruby-changes:36237
From: akr <ko1@a...>
Date: Sun, 9 Nov 2014 00:38:46 +0900 (JST)
Subject: [ruby-changes:36237] akr:r48318 (trunk): * test/webrick: Examine log and use assert_join_threads.
akr 2014-11-09 00:38:33 +0900 (Sun, 09 Nov 2014) New Revision: 48318 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48318 Log: * test/webrick: Examine log and use assert_join_threads. Modified files: trunk/ChangeLog trunk/test/webrick/test_httpauth.rb trunk/test/webrick/test_httpresponse.rb trunk/test/webrick/test_httpserver.rb trunk/test/webrick/test_server.rb trunk/test/webrick/utils.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 48317) +++ ChangeLog (revision 48318) @@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun Nov 9 00:37:44 2014 Tanaka Akira <akr@f...> + + * test/webrick: Examine log and use assert_join_threads. + Fri Nov 7 00:00:12 2014 Nobuyoshi Nakada <nobu@r...> * template/unicode_norm_gen.tmpl: expand kompatible_table so that Index: test/webrick/test_httpresponse.rb =================================================================== --- test/webrick/test_httpresponse.rb (revision 48317) +++ test/webrick/test_httpresponse.rb (revision 48318) @@ -63,6 +63,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpresponse.rb#L63 assert_equal 'hello', r.read } } + assert_equal 0, logger.messages.length end def test_send_body_string @@ -75,6 +76,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpresponse.rb#L76 assert_equal 'hello', r.read } + assert_equal 0, logger.messages.length end def test_send_body_string_io @@ -87,6 +89,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpresponse.rb#L89 assert_equal 'hello', r.read } + assert_equal 0, logger.messages.length end def test_send_body_io_chunked @@ -108,6 +111,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpresponse.rb#L111 assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read } } + assert_equal 0, logger.messages.length end def test_send_body_string_chunked @@ -123,6 +127,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpresponse.rb#L127 r.binmode assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read } + assert_equal 0, logger.messages.length end def test_send_body_string_io_chunked @@ -138,6 +143,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpresponse.rb#L143 r.binmode assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read } + assert_equal 0, logger.messages.length end end end Index: test/webrick/test_httpauth.rb =================================================================== --- test/webrick/test_httpauth.rb (revision 48317) +++ test/webrick/test_httpauth.rb (revision 48318) @@ -27,7 +27,7 @@ class TestWEBrickHTTPAuth < Test::Unit:: https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpauth.rb#L27 end def test_basic_auth2 - TestWEBrick.start_httpserver{|server, addr, port, log| + log = TestWEBrick.start_httpserver{|server, addr, port, log| realm = "WEBrick's realm" path = "/basic_auth2" @@ -61,6 +61,11 @@ class TestWEBrickHTTPAuth < Test::Unit:: https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpauth.rb#L61 http.request(g){|res| assert_not_equal("hoge", res.body, log.call)} } } + pat = /ERROR Basic WEBrick's realm: webrick: password unmatch\./ + assert_match(pat, log); log.sub!(pat, '') + pat = /ERROR WEBrick::HTTPStatus::Unauthorized/ + assert_match(pat, log); log.sub!(pat, '') + assert_not_match(/ERROR/, log) end def test_basic_auth3 @@ -92,7 +97,7 @@ class TestWEBrickHTTPAuth < Test::Unit:: https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpauth.rb#L97 )/x def test_digest_auth - TestWEBrick.start_httpserver{|server, addr, port, log| + log = TestWEBrick.start_httpserver{|server, addr, port, log| realm = "WEBrick's realm" path = "/digest_auth" @@ -143,6 +148,15 @@ class TestWEBrickHTTPAuth < Test::Unit:: https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpauth.rb#L148 end } } + pat = /ERROR Digest WEBrick's realm: no credentials in the request\./ + assert_match(pat, log); log.sub!(pat, '') + pat = /ERROR WEBrick::HTTPStatus::Unauthorized/ + assert_match(pat, log); log.sub!(pat, '') + pat = /ERROR Digest WEBrick's realm: webrick: digest unmatch\./ + assert_match(pat, log); log.sub!(pat, '') + pat = /ERROR WEBrick::HTTPStatus::Unauthorized/ + assert_match(pat, log); log.sub!(pat, '') + assert_not_match(/ERROR/, log) end private Index: test/webrick/utils.rb =================================================================== --- test/webrick/utils.rb (revision 48317) +++ test/webrick/utils.rb (revision 48318) @@ -26,6 +26,9 @@ module TestWEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/utils.rb#L26 RubyBin << " \"-I#{File.dirname(EnvUtil.rubybin)}/.ext/common\"" RubyBin << " \"-I#{File.dirname(EnvUtil.rubybin)}/.ext/#{RUBY_PLATFORM}\"" + include Test::Unit::Assertions + extend Test::Unit::Assertions + module_function def start_server(klass, config={}, &block) @@ -41,15 +44,16 @@ module TestWEBrick https://github.com/ruby/ruby/blob/trunk/test/webrick/utils.rb#L44 :Logger => WEBrick::Log.new(logger), :AccessLog => [[logger, ""]] }.update(config)) - begin - server_thread = server.start - addr = server.listeners[0].addr - block.yield([server, addr[3], addr[1], log]) - ensure - server.shutdown - - server_thread.join - end + server_thread = server.start + addr = server.listeners[0].addr + client_thread = Thread.new { + begin + block.yield([server, addr[3], addr[1], log]) + ensure + server.shutdown + end + } + assert_join_threads([client_thread, server_thread]) log_string end Index: test/webrick/test_httpserver.rb =================================================================== --- test/webrick/test_httpserver.rb (revision 48317) +++ test/webrick/test_httpserver.rb (revision 48318) @@ -4,9 +4,16 @@ require "webrick" https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpserver.rb#L4 require_relative "utils" class TestWEBrickHTTPServer < Test::Unit::TestCase + empty_log = Object.new + def empty_log.<<(str) + assert_equal('', str) + self + end + NoLog = WEBrick::Log.new(empty_log, WEBrick::BasicLog::WARN) + def test_mount httpd = WEBrick::HTTPServer.new( - :Logger => WEBrick::Log.new(TestWEBrick::NullWriter), + :Logger => NoLog, :DoNotListen=>true ) httpd.mount("/", :Root) @@ -75,7 +82,7 @@ class TestWEBrickHTTPServer < Test::Unit https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpserver.rb#L82 def httpd(addr, port, host, ali) config ={ - :Logger => WEBrick::Log.new(TestWEBrick::NullWriter), + :Logger => NoLog, :DoNotListen => true, :BindAddress => addr, :Port => port, @@ -229,7 +236,7 @@ class TestWEBrickHTTPServer < Test::Unit https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpserver.rb#L236 :BindAddress => addr, :Port => port, :DoNotListen => true, - :Logger => WEBrick::Log.new(TestWEBrick::NullWriter), + :Logger => NoLog, :AccessLog => [], :RequestCallback => Proc.new{|req, res| requested1 += 1 }, } Index: test/webrick/test_server.rb =================================================================== --- test/webrick/test_server.rb (revision 48317) +++ test/webrick/test_server.rb (revision 48318) @@ -25,27 +25,29 @@ class TestWEBrickServer < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/webrick/test_server.rb#L25 def test_start_exception stopped = 0 - config = { - :StopCallback => Proc.new{ stopped += 1 }, - } - assert_raises(SignalException) do - TestWEBrick.start_server(Echo, config) { |server, addr, port, log| - listener = server.listeners.first - - def listener.accept - raise SignalException, 'SIGTERM' # simulate signal in main thread - end + log = StringIO.new('') + logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN) - Thread.pass while server.status != :Running - - TCPSocket.open(addr, port) { |sock| sock << "foo\n" } + assert_raises(SignalException) do + listener = Object.new + def listener.to_io # IO.select invokes #to_io. + raise SignalException, 'SIGTERM' # simulate signal in main thread + end + + server = WEBrick::HTTPServer.new({ + :BindAddress => "127.0.0.1", :Port => 0, + :StopCallback => Proc.new{ stopped += 1 }, + :Logger => logger, + }) + server.listeners[0].close + server.listeners[0] = listener - Thread.pass until server.status == :Stop - } + server.start end assert_equal(stopped, 1) + assert_match(/FATAL SignalException: SIGTERM/, log.string) end def test_callbacks -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/