Class: Pwwka::TestHandler

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/pwwka/test_handler.rb

Overview

A handler you can use to examine messages your app sends during tests.

To use this:

  1. Create an instance and arrange for `test_setup` to be called when your tests are being setup (e.g.`def setup` or `before`)

  2. Arrange for `test_teardown` to be called during teardown of your tests

  3. Use the method `pop_message` to examine the message on the queue

Defined Under Namespace

Classes: Message

Constant Summary

Constants included from Logging

Logging::LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logf, #logger

Constructor Details

#initializeTestHandler


15
16
17
# File 'lib/pwwka/test_handler.rb', line 15

def initialize
  @channel_connector = ChannelConnector.new
end

Instance Attribute Details

#channel_connectorObject (readonly)

Returns the value of attribute channel_connector


13
14
15
# File 'lib/pwwka/test_handler.rb', line 13

def channel_connector
  @channel_connector
end

Instance Method Details

#get_topic_message_delivery_info_for_testsObject


54
55
56
57
58
# File 'lib/pwwka/test_handler.rb', line 54

def get_topic_message_delivery_info_for_tests
  deprecated!(:get_topic_message_delivery_info_for_tests,
              "Use `pop_message.delivery_info` instead")
  pop_message.delivery_info
end

#get_topic_message_payload_for_testsObject


42
43
44
45
46
# File 'lib/pwwka/test_handler.rb', line 42

def get_topic_message_payload_for_tests
  deprecated!(:get_topic_message_payload_for_tests,
              "Use `pop_message.payload` instead")
  pop_message.payload
end

#get_topic_message_properties_for_testsObject


48
49
50
51
52
# File 'lib/pwwka/test_handler.rb', line 48

def get_topic_message_properties_for_tests
  deprecated!(:get_topic_message_properties_for_tests,
              "Use `pop_message.properties` instead")
  pop_message.properties
end

#pop_messageObject

Get the message on the queue as TestHandler::Message


35
36
37
38
39
40
# File 'lib/pwwka/test_handler.rb', line 35

def pop_message
  delivery_info, properties, payload = test_queue.pop
  Message.new(delivery_info,
              properties,
              payload)
end

#purge_test_queueObject


60
61
62
63
# File 'lib/pwwka/test_handler.rb', line 60

def purge_test_queue
  test_queue.purge  
  channel_connector.delayed_queue.purge if channel_connector.configuration.allow_delayed?
end

#test_queueObject


26
27
28
29
30
31
32
# File 'lib/pwwka/test_handler.rb', line 26

def test_queue
  @test_queue  ||= begin
                     test_queue  = channel_connector.channel.queue("test-queue", durable: true)
                     test_queue.bind(channel_connector.topic_exchange, routing_key: "#.#")
                     test_queue
                   end
end

#test_setupObject

call this method to create the queue used for testing queue needs to be declared before the exchange is published to


21
22
23
24
# File 'lib/pwwka/test_handler.rb', line 21

def test_setup
  test_queue
  true
end

#test_teardownObject


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pwwka/test_handler.rb', line 65

def test_teardown
  test_queue.delete
  channel_connector.topic_exchange.delete
  # delayed messages
  if Pwwka.configuration.allow_delayed?
    channel_connector.delayed_queue.delete
    channel_connector.delayed_exchange.delete
  end

  channel_connector.connection_close
end