Skip to content

Commit

Permalink
Move timeout setting onto configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwynne committed Dec 18, 2009
1 parent 6a6a5c0 commit 76687b5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
6 changes: 5 additions & 1 deletion lib/cucumber/wire_support/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
module Cucumber
module WireSupport
class Configuration
attr_reader :host, :port, :timeout
attr_reader :host, :port

def initialize(wire_file)
params = YAML.load_file(wire_file)
@host = params['host']
@port = params['port']
@timeout = 3
end

def timeout(message = nil)
return @timeout
end
end
end
end
17 changes: 7 additions & 10 deletions lib/cucumber/wire_support/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,30 @@ module WireSupport
class Connection
include WireProtocol

attr_reader :default_timeout

def initialize(config)
@host, @port = config.host, config.port
@default_timeout = config.timeout
@config = config
end

def call_remote(request_handler, message, params)
packet = WirePacket.new(message, params)

begin
send_data_to_socket(packet.to_json, default_timeout)
response = fetch_data_from_socket(request_handler.timeout)
send_data_to_socket(packet.to_json)
response = fetch_data_from_socket(@config.timeout(message))
response.handle_with(request_handler)
rescue Timeout::Error
raise "Timed out calling server with message #{message}"
end
end

def exception(params)
WireException.new(params, @host, @port)
WireException.new(params, @config.host, @config.port)
end

private

def send_data_to_socket(data, timeout)
Timeout.timeout(timeout) { socket.puts(data) }
def send_data_to_socket(data)
Timeout.timeout(@config.timeout) { socket.puts(data) }
end

def fetch_data_from_socket(timeout)
Expand All @@ -41,7 +38,7 @@ def fetch_data_from_socket(timeout)
end

def socket
@socket ||= TCPSocket.new(@host, @port)
@socket ||= TCPSocket.new(@config.host, @config.port)
end
end
end
Expand Down
3 changes: 0 additions & 3 deletions lib/cucumber/wire_support/request_handler.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
module Cucumber
module WireSupport
class RequestHandler
attr_reader :timeout

def initialize(connection)
@connection = connection
@message = underscore(self.class.name.split('::').last)
@timeout = connection.default_timeout
end

def execute(request_params = nil)
Expand Down

0 comments on commit 76687b5

Please sign in to comment.