class Shokkenki::Consumer::Stubber::Server

Attributes

app[R]
error[R]
host[R]
port[R]
server_thread[R]

Public Class Methods

new(attributes) click to toggle source
# File lib/shokkenki/consumer/stubber/server.rb, line 15
def initialize attributes
  @app = attributes[:app]
  @server_thread = nil # suppress warnings
  @host = attributes[:host]
  @port = attributes[:port]
end

Public Instance Methods

assert_ok!() click to toggle source
# File lib/shokkenki/consumer/stubber/server.rb, line 26
def assert_ok!
  raise ServerApplicationError.new(error) if error
end
is_app?(response) click to toggle source
# File lib/shokkenki/consumer/stubber/server.rb, line 41
def is_app? response
  response.body.to_s == @app.object_id.to_s
end
ok?(response) click to toggle source
# File lib/shokkenki/consumer/stubber/server.rb, line 45
def ok? response
  response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end
responsive?() click to toggle source
# File lib/shokkenki/consumer/stubber/server.rb, line 30
def responsive?
  return false if server_thread && server_thread.join(0)
  res = Net::HTTP.start(host, @port) do |http|
    http.get @app.identify_path
  end
  assert_ok!
  ok?(res) && is_app?(res)
rescue SystemCallError
  return false
end
run(app, host, port) click to toggle source
# File lib/shokkenki/consumer/stubber/server.rb, line 49
def run app, host, port
  Rack::Handler::WEBrick.run(app, :Host => host, :Port => port, :AccessLog => [], :Logger => WEBrick::Log::new(nil, 0))
end
shutdown() click to toggle source
# File lib/shokkenki/consumer/stubber/server.rb, line 63
def shutdown
  server_thread.kill
end
start() click to toggle source
# File lib/shokkenki/consumer/stubber/server.rb, line 53
def start
  @server_thread = Thread.new do
    run @app, @host, @port
  end

  Timeout.timeout(10) { @server_thread.join(0.1) until responsive? }
rescue Timeout::Error
  raise "Rack application #{@app} timed out during boot"
end