Sha256: 0707eeb5cc37cf0010edeb085929d40d4985eb0e4d23d12027c7b55a506c0bb8

Contents?: true

Size: 1.98 KB

Versions: 11

Compression:

Stored size: 1.98 KB

Contents

module Aws
  module AsyncClientStubs 

    include Aws::ClientStubs

    # @api private
    def setup_stubbing
      @stubs = {}
      @stub_mutex = Mutex.new
      if Hash === @config.stub_responses
        @config.stub_responses.each do |operation_name, stubs|
          apply_stubs(operation_name, Array === stubs ? stubs : [stubs])
        end
      end

      # When a client is stubbed allow the user to access the requests made
      @api_requests = []

      # allow to access signaled events when client is stubbed
      @send_events = []

      requests = @api_requests
      send_events = @send_events

      self.handle do |context|
        if input_stream = context[:input_event_stream_handler]
          stub_stream = StubStream.new
          stub_stream.send_events = send_events
          input_stream.event_emitter.stream = stub_stream 
          input_stream.event_emitter.validate_event = context.config.validate_params
        end
        requests << {
          operation_name: context.operation_name,
          params: context.params,
          context: context
        }
        @handler.call(context)
      end
    end

    def send_events
      if config.stub_responses
        @send_events
      else
        msg = 'This method is only implemented for stubbed clients, and is '\
              'available when you enable stubbing in the constructor with `stub_responses: true`'
        raise NotImplementedError.new(msg)
      end
    end

    class StubStream

      def initialize
        @state = :open
      end

      attr_accessor :send_events

      attr_reader :state

      def data(bytes, options = {})
        if options[:end_stream]
          @state = :closed
        else
          decoder = Aws::EventStream::Decoder.new
          event = decoder.decode_chunk(bytes).first
          @send_events << decoder.decode_chunk(event.payload.read).first
        end
      end

      def closed?
        @state == :closed
      end

      def close
        @state = :closed
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
aws-sdk-core-3.100.0 lib/aws-sdk-core/async_client_stubs.rb
aws-sdk-core-3.99.2 lib/aws-sdk-core/async_client_stubs.rb
aws-sdk-core-3.99.1 lib/aws-sdk-core/async_client_stubs.rb
aws-sdk-core-3.99.0 lib/aws-sdk-core/async_client_stubs.rb
aws-sdk-core-3.98.0 lib/aws-sdk-core/async_client_stubs.rb
aws-sdk-core-3.97.1 lib/aws-sdk-core/async_client_stubs.rb
aws-sdk-core-3.97.0 lib/aws-sdk-core/async_client_stubs.rb
aws-sdk-core-3.96.1 lib/aws-sdk-core/async_client_stubs.rb
aws-sdk-core-3.96.0 lib/aws-sdk-core/async_client_stubs.rb
aws-sdk-core-3.95.0 lib/aws-sdk-core/async_client_stubs.rb
aws-sdk-core-3.94.1 lib/aws-sdk-core/async_client_stubs.rb