Sha256: 80fd01fc87a8fac2710cd3c617e852a4946dcc25ab55a818330479d455e0b177

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

require 'capybara'
require 'capybara/wait_until'

# This uses a separate process rather than a separate thread to run the server.
# Patron always times out when this is running in a thread in the same process.
#
# However, jruby doesn't support forking, and patron doesn't work on jruby...
# so we just leave Capybara::Server's definition of #boot.
class VCR::LocalhostServer < Capybara::Server

  def boot
    return self unless @app
    find_available_port
    Capybara.log "application has already booted" and return self if responsive?
    Capybara.log "booting Rack applicartion on port #{port}"

    pid = Process.fork do
      trap(:INT) { Rack::Handler::WEBrick.shutdown }
      Rack::Handler::WEBrick.run(Identify.new(@app), :Port => port, :AccessLog => [], :Logger => WEBrick::BasicLog.new(StringIO.new))
      exit # manually exit; otherwise this sub-process will re-run the specs that haven't run yet.
    end
    Capybara.log "checking if application has booted"

    Capybara::WaitUntil.timeout(10) do
      if responsive?
        Capybara.log("application has booted")
        true
      else
        sleep 0.5
        false
      end
    end

    at_exit do
      Process.kill('INT', pid)
      begin
        Process.wait(pid)
      rescue Errno::ECHILD
        # ignore this error...I think it means the child process has already exited.
      end
    end

    self
  rescue Timeout::Error
    Capybara.log "Rack application timed out during boot"
    exit
  end unless RUBY_PLATFORM =~ /java/

  STATIC_SERVERS = Hash.new do |h, k|
    h[k] = server = new(lambda { |env| [200, {}, StringIO.new(k)] })
    server.boot
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vcr-1.1.2 spec/support/vcr_localhost_server.rb
vcr-1.1.1 spec/support/vcr_localhost_server.rb