Sha256: 1c44a3c7075a98f0f5cfe35c440280a718fcddad4142b8896bb1df50f4654637

Contents?: true

Size: 1.91 KB

Versions: 11

Compression:

Stored size: 1.91 KB

Contents

require 'timeout'
require 'socket'

module Sauce
  module Utilities
    def silence_stream(stream)
      old_stream = stream.dup
      stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
      stream.sync = true
      yield
    ensure
      stream.reopen(old_stream)
    end

    def wait_for_server_on_port(port)
      while true
        begin
          Timeout::timeout(2) do
              socket = TCPSocket.new('127.0.0.1', port)
              socket.close unless socket.nil?
              return
          end
        rescue Errno::ECONNREFUSED, 
          Errno::EBADF,           # Windows
          Timeout::Error
        end
        sleep 2
      end
    end

    def with_selenium_rc
      ENV['LOCAL_SELENIUM'] = "true"
      STDERR.puts "Starting Selenium RC server on port 4444..."
      server = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", 4444)
      server.jar_file = File.expand_path(File.dirname(__FILE__) + "/../../support/selenium-server.jar")
      silence_stream(STDOUT) do
        server.start :background => true
        wait_for_server_on_port(4444)
      end
      STDERR.puts "Selenium RC running!"
      begin
        yield
      ensure
        server.stop
      end
    end

    def with_rails_server
      STDERR.puts "Starting Rails server on port 3001..."
      if File.exists?('script/server')
        server = IO.popen("ruby script/server RAILS_ENV=test --port 3001 --daemon")
      elsif File.exists?('script/rails')
        server = IO.popen("script/rails server -p 3001 -e test")
      end

      wait_for_server_on_port(3001)
      STDERR.puts "Rails server running!"
      begin
        yield
      ensure
        begin
          pid = IO.read(File.join('tmp', 'pids', 'server.pid')).to_i
          Process.kill("INT", pid)
        rescue
          STDERR.puts "Rails server could not be killed. Is the pid in #{File.join('tmp', 'pids', 'server.pid')}?"
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
sauce-0.12.10 lib/sauce/utilities.rb
sauce-0.12.9 lib/sauce/utilities.rb
sauce-0.12.8 lib/sauce/utilities.rb
sauce-0.12.7 lib/sauce/utilities.rb
sauce-0.12.6 lib/sauce/utilities.rb
sauce-0.12.5 lib/sauce/utilities.rb
sauce-0.12.4 lib/sauce/utilities.rb
sauce-0.12.3 lib/sauce/utilities.rb
sauce-0.12.2 lib/sauce/utilities.rb
sauce-0.12.1 lib/sauce/utilities.rb
sauce-0.12.0 lib/sauce/utilities.rb