Class: TCPSocket

Inherits:
Object show all
Defined in:
lib/riak/util/tcp_socket_extensions.rb

Overview

Borrowed from Webrat and Selenium client, watches for TCP port liveness of the spawned server.

Class Method Summary collapse

Class Method Details

.listening_service?(options) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/riak/util/tcp_socket_extensions.rb', line 31

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

.verbose_waitObject



44
45
46
47
# File 'lib/riak/util/tcp_socket_extensions.rb', line 44

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

.wait_for_service(options) ⇒ Object



23
24
25
# File 'lib/riak/util/tcp_socket_extensions.rb', line 23

def self.wait_for_service(options)
  verbose_wait until listening_service?(options)
end

.wait_for_service_termination(options) ⇒ Object



27
28
29
# File 'lib/riak/util/tcp_socket_extensions.rb', line 27

def self.wait_for_service_termination(options)
  verbose_wait while listening_service?(options)
end

.wait_for_service_termination_with_timeout(options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/riak/util/tcp_socket_extensions.rb', line 61

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

.wait_for_service_with_timeout(options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/riak/util/tcp_socket_extensions.rb', line 49

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