Sha256: c25ec14fe56ab59aafafed1a39a14290d6f1bd282875cbb452ea2652292f8361

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'socket'

# Stolen from selenium and webrat.
class TCPSocket

  def self.wait_for_service_with_timeout(options)
    start_time = Time.now

    until listening_service?(options)

      if options[:timeout] && (Time.now > start_time + options[:timeout])
        raise SocketError.new("Socket did not open within #{options[:timeout]} seconds")
      end
    end
  end

  def self.wait_for_service_termination_with_timeout(options)
    start_time = Time.now

    while listening_service?(options)
      if options[:timeout] && (Time.now > start_time + options[:timeout])
        raise SocketError.new("Socket did not terminate within #{options[:timeout]} seconds")
      end
    end
  end

  def self.listening_service?(options)
    Timeout::timeout(options[:timeout] || 20) do
      begin
        socket = TCPSocket.new(options[:host], options[:port])
        socket.close unless socket.nil?
        true
      rescue Errno::ECONNREFUSED,
             Errno::EBADF           # Windows
        false
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tcpsocket-wait-1.0.0 lib/tcpsocket-wait.rb