Class: Lepus::Testing::Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/lepus/testing/exchange.rb

Overview

Represents a fake exchange that stores published messages for testing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Exchange

Returns a new instance of Exchange.



9
10
11
12
# File 'lib/lepus/testing/exchange.rb', line 9

def initialize(name)
  @name = name
  @messages = []
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



7
8
9
# File 'lib/lepus/testing/exchange.rb', line 7

def messages
  @messages
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/lepus/testing/exchange.rb', line 7

def name
  @name
end

Class Method Details

.[](name) ⇒ Lepus::Testing::Exchange

Get or create an exchange by name

Parameters:

  • name (String)

    The exchange name

Returns:



62
63
64
# File 'lib/lepus/testing/exchange.rb', line 62

def [](name)
  exchanges[name.to_s] ||= new(name.to_s)
end

.allHash<String, Lepus::Testing::Exchange>

Get all exchanges

Returns:



68
69
70
# File 'lib/lepus/testing/exchange.rb', line 68

def all
  exchanges
end

.clear_all!Object

Clear all exchanges (remove them completely)



78
79
80
# File 'lib/lepus/testing/exchange.rb', line 78

def clear_all!
  exchanges.clear
end

.clear_all_messages!Object

Clear all messages from all exchanges



73
74
75
# File 'lib/lepus/testing/exchange.rb', line 73

def clear_all_messages!
  exchanges.each_value(&:clear_messages)
end

.total_messagesObject

Get the total number of messages across all exchanges



83
84
85
# File 'lib/lepus/testing/exchange.rb', line 83

def total_messages
  exchanges.values.sum(&:size)
end

Instance Method Details

#add_message(message) ⇒ Object

Add a message to this exchange

Parameters:

  • message (Hash)

    The message data including payload, routing_key, headers, etc.



16
17
18
# File 'lib/lepus/testing/exchange.rb', line 16

def add_message(message)
  @messages << message
end

#clear_messagesObject

Clear all messages from this exchange



21
22
23
# File 'lib/lepus/testing/exchange.rb', line 21

def clear_messages
  @messages.clear
end

#empty?Boolean

Check if this exchange has any messages

Returns:

  • (Boolean)


31
32
33
# File 'lib/lepus/testing/exchange.rb', line 31

def empty?
  @messages.empty?
end

#find_messages(criteria = {}) ⇒ Array<Hash>

Find messages matching specific criteria

Parameters:

  • criteria (Hash) (defaults to: {})

    Criteria to match against message data

Returns:

  • (Array<Hash>)

    Matching messages



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lepus/testing/exchange.rb', line 38

def find_messages(criteria = {})
  return @messages if criteria.empty?

  @messages.select do |message|
    criteria.all? do |key, value|
      case key
      when :routing_key
        message[:routing_key] == value
      when :payload
        message[:payload] == value
      when :headers
        message[:headers]&.any? { |k, v| value.any? { |vk, vv| k == vk && v == vv } }
      else
        message[key] == value
      end
    end
  end
end

#sizeObject

Get the number of messages in this exchange



26
27
28
# File 'lib/lepus/testing/exchange.rb', line 26

def size
  @messages.size
end