Sha256: 287ea84984364fd13b04e20735f571c52991ebbe28c88fa1ca8ae9400f0337d4

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

module Asynk
  module TestHelper
    def publish_sync(routing_key, options)
      consumers = find_consumers_by_route(routing_key)
      raise "Cant find consumer by route: #{routing_key}" if consumers.empty?
      raise "No ability to test multiple consumer per route" if consumers.count > 1
      consumer = consumers.first

      bunny_mocked_channel = BunnyMockedChannel.new
      bunny_mocked_delivery_info = BuunyMockedDeliveryInfo.new(routing_key)
      bunny_mock_properties = BuunyMockedProperties.new

      message = Asynk::Message.new(bunny_mocked_delivery_info, bunny_mock_properties, options.to_json)
      consumer_instance = consumer.new(bunny_mocked_channel, bunny_mocked_delivery_info) do |result|
        self.asynk_response = result.kind_of?(String) ? result : result.to_json
      end

      consumer_instance.invoke_processing(message)
      asynk_response
    end

    def asynk_response=(response)
      @asynk_response = response
    end

    def asynk_response
      @asynk_response
    end

    def find_consumers_by_route(route)
      Asynk.consumers.select{ |consumer| consumer.routing_keys.any?{ |key| key == route } }
    end

    class BunnyMockedChannel
      def ack(*args); end
      def reject(*args); end
      def retry(*args); end
    end

    class BuunyMockedDeliveryInfo
      attr_reader :routing_key
      def initialize(routing_key); @routing_key = routing_key; end
      def exchange; end
      def delivery_tag; end
    end

    class BuunyMockedProperties
      def timestamp; Time.now; end
      def message_id; 1; end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
asynk-0.0.2 lib/asynk/test_helper.rb
asynk-0.0.1 lib/asynk/test_helper.rb