Class: EchSpec::Spec::WithSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/echspec/spec.rb

Instance Method Summary collapse

Constructor Details

#initializeWithSocket

Returns a new instance of WithSocket.



74
75
76
# File 'lib/echspec/spec.rb', line 74

def initialize
  @stack = Log::MessageStack.new
end

Instance Method Details

#message_stackObject



78
79
80
# File 'lib/echspec/spec.rb', line 78

def message_stack
  @stack.marshal
end

#with_socket(hostname, port) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/echspec/spec.rb', line 61

def with_socket(hostname, port)
  socket = TCPSocket.new(hostname, port)
  yield(socket)
rescue Timeout::Error
  Err.new("#{hostname}:#{port} connection timeout", message_stack)
rescue Errno::ECONNREFUSED
  Err.new("#{hostname}:#{port} connection refused", message_stack)
rescue Error::BeforeTargetSituationError => e
  Err.new(e.message, message_stack)
ensure
  socket&.close
end