Class: Messaging::Adapters::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/messaging/adapters/test.rb,
lib/messaging/adapters/test/store.rb,
lib/messaging/adapters/test/stream.rb,
lib/messaging/adapters/test/category.rb,
lib/messaging/adapters/test/consumer.rb,
lib/messaging/adapters/test/categories.rb

Overview

Public: An adapter useful for test.

Do NOT use this outside tests. All messages will be saved in memory until cleared.

Defined Under Namespace

Classes: Categories, Category, Consumer, Store, Stream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTest

Returns a new instance of Test.



16
17
18
19
20
# File 'lib/messaging/adapters/test.rb', line 16

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

Instance Attribute Details

#topicsObject (readonly)

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



14
15
16
# File 'lib/messaging/adapters/test.rb', line 14

def topics
  @topics
end

Instance Method Details

#call(message) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/messaging/adapters/test.rb', line 22

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

#clear_topicsObject

Resests the topics to a blank state.

Useful to run before each example in specs to not share state.



37
38
39
# File 'lib/messaging/adapters/test.rb', line 37

def clear_topics
  topics.clear
end

#create_consumer(name, **options) ⇒ Object



30
31
32
# File 'lib/messaging/adapters/test.rb', line 30

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

#storeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
# File 'lib/messaging/adapters/test.rb', line 42

def store
  @store ||= Store.new
end