Sha256: be667fcc0d4b06411a82a124ab04ec9b012231cb867162e6b4a2498b38cc442f

Contents?: true

Size: 1.74 KB

Versions: 40

Compression:

Stored size: 1.74 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)
      end

      private

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

      def fetch_data_from_socket(timeout)
        raw_response =
          if timeout == :never
            socket.gets
          else
            Timeout.timeout(timeout) { socket.gets }
          end
        raise exception({'message' => "Remote Socket with #{@config.host}:#{@config.port} closed."}) if raw_response.nil?
        WirePacket.parse(raw_response)
      end

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

Version data entries

40 entries across 38 versions & 2 rubygems

Version Path
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/cucumber-1.3.18/lib/cucumber/wire_support/connection.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/cucumber-1.3.18/lib/cucumber/wire_support/connection.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/cucumber-1.3.16/lib/cucumber/wire_support/connection.rb
cucumber-2.1.0 lib/cucumber/wire_support/connection.rb
cucumber-2.0.2 lib/cucumber/wire_support/connection.rb
cucumber-2.0.1 lib/cucumber/wire_support/connection.rb
cucumber-1.3.20 lib/cucumber/wire_support/connection.rb
cucumber-2.0.0 lib/cucumber/wire_support/connection.rb
cucumber-2.0.0.rc.5 lib/cucumber/wire_support/connection.rb
cucumber-1.3.19 lib/cucumber/wire_support/connection.rb
cucumber-2.0.0.rc.4 lib/cucumber/wire_support/connection.rb
cucumber-2.0.0.rc.3 lib/cucumber/wire_support/connection.rb
cucumber-2.0.0.rc.2 lib/cucumber/wire_support/connection.rb
cucumber-2.0.0.rc.1 lib/cucumber/wire_support/connection.rb
cucumber-1.3.18 lib/cucumber/wire_support/connection.rb
cucumber-2.0.0.beta.5 lib/cucumber/wire_support/connection.rb
cucumber-2.0.0.beta.4 lib/cucumber/wire_support/connection.rb
cucumber-2.0.0.beta.3 lib/cucumber/wire_support/connection.rb
cucumber-1.3.17 lib/cucumber/wire_support/connection.rb
cucumber-2.0.0.beta.2 lib/cucumber/wire_support/connection.rb