Sha256: 2ada4d9cd3b7e935894798aa11ab033a864e70f7bfa8129245630169f0fccbb6

Contents?: true

Size: 1.79 KB

Versions: 20

Compression:

Stored size: 1.79 KB

Contents

require 'timeout'
require 'cucumber/wire/protocol'
require 'cucumber/wire/exception'
require 'cucumber/wire/data_packet'

module Cucumber
  module Wire
    class Connection
      class ConnectionError < StandardError; end

      include Wire::Protocol

      def initialize(config)
        @config = config
      end

      def call_remote(request_handler, message, params)
        packet = DataPacket.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)
        Wire::Exception.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?
        DataPacket.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

20 entries across 20 versions & 3 rubygems

Version Path
cucumber-wire-7.0.0 lib/cucumber/wire/connection.rb
cucumber-wire-6.2.1 lib/cucumber/wire/connection.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/cucumber-wire-6.2.0/lib/cucumber/wire/connection.rb
cucumber-wire-6.2.0 lib/cucumber/wire/connection.rb
cucumber-wire-6.1.1 lib/cucumber/wire/connection.rb
cucumber-wire-6.1.0 lib/cucumber/wire/connection.rb
cucumber-wire-6.0.1 lib/cucumber/wire/connection.rb
cucumber-wire-6.0.0 lib/cucumber/wire/connection.rb
cucumber-wire-5.0.1 lib/cucumber/wire/connection.rb
cucumber-wire-5.0.0 lib/cucumber/wire/connection.rb
cucumber-wire-4.0.1 lib/cucumber/wire/connection.rb
cucumber-wire-3.1.0 lib/cucumber/wire/connection.rb
cucumber-wire-3.0.0 lib/cucumber/wire/connection.rb
cucumber-wire-2.0.1 lib/cucumber/wire/connection.rb
cucumber-wire-2.0.0 lib/cucumber/wire/connection.rb
cucumber-wire-1.2.0 lib/cucumber/wire/connection.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/cucumber-wire-0.0.1/lib/cucumber/wire/connection.rb
cucumber-wire-1.1.0 lib/cucumber/wire/connection.rb
cucumber-wire-1.0.0 lib/cucumber/wire/connection.rb
cucumber-wire-0.0.1 lib/cucumber/wire/connection.rb