2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2005-01-07 06:05:22 -05:00
|
|
|
require "test/unit"
|
|
|
|
require "tempfile"
|
|
|
|
require "webrick"
|
2010-02-02 08:58:56 -05:00
|
|
|
require_relative "utils"
|
2005-01-07 06:05:22 -05:00
|
|
|
|
|
|
|
class TestWEBrickServer < Test::Unit::TestCase
|
|
|
|
class Echo < WEBrick::GenericServer
|
|
|
|
def run(sock)
|
|
|
|
while line = sock.gets
|
|
|
|
sock << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_server
|
2008-10-29 07:48:35 -04:00
|
|
|
TestWEBrick.start_server(Echo){|server, addr, port, log|
|
2005-01-07 06:05:22 -05:00
|
|
|
TCPSocket.open(addr, port){|sock|
|
2008-10-29 07:48:35 -04:00
|
|
|
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)
|
2005-01-07 06:05:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-04-11 16:28:11 -04:00
|
|
|
def test_start_exception
|
|
|
|
stopped = 0
|
|
|
|
|
2014-11-09 06:51:06 -05:00
|
|
|
log = []
|
2014-11-08 10:38:33 -05:00
|
|
|
logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
|
2012-04-11 16:28:11 -04:00
|
|
|
|
2015-06-01 22:18:44 -04:00
|
|
|
assert_raise(SignalException) do
|
2014-11-08 10:38:33 -05:00
|
|
|
listener = Object.new
|
|
|
|
def listener.to_io # IO.select invokes #to_io.
|
|
|
|
raise SignalException, 'SIGTERM' # simulate signal in main thread
|
|
|
|
end
|
2014-11-09 18:03:40 -05:00
|
|
|
def listener.shutdown
|
|
|
|
end
|
|
|
|
def listener.close
|
|
|
|
end
|
2012-04-11 16:28:11 -04:00
|
|
|
|
2014-11-08 10:38:33 -05:00
|
|
|
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
|
2012-04-11 16:28:11 -04:00
|
|
|
|
2014-11-08 10:38:33 -05:00
|
|
|
server.start
|
2012-04-11 16:28:11 -04:00
|
|
|
end
|
|
|
|
|
2014-11-09 07:16:38 -05:00
|
|
|
assert_equal(1, stopped)
|
2014-11-09 06:51:06 -05:00
|
|
|
assert_equal(1, log.length)
|
|
|
|
assert_match(/FATAL SignalException: SIGTERM/, log[0])
|
2012-04-11 16:28:11 -04:00
|
|
|
end
|
|
|
|
|
2005-01-07 06:05:22 -05:00
|
|
|
def test_callbacks
|
|
|
|
accepted = started = stopped = 0
|
|
|
|
config = {
|
|
|
|
:AcceptCallback => Proc.new{ accepted += 1 },
|
|
|
|
:StartCallback => Proc.new{ started += 1 },
|
|
|
|
:StopCallback => Proc.new{ stopped += 1 },
|
|
|
|
}
|
2008-10-29 07:48:35 -04:00
|
|
|
TestWEBrick.start_server(Echo, config){|server, addr, port, log|
|
2005-01-07 06:05:22 -05:00
|
|
|
true while server.status != :Running
|
2019-03-11 06:09:48 -04:00
|
|
|
sleep 1 if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? # server.status behaves unexpectedly with --jit-wait
|
2014-11-09 07:16:38 -05:00
|
|
|
assert_equal(1, started, log.call)
|
|
|
|
assert_equal(0, stopped, log.call)
|
|
|
|
assert_equal(0, accepted, log.call)
|
2005-01-07 06:05:22 -05:00
|
|
|
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 }
|
2014-11-09 07:16:38 -05:00
|
|
|
assert_equal(3, accepted, log.call)
|
2005-01-07 06:05:22 -05:00
|
|
|
}
|
2014-11-09 07:16:38 -05:00
|
|
|
assert_equal(1, started)
|
|
|
|
assert_equal(1, stopped)
|
2005-01-07 06:05:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_daemon
|
|
|
|
begin
|
2010-05-07 04:09:37 -04:00
|
|
|
r, w = IO.pipe
|
2010-05-07 11:07:11 -04:00
|
|
|
pid1 = Process.fork{
|
2010-05-07 04:09:37 -04:00
|
|
|
r.close
|
2005-01-07 06:05:22 -05:00
|
|
|
WEBrick::Daemon.start
|
2010-05-07 04:09:37 -04:00
|
|
|
w.puts(Process.pid)
|
2010-05-07 11:07:11 -04:00
|
|
|
sleep 10
|
2005-01-07 06:05:22 -05:00
|
|
|
}
|
2010-05-07 11:07:11 -04:00
|
|
|
pid2 = r.gets.to_i
|
|
|
|
assert(Process.kill(:KILL, pid2))
|
|
|
|
assert_not_equal(pid1, pid2)
|
2005-01-07 06:05:22 -05:00
|
|
|
rescue NotImplementedError
|
|
|
|
# snip this test
|
2010-05-07 04:09:37 -04:00
|
|
|
ensure
|
2010-05-07 11:07:11 -04:00
|
|
|
Process.wait(pid1) if pid1
|
2010-05-07 04:09:37 -04:00
|
|
|
r.close
|
|
|
|
w.close
|
2005-01-07 06:05:22 -05:00
|
|
|
end
|
|
|
|
end
|
2014-11-10 06:05:00 -05:00
|
|
|
|
2015-03-10 07:05:21 -04:00
|
|
|
def test_restart_after_shutdown
|
2014-11-10 06:05:00 -05:00
|
|
|
address = '127.0.0.1'
|
|
|
|
port = 0
|
|
|
|
log = []
|
|
|
|
config = {
|
|
|
|
:BindAddress => address,
|
|
|
|
:Port => port,
|
|
|
|
:Logger => WEBrick::Log.new(log, WEBrick::BasicLog::WARN),
|
|
|
|
}
|
|
|
|
server = Echo.new(config)
|
|
|
|
client_proc = lambda {|str|
|
|
|
|
begin
|
|
|
|
ret = server.listeners.first.connect_address.connect {|s|
|
|
|
|
s.write(str)
|
|
|
|
s.close_write
|
|
|
|
s.read
|
|
|
|
}
|
|
|
|
assert_equal(str, ret)
|
|
|
|
ensure
|
|
|
|
server.shutdown
|
|
|
|
end
|
|
|
|
}
|
|
|
|
server_thread = Thread.new { server.start }
|
|
|
|
client_thread = Thread.new { client_proc.call("a") }
|
|
|
|
assert_join_threads([client_thread, server_thread])
|
|
|
|
server.listen(address, port)
|
|
|
|
server_thread = Thread.new { server.start }
|
|
|
|
client_thread = Thread.new { client_proc.call("b") }
|
|
|
|
assert_join_threads([client_thread, server_thread])
|
|
|
|
assert_equal([], log)
|
|
|
|
end
|
2015-03-10 07:05:21 -04:00
|
|
|
|
|
|
|
def test_restart_after_stop
|
|
|
|
log = Object.new
|
|
|
|
class << log
|
|
|
|
include Test::Unit::Assertions
|
|
|
|
def <<(msg)
|
|
|
|
flunk "unexpected log: #{msg.inspect}"
|
|
|
|
end
|
|
|
|
end
|
2015-07-13 22:20:16 -04:00
|
|
|
client_thread = nil
|
|
|
|
wakeup = -> {client_thread.wakeup}
|
2015-03-10 07:05:21 -04:00
|
|
|
warn_flunk = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
|
|
|
|
server = WEBrick::HTTPServer.new(
|
2015-07-13 22:20:16 -04:00
|
|
|
:StartCallback => wakeup,
|
|
|
|
:StopCallback => wakeup,
|
2015-03-10 07:05:21 -04:00
|
|
|
:BindAddress => '0.0.0.0',
|
|
|
|
:Port => 0,
|
|
|
|
:Logger => warn_flunk)
|
|
|
|
2.times {
|
|
|
|
server_thread = Thread.start {
|
|
|
|
server.start
|
|
|
|
}
|
|
|
|
client_thread = Thread.start {
|
|
|
|
sleep 0.1 until server.status == :Running || !server_thread.status
|
|
|
|
server.stop
|
|
|
|
sleep 0.1 until server.status == :Stop || !server_thread.status
|
|
|
|
}
|
|
|
|
assert_join_threads([client_thread, server_thread])
|
|
|
|
}
|
|
|
|
end
|
2005-01-07 06:05:22 -05:00
|
|
|
end
|