Sha256: 6df36dc97e89b46723585efab22f6e133688e6e0d551e3a83c7654e54f1431eb

Contents?: true

Size: 1.45 KB

Versions: 40

Compression:

Stored size: 1.45 KB

Contents

require 'time'
require 'timeout'
require 'socket'

# Borrowed from Webrat and Selenium client, watches for TCP port
# liveness of the spawned server.
# @private
class TCPSocket
  def self.wait_for_service(options)
    verbose_wait until listening_service?(options)
  end

  def self.wait_for_service_termination(options)
    verbose_wait while listening_service?(options)
  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

  def self.verbose_wait
    # Removed the puts call so as not to clutter up test output.
    sleep 2
  end

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

    until listening_service?(options)
      verbose_wait

      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)
      verbose_wait

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

Version data entries

40 entries across 30 versions & 3 rubygems

Version Path
riak-client-2.6.0 lib/riak/util/tcp_socket_extensions.rb
riak-client-2.5.0 lib/riak/util/tcp_socket_extensions.rb
riak-client-2.3.0 lib/riak/util/tcp_socket_extensions.rb
riak-client-2.2.2 lib/riak/util/tcp_socket_extensions.rb
riak-client-2.2.1 lib/riak/util/tcp_socket_extensions.rb
riak-client-noenc-1.0.0 lib/riak/util/tcp_socket_extensions.rb
riak-client-2.2.0 lib/riak/util/tcp_socket_extensions.rb
riak-client-2.2.0.pre1 lib/riak/util/tcp_socket_extensions.rb
riak-client-2.1.0 lib/riak/util/tcp_socket_extensions.rb
riak-client-1.4.5 lib/riak/util/tcp_socket_extensions.rb
riak-client-2.0.0 lib/riak/util/tcp_socket_extensions.rb
riak-client-2.0.0.rc2 lib/riak/util/tcp_socket_extensions.rb
riak-client-2.0.0.rc1 lib/riak/util/tcp_socket_extensions.rb
riak-client-1.4.4.1 lib/riak/util/tcp_socket_extensions.rb
riak-client-1.4.4 lib/riak/util/tcp_socket_extensions.rb
riak-client-1.4.3 lib/riak/util/tcp_socket_extensions.rb
riak-client-1.4.2 lib/riak/util/tcp_socket_extensions.rb
riak-client-1.4.1 lib/riak/util/tcp_socket_extensions.rb
riak-client-1.4.0 lib/riak/util/tcp_socket_extensions.rb
riak-client-1.2.0 lib/riak/util/tcp_socket_extensions.rb