1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/webrick/test_server.rb
drbrain 8c5c5a221f * lib/webrick/server.rb (module WEBrick::GenericServer): A server
will now continue only when a StandardError subclass is raised.  For
  other exception types the error will be logged at the fatal level and
  the server will safely stop.  Based on a patch by Alex Young.
  [ruby-trunk - Feature #6236]
* test/webrick/test_server.rb:  Test for new exception handling
  behavior.  Join the server thread instead of busy-waiting for it to
  shut down to remove race conditions.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35303 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-04-11 20:28:11 +00:00

93 lines
2.4 KiB
Ruby

require "test/unit"
require "tempfile"
require "webrick"
require_relative "utils"
class TestWEBrickServer < Test::Unit::TestCase
class Echo < WEBrick::GenericServer
def run(sock)
while line = sock.gets
sock << line
end
end
end
def test_server
TestWEBrick.start_server(Echo){|server, addr, port, log|
TCPSocket.open(addr, port){|sock|
sock.puts("foo"); assert_equal("foo\n", sock.gets, log.call)
sock.puts("bar"); assert_equal("bar\n", sock.gets, log.call)
sock.puts("baz"); assert_equal("baz\n", sock.gets, log.call)
sock.puts("qux"); assert_equal("qux\n", sock.gets, log.call)
}
}
end
def test_start_exception
stopped = 0
config = {
:StopCallback => Proc.new{ stopped += 1 },
}
e = assert_raises(Exception) do
TestWEBrick.start_server(Echo, config) { |server, addr, port, log|
listener = server.listeners.first
def listener.accept
raise Exception, 'fatal' # simulate ^C
end
true while server.status != :Running
TCPSocket.open(addr, port) { |sock| sock << "foo\n" }
sleep 0.1 until server.status == :Stop
}
end
assert_equal('fatal', e.message)
assert_equal(stopped, 1)
end
def test_callbacks
accepted = started = stopped = 0
config = {
:AcceptCallback => Proc.new{ accepted += 1 },
:StartCallback => Proc.new{ started += 1 },
:StopCallback => Proc.new{ stopped += 1 },
}
TestWEBrick.start_server(Echo, config){|server, addr, port, log|
true while server.status != :Running
assert_equal(started, 1, log.call)
assert_equal(stopped, 0, log.call)
assert_equal(accepted, 0, log.call)
TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
assert_equal(accepted, 3, log.call)
}
assert_equal(started, 1)
assert_equal(stopped, 1)
end
def test_daemon
begin
r, w = IO.pipe
pid1 = Process.fork{
r.close
WEBrick::Daemon.start
w.puts(Process.pid)
sleep 10
}
pid2 = r.gets.to_i
assert(Process.kill(:KILL, pid2))
assert_not_equal(pid1, pid2)
rescue NotImplementedError
# snip this test
ensure
Process.wait(pid1) if pid1
r.close
w.close
end
end
end