Sha256: 58524471e0c50c6dd0c8221ba6b692719158cfa5c0876f7d350b4252fbf88c7c

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

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

module Cucumber
  module WireSupport
    class Connection
      include WireProtocol
      
      def initialize(config)
        @host, @port = config['host'], config['port']
      end
      
      def call_remote(response_handler, message, params)
        timeout = 3
        packet = WirePacket.new(message, params)

        begin
          send_data_to_socket(packet.to_json, timeout)
          response = fetch_data_from_socket(timeout)
          response.handle_with(response_handler)
        rescue Timeout::Error
          raise "Timed out calling server with message #{message}"
        end
      end
      
      def exception(params)
        WireException.new(params, @host, @port)
      end

      private
      
      def send_data_to_socket(data, timeout)
        Timeout.timeout(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(@host, @port)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cucumber-0.5.1 lib/cucumber/wire_support/connection.rb
cucumber-0.5.0 lib/cucumber/wire_support/connection.rb