class Warren::Handler::Test

Class Warren::Test provides provides a dummy RabbitMQ connection pool for use during testing.

Set up a test warren

By default, the test warren is disabled during testing to avoid storing messages unnecessarily. Instead you must explicitly enable it when you wish to test message receipt.

If using rspec it is suggested that you add the following to your spec_helper.rb

config.around(:each, warren: true) do |ex|
  Warren.handler.enable!
  ex.run
  Warren.handler.disable!
end

Making assertions

It is possible to query the test warren about the messages it has seen. In particular the following methods are useful:

{render:#messages}

{render:#last_message}

{render:#message_count}

{render:#messages_matching}

Example

describe QcResult, warren: true do
  let(:warren) { Warren.handler }

  setup { warren.clear_messages }
  let(:resource) { build :qc_result }
  let(:routing_key) { 'test.message.qc_result.' }

  it 'broadcasts the resource' do
    resource.save!
    expect(warren.messages_matching(routing_key)).to eq(1)
  end
end

Constants

DISABLED_WARNING

Warning displayed if the user attempts to make assertions against the handler without having enabled it.

Public Class Methods

new(*_args) click to toggle source

Creates a test warren with no messages. Test warrens are shared across all threads.

@param [_] _args Configuration arguments are ignored.

Calls superclass method
# File lib/warren/handler/test.rb, line 98
def initialize(*_args)
  super()
  @messages = []
  @exchanges = []
  @enabled = false
end

Public Instance Methods

<<(message) click to toggle source

Disable message logging if not required

# File lib/warren/handler/test.rb, line 187
def <<(message)
  @messages << message if @enabled
end
clear_messages() click to toggle source

Clear any logged messaged

@return [Array] The new empty array, lacking messages

# File lib/warren/handler/test.rb, line 131
def clear_messages
  @messages = []
  @exchanges = []
end
disable!() click to toggle source

Clean up after ourselves to avoid memory leaks

# File lib/warren/handler/test.rb, line 173
def disable!
  @enabled = false
  clear_messages
end
enable!() click to toggle source

Enable the warren

# File lib/warren/handler/test.rb, line 167
def enable!
  @enabled = true
  clear_messages
end
last_message() click to toggle source

Returns the last message received by the warren

@return [#routing_key#payload] The last message object received by the warren

# File lib/warren/handler/test.rb, line 141
def last_message
  messages.last
end
message_count() click to toggle source

Returns the total number message received by the warren since it was enabled

@return [Integer] The total number of messages

# File lib/warren/handler/test.rb, line 150
def message_count
  messages.length
end
messages() click to toggle source

Returns an array of all message received by the warren since it was enabled

@return [Array<#routing_key#payload>] All received messages

# File lib/warren/handler/test.rb, line 181
def messages
  raise_if_not_tracking
  @messages
end
messages_matching(routing_key) click to toggle source

Returns the total number message received by the warren matching the given routing_key since it was enabled

@param routing_key [String] The routing key to filter by

@return [Integer] The number of matching messages

# File lib/warren/handler/test.rb, line 162
def messages_matching(routing_key)
  messages.count { |message| message.routing_key == routing_key }
end
new_channel() click to toggle source

Returns a new channel, which proxies all message back to {messages} on the {Warren::Handler::Test}

@return [Warren::Test::Channel] A rabbitMQ channel that logs messaged to the test warren

# File lib/warren/handler/test.rb, line 122
def new_channel
  Channel.new(@logger, routing_key_template: @routing_key_template)
end
with_channel() { |new_channel| ... } click to toggle source

Yields a new channel, which proxies all message back to {messages} on the {Warren::Handler::Test}

@return [void]

@yieldreturn [Warren::Test::Channel] A rabbitMQ channel that logs messaged to the test warren

# File lib/warren/handler/test.rb, line 112
def with_channel
  yield new_channel
end

Private Instance Methods

raise_if_not_tracking() click to toggle source
# File lib/warren/handler/test.rb, line 193
def raise_if_not_tracking
  raise StandardError, DISABLED_WARNING unless @enabled
end