Class: Warren::Handler::Test

Inherits:
Base
  • Object
show all
Defined in:
lib/warren/handler/test.rb

Overview

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:

Method: Warren::Handler::Test#messages — Documentation for sanger_warren (0.2.0)

Method: Warren::Handler::Test#messages

Defined in:
lib/warren/handler/test.rb

#messagesArray<#routing_key#payload>

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

Returns:

  • (Array<#routing_key#payload>)

    All received messages




178
179
180
181
# File 'lib/warren/handler/test.rb', line 178

def messages
  raise_if_not_tracking
  @messages
end

Method: Warren::Handler::Test#last_message — Documentation for sanger_warren (0.2.0)

Method: Warren::Handler::Test#last_message

Defined in:
lib/warren/handler/test.rb

#last_message#routing_key#payload

Returns the last message received by the warren

Returns:

  • (#routing_key#payload)

    The last message object received by the warren




138
139
140
# File 'lib/warren/handler/test.rb', line 138

def last_message
  messages.last
end

Method: Warren::Handler::Test#message_count — Documentation for sanger_warren (0.2.0)

Method: Warren::Handler::Test#message_count

Defined in:
lib/warren/handler/test.rb

#message_countInteger

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

Returns:

  • (Integer)

    The total number of messages




147
148
149
# File 'lib/warren/handler/test.rb', line 147

def message_count
  messages.length
end

Method: Warren::Handler::Test#messages_matching — Documentation for sanger_warren (0.2.0)

Method: Warren::Handler::Test#messages_matching

Defined in:
lib/warren/handler/test.rb

#messages_matching(routing_key) ⇒ Integer

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

Parameters:

  • routing_key (String)

    The routing key to filter by

Returns:

  • (Integer)

    The number of matching messages




159
160
161
# File 'lib/warren/handler/test.rb', line 159

def messages_matching(routing_key)
  messages.count { |message| message.routing_key == routing_key }
end

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

Defined Under Namespace

Classes: Channel

Constant Summary collapse

DISABLED_WARNING =

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

<<~DISABLED_WARREN
  Test made against a disabled warren.
  Warren::Handler::Test must be explicitly enabled to track messages,
  it is a good idea to disable it again after testing the relevant
  behaviour. This ensures we track messages on a per-test basis, and
  avoids unnecessary message storage.

  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

  You can then tag tests with warren: true to enable warren testing.
DISABLED_WARREN

Instance Method Summary collapse

Methods inherited from Base

#connect, #disconnect

Constructor Details

#initialize(*_args) ⇒ Test

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

Parameters:

  • _args (_)

    Configuration arguments are ignored.


95
96
97
98
99
100
# File 'lib/warren/handler/test.rb', line 95

def initialize(*_args)
  super()
  @messages = []
  @exchanges = []
  @enabled = false
end

Instance Method Details

#<<(message) ⇒ Object

Disable message logging if not required


184
185
186
# File 'lib/warren/handler/test.rb', line 184

def <<(message)
  @messages << message if @enabled
end

#add_exchange(name, options) ⇒ Object


188
189
190
# File 'lib/warren/handler/test.rb', line 188

def add_exchange(name, options)
  @exchanges << [name, options] if @enabled
end

#clear_messagesArray

Clear any logged messaged

Returns:

  • (Array)

    The new empty array, lacking messages


128
129
130
131
# File 'lib/warren/handler/test.rb', line 128

def clear_messages
  @messages = []
  @exchanges = []
end

#disable!Object

Clean up after ourselves to avoid memory leaks


170
171
172
173
# File 'lib/warren/handler/test.rb', line 170

def disable!
  @enabled = false
  clear_messages
end

#enable!Object

Enable the warren


164
165
166
167
# File 'lib/warren/handler/test.rb', line 164

def enable!
  @enabled = true
  clear_messages
end

#last_message#routing_key#payload

Returns the last message received by the warren

Returns:

  • (#routing_key#payload)

    The last message object received by the warren


138
139
140
# File 'lib/warren/handler/test.rb', line 138

def last_message
  messages.last
end

#message_countInteger

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

Returns:

  • (Integer)

    The total number of messages


147
148
149
# File 'lib/warren/handler/test.rb', line 147

def message_count
  messages.length
end

#messagesArray<#routing_key#payload>

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

Returns:

  • (Array<#routing_key#payload>)

    All received messages


178
179
180
181
# File 'lib/warren/handler/test.rb', line 178

def messages
  raise_if_not_tracking
  @messages
end

#messages_matching(routing_key) ⇒ Integer

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

Parameters:

  • routing_key (String)

    The routing key to filter by

Returns:

  • (Integer)

    The number of matching messages


159
160
161
# File 'lib/warren/handler/test.rb', line 159

def messages_matching(routing_key)
  messages.count { |message| message.routing_key == routing_key }
end

#new_channelWarren::Test::Channel

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

Returns:

  • (Warren::Test::Channel)

    A rabbitMQ channel that logs messaged to the test warren


119
120
121
# File 'lib/warren/handler/test.rb', line 119

def new_channel
  Channel.new(@logger, routing_key_template: @routing_key_template)
end

#with_channel {|new_channel| ... } ⇒ void

This method returns an undefined value.

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

Yields:

Yield Returns:

  • (Warren::Test::Channel)

    A rabbitMQ channel that logs messaged to the test warren


109
110
111
# File 'lib/warren/handler/test.rb', line 109

def with_channel
  yield new_channel
end