Sha256: 6cb76a718da99ae76c38767503c2db97cb921662ac5ffd43ec8916bd2f038eb9

Contents?: true

Size: 1.72 KB

Versions: 34

Compression:

Stored size: 1.72 KB

Contents

require_relative 'test/consumer'
require_relative 'test/store'
module Messaging
  module Adapters
    # Public: An adapter useful for test.
    #
    # Do NOT use this outside tests.
    # All messages will be saved in memory until cleared.
    class Test
      # Public: Used to inspect what have been published
      #
      # It's a hash keyed on the topic name.
      # Each value is an array of the messages that has been published to the topic
      attr_reader :topics

      def initialize
        # See https://www.rubytapas.com/2013/01/11/episode-045-hash-default-value/
        # for why this is needed for default values in a Hash
        @topics = Hash.new { |h, k| h[k] = [] }
      end

      def call(message)
        topics[message.topic] << message
        Messaging.routes.consumers.each do |c|
          Messaging.logger.info "Sending #{message} to #{c.queue} with length #{c.queue.length}"
          c.queue << message
        end
      end

      def create_consumer(name, **options)
        Consumer.new(name: name, **options)
      end

      # Resests the topics to a blank state.
      #
      # Useful to run before each example in specs to not share state.
      def clear_topics
        topics.clear
      end

      # @api private
      def store
        @store ||= Store.new
      end

      def self.register!
        return if Adapters.key? :test

        Adapters.register(:test, memoize: true) { Test.new }
        Adapters::Consumer.register(:test, memoize: true) { Adapters[:test] }
        Adapters::Dispatcher.register(:test, memoize: true) { Adapters[:test] }
        Adapters::Store.register(:test, memoize: true) { Adapters[:test].store }
      end
      private_class_method :register!

      register!
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
messaging-4.0.12 lib/messaging/adapters/test.rb
messaging-4.0.11 lib/messaging/adapters/test.rb
messaging-4.0.10 lib/messaging/adapters/test.rb
messaging-4.0.10.pre lib/messaging/adapters/test.rb
messaging-4.0.9 lib/messaging/adapters/test.rb
messaging-4.0.8 lib/messaging/adapters/test.rb
messaging-4.0.7 lib/messaging/adapters/test.rb
messaging-4.0.6 lib/messaging/adapters/test.rb
messaging-4.0.5 lib/messaging/adapters/test.rb
messaging-4.0.4.pre lib/messaging/adapters/test.rb
messaging-4.0.3.pre lib/messaging/adapters/test.rb
messaging-4.0.2.pre lib/messaging/adapters/test.rb
messaging-4.0.1.pre lib/messaging/adapters/test.rb
messaging-4.0.0.pre lib/messaging/adapters/test.rb
messaging-3.8.2 lib/messaging/adapters/test.rb
messaging-3.8.1 lib/messaging/adapters/test.rb
messaging-3.8.0 lib/messaging/adapters/test.rb
messaging-3.7.3 lib/messaging/adapters/test.rb
messaging-3.7.2 lib/messaging/adapters/test.rb
messaging-3.7.1 lib/messaging/adapters/test.rb