2004-11-16 17:07:15 +03:00
|
|
|
require 'timeout'
|
|
|
|
|
2013-11-03 15:41:53 +04:00
|
|
|
module TestXMLRPC
|
2004-11-16 17:07:15 +03:00
|
|
|
module WEBrick_Testing
|
2014-11-09 12:36:33 +03:00
|
|
|
def start_server(logger, config={})
|
2010-01-26 02:12:50 +03:00
|
|
|
raise "already started" if defined?(@__server) && @__server
|
2004-12-21 19:23:33 +03:00
|
|
|
@__started = false
|
|
|
|
|
2013-11-04 10:27:54 +04:00
|
|
|
@__server = WEBrick::HTTPServer.new(
|
|
|
|
{
|
|
|
|
:BindAddress => "localhost",
|
2014-11-09 12:36:33 +03:00
|
|
|
:Logger => logger,
|
2013-11-04 10:27:54 +04:00
|
|
|
:AccessLog => [],
|
|
|
|
}.update(config))
|
|
|
|
yield @__server
|
|
|
|
@__started = true
|
|
|
|
|
2013-11-04 17:21:53 +04:00
|
|
|
addr = @__server.listeners.first.connect_address
|
|
|
|
|
2007-12-15 17:51:48 +03:00
|
|
|
@__server_thread = Thread.new {
|
2011-02-13 03:56:40 +03:00
|
|
|
begin
|
|
|
|
@__server.start
|
|
|
|
rescue IOError => e
|
|
|
|
assert_match(/closed/, e.message)
|
|
|
|
ensure
|
|
|
|
@__started = false
|
|
|
|
end
|
2004-12-21 19:23:33 +03:00
|
|
|
}
|
2013-11-04 17:21:53 +04:00
|
|
|
|
|
|
|
addr
|
2004-11-16 17:07:15 +03:00
|
|
|
end
|
|
|
|
|
2014-11-09 12:07:41 +03:00
|
|
|
def with_server(config, servlet)
|
2014-11-09 12:36:33 +03:00
|
|
|
log = []
|
|
|
|
logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
|
|
|
|
addr = start_server(logger, config) {|w|
|
|
|
|
servlet = servlet.call(w) if servlet.respond_to? :call
|
|
|
|
w.mount('/RPC2', servlet)
|
|
|
|
}
|
2014-11-09 12:07:41 +03:00
|
|
|
client_thread = Thread.new {
|
|
|
|
begin
|
|
|
|
yield addr
|
|
|
|
ensure
|
|
|
|
@__server.shutdown
|
|
|
|
end
|
|
|
|
}
|
|
|
|
server_thread = Thread.new {
|
|
|
|
@__server_thread.join
|
|
|
|
@__server = nil
|
2014-11-09 12:36:33 +03:00
|
|
|
assert_equal([], log)
|
2014-11-09 12:07:41 +03:00
|
|
|
}
|
|
|
|
assert_join_threads([client_thread, server_thread])
|
2004-11-16 17:07:15 +03:00
|
|
|
end
|
|
|
|
end
|
2013-11-03 15:41:53 +04:00
|
|
|
end
|