Sha256: 4d04c0f11c2de2e70d44c7a1f709009fdbaacdd409f2f47e1e324c7efe59d837

Contents?: true

Size: 1.74 KB

Versions: 24

Compression:

Stored size: 1.74 KB

Contents

require 'socket'
require 'net/http'

# Adapted from code by Jon Leighton
# https://github.com/jonleighton/focused_controller/blob/ec7ccf1/test/acceptance/app_test.rb

class DummyApp

  def initialize(environment)
    raise ArgumentError, "Environment must be development or production" unless ["development", "production"].include?(environment.to_s)
    @environment = environment
  end

  attr_reader :environment

  def url
    "http://#{localhost}:#{port}"
  end

  def get(path)
    Net::HTTP.get(URI(url + path))
  end

  def within_app(&block)
    Dir.chdir(root, &block)
  end

  def start_server
    within_app do
      IO.popen("bundle exec rails s -e #{@environment} -p #{port} 2>&1") do |out|
        start   = Time.now
        started = false
        output  = ""
        timeout = 60.0

        while !started && !out.eof? && Time.now - start <= timeout
          output << read_output(out)
          sleep 0.1

          begin
            TCPSocket.new(localhost, port)
          rescue Errno::ECONNREFUSED
          else
            started = true
          end
        end

        raise "Server failed to start:\n#{output}" unless started

        yield

        Process.kill("KILL", out.pid)
        File.delete("tmp/pids/server.pid")
      end
    end
  end

  private

  def root
    File.expand_path("../../dummy", __FILE__)
  end

  def localhost
    "127.0.0.1"
  end

  def port
    @port ||= begin
      server = TCPServer.new(localhost, 0)
      server.addr[1]
    ensure
      server.close if server
    end
  end

  def read_output(stream)
    read = IO.select([stream], [], [stream], 0.1)
    output = ""
    loop { output << stream.read_nonblock(1024) } if read
    output
  rescue Errno::EAGAIN, Errno::EWOULDBLOCK, EOFError
    output
  end
end

Version data entries

24 entries across 24 versions & 6 rubygems

Version Path
draper-4.0.2 spec/support/dummy_app.rb
draper-4.0.1 spec/support/dummy_app.rb
draper-4.0.0 spec/support/dummy_app.rb
draper-3.1.0 spec/support/dummy_app.rb
draper-3.0.1 spec/support/dummy_app.rb
draper-3.0.0 spec/support/dummy_app.rb
draper-3.0.0.pre1 spec/support/dummy_app.rb
draper_new-3.0.0 spec/support/dummy_app.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/draper-2.1.0/spec/support/dummy_app.rb
draper-2.1.0 spec/support/dummy_app.rb
draper-2.0.0 spec/support/dummy_app.rb
draper-1.4.0 spec/support/dummy_app.rb
draper-1.3.1 spec/support/dummy_app.rb
strong_presenter-0.2.2 spec/support/dummy_app.rb
draper-1.3.0 spec/support/dummy_app.rb
strong_presenter-0.2.1 spec/support/dummy_app.rb
strong_presenter-0.2.0 spec/support/dummy_app.rb
strong_presenter-0.1.0 spec/support/dummy_app.rb
draper-1.2.1 spec/support/dummy_app.rb
locale_setter-0.4.0 spec/support/dummy_app.rb