Sha256: 4847f4276aeb31fb1d302d17e4763ccbad560c350df39c37bdc6212f99166ef4

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 KB

Contents

require 'timeout'
require 'cucumber/wire_support/wire_protocol'

module Cucumber
  module WireSupport
    class Connection
      class ConnectionError < StandardError; end
        
      include WireProtocol
      
      def initialize(config)
        @config = config
      end
      
      def call_remote(request_handler, message, params)
        packet = WirePacket.new(message, params)

        begin
          send_data_to_socket(packet.to_json)
          response = fetch_data_from_socket(@config.timeout(message))
          response.handle_with(request_handler)
        rescue Timeout::Error => e
          backtrace = e.backtrace ; backtrace.shift # because Timeout puts some wierd stuff in there
          raise Timeout::Error, "Timed out calling wire server with message '#{message}'", backtrace
        end
      end
      
      def exception(params)
        WireException.new(params, @config.host, @config.port)
      end

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

      def fetch_data_from_socket(timeout)
        raw_response = Timeout.timeout(timeout) { socket.gets }
        WirePacket.parse(raw_response)
      end

      def socket
        @socket ||= TCPSocket.new(@config.host, @config.port)
      rescue Errno::ECONNREFUSED => exception
        raise(ConnectionError, "Unable to contact the wire server at #{@config.host}:#{@config.port}. Is it up?")
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cucumber-0.6.1 lib/cucumber/wire_support/connection.rb
cucumber-0.6.0 lib/cucumber/wire_support/connection.rb
cucumber-0.5.3 lib/cucumber/wire_support/connection.rb
cucumber-0.5.2 lib/cucumber/wire_support/connection.rb