Class: Isimud::TestClient

Inherits:
Client
  • Object
show all
Defined in:
lib/isimud/test_client.rb

Overview

Interface for a messaging client that is suitable for testing. No network connections are used. All message deliveries are handled in a synchronous manner. When a message is published to the client, each declared queue is examined and, if the message’s routing key matches any of the patterns bound to the queue, the queue’s block is called with the message. Any uncaught exceptions raised within a message processing block will cause any declared exception handlers to be run. However, the message will not be re-queued should this occur.

Defined Under Namespace

Classes: Queue

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Client

#on_exception, #run_exception_handlers

Methods included from Logging

#log, #logger

Constructor Details

#initialize(connection = nil, options = {}) ⇒ TestClient

Returns a new instance of TestClient.



63
64
65
# File 'lib/isimud/test_client.rb', line 63

def initialize(connection = nil, options = {})
  self.queues = Hash.new
end

Instance Attribute Details

#queuesObject

Returns the value of attribute queues.



10
11
12
# File 'lib/isimud/test_client.rb', line 10

def queues
  @queues
end

Instance Method Details

#bind(queue_name, exchange_name, *keys, &block) ⇒ Object



87
88
89
90
# File 'lib/isimud/test_client.rb', line 87

def bind(queue_name, exchange_name, *keys, &block)
  queue = create_queue(queue_name, exchange_name, routing_keys: keys)
  subscribe(queue, &block)
end

#channelObject



71
72
73
# File 'lib/isimud/test_client.rb', line 71

def channel
  self
end

#closeObject



79
80
# File 'lib/isimud/test_client.rb', line 79

def close
end

#connectObject



67
68
69
# File 'lib/isimud/test_client.rb', line 67

def connect
  self
end

#connected?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/isimud/test_client.rb', line 75

def connected?
  true
end

#create_queue(queue_name, exchange_name, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/isimud/test_client.rb', line 96

def create_queue(queue_name, exchange_name, options = {})
  keys = options[:routing_keys] || []
  log "Isimud::TestClient: Binding queue #{queue_name} on exchange #{exchange_name} for keys #{keys.inspect}"
  queue = find_queue(queue_name)
  keys.each do |k|
    queue.bind(exchange_name, routing_key: k)
  end
  queue
end

#delete_queue(queue_name) ⇒ Object



82
83
84
85
# File 'lib/isimud/test_client.rb', line 82

def delete_queue(queue_name)
  log "Isimud::TestClient: deleting queue #{queue_name}"
  queues.delete(queue_name)
end

#find_queue(queue_name, _options = {}) ⇒ Object



92
93
94
# File 'lib/isimud/test_client.rb', line 92

def find_queue(queue_name, _options = {})
  queues[queue_name] ||= Queue.new(self, queue_name)
end

#publish(exchange, routing_key, payload, _options = {}) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/isimud/test_client.rb', line 112

def publish(exchange, routing_key, payload, _options = {})
  log "Isimud::TestClient: Delivering message exchange: #{exchange} key: #{routing_key} payload: #{payload}"
  call_queues = queues.values.select { |queue| queue.has_matching_key?(exchange, routing_key) }
  call_queues.each do |queue|
    log "Isimud::TestClient: Queue #{queue.name} matches routing key #{routing_key}"
    queue.deliver(payload)
  end
end

#reconnectObject



125
126
127
# File 'lib/isimud/test_client.rb', line 125

def reconnect
  self
end

#resetObject



121
122
123
# File 'lib/isimud/test_client.rb', line 121

def reset
  self.queues.clear
end

#subscribe(queue, _options = {}, &block) ⇒ Object



106
107
108
109
110
# File 'lib/isimud/test_client.rb', line 106

def subscribe(queue, _options = {}, &block)
  log "Isimud::TestClient: subscribing to events on queue #{queue.name}"
  queue.proc = block
  queue
end