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 involved. Note that 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-placed onto the queue 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

- (TestClient) initialize(connection = nil, options = {})

Returns a new instance of TestClient



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

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

Instance Attribute Details

- (Object) queues

Returns the value of attribute queues



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

def queues
  @queues
end

Instance Method Details

- (Object) bind(queue_name, exchange_name, *keys, &block)



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

- (Object) channel



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

def channel
  self
end

- (Object) close



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

def close
end

- (Object) connect



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

def connect
  self
end

- (Boolean) connected?

Returns:

  • (Boolean)


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

def connected?
  true
end

- (Object) create_queue(queue_name, exchange_name, options = {})



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} for keys #{keys.inspect}"
  queue = find_queue(queue_name)
  keys.each do |k|
    queue.bind(exchange_name, routing_key: k)
  end
  queue
end

- (Object) delete_queue(queue_name)



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

def delete_queue(queue_name)
  queues.delete(queue_name)
end

- (Object) find_queue(queue_name, options = {})



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

- (Object) publish(exchange, routing_key, payload)



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

def publish(exchange, routing_key, payload)
  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

- (Object) reconnect



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

def reconnect
  self
end

- (Object) reset



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

def reset
  self.queues.clear
end

- (Object) subscribe(queue, options = {}, &block)



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

def subscribe(queue, options = {}, &block)
  queue.proc = block
  queue
end