class BunnyMock::Channel

Attributes

acknowledged_state[R]

@return [Hash] with details of pending, acked and nacked messaged

connection[R]

@return [BunnyMock::Session] Session this channel belongs to

id[R]

@return [Integer] Channel identifier

status[R]

@return [Symbol] Current channel state

Public Class Methods

new(connection = nil, id = nil) click to toggle source

Create a new {BunnyMock::Channel} instance

@param [BunnyMock::Session] connection Mocked session instance @param [Integer] id Channel identifier

@api public

# File lib/bunny_mock/channel.rb, line 28
def initialize(connection = nil, id = nil)
  # store channel id
  @id = id

  # store connection information
  @connection = connection

  # initialize exchange and queue storage
  @exchanges = {}
  @queues    = {}
  @acknowledged_state = { pending: {}, acked: {}, nacked: {}, rejected: {} }

  # set status to opening
  @status = :opening
end

Public Instance Methods

ack(delivery_tag, multiple = false) click to toggle source

Acknowledge message.

@param [Integer] delivery_tag Delivery tag to acknowledge @param [Boolean] multiple (false) Should all unacknowledged messages up to this be acknowleded as well?

@return nil @api public

# File lib/bunny_mock/channel.rb, line 279
def ack(delivery_tag, multiple = false)
  if multiple
    @acknowledged_state[:pending].keys.each do |key|
      ack(key, false) if key <= delivery_tag
    end
  elsif @acknowledged_state[:pending].key?(delivery_tag)
    update_acknowledgement_state(delivery_tag, :acked)
  end
  nil
end
Also aliased as: acknowledge
acknowledge(delivery_tag, multiple = false)
Alias for: ack
basic_publish(payload, xchg, routing_key, opts = {}) click to toggle source

@return [BunnyMock::Channel] Self

# File lib/bunny_mock/channel.rb, line 201
def basic_publish(payload, xchg, routing_key, opts = {})
  xchg = xchg_find_or_create(xchg) unless xchg.respond_to? :name

  xchg.publish payload, opts.merge(routing_key: routing_key)

  self
end
close() click to toggle source

Sets status to closed

@return [BunnyMock::Channel] self @api public

# File lib/bunny_mock/channel.rb, line 74
def close
  @status = :closed

  self
end
closed?() click to toggle source

@return [Boolean] true if status is closed, false otherwise @api public

# File lib/bunny_mock/channel.rb, line 52
def closed?
  @status == :closed
end
confirm_select(callback = nil) click to toggle source

Does nothing atm.

@return nil @api public

# File lib/bunny_mock/channel.rb, line 246
def confirm_select(callback = nil)
  # noop
end
default_exchange() click to toggle source

Mocks RabbitMQ default exchange

@return [BunnyMock::Exchange] Mocked default exchange instance @api public

# File lib/bunny_mock/channel.rb, line 188
def default_exchange
  direct '', no_declare: true
end
deregister_exchange(xchg) click to toggle source

@private

# File lib/bunny_mock/channel.rb, line 343
def deregister_exchange(xchg)
  @connection.deregister_exchange xchg.name
end
deregister_queue(queue) click to toggle source

@private

# File lib/bunny_mock/channel.rb, line 338
def deregister_queue(queue)
  @connection.deregister_queue queue.name
end
direct(name, opts = {}) click to toggle source

Mocks a direct exchange

@param [String] name Exchange name @param [Hash] opts Exchange parameters

@option opts [Boolean] :durable @option opts [Boolean] :auto_delete @option opts [Hash] :arguments

@return [BunnyMock::Exchange] Mocked exchange instance @api public

# File lib/bunny_mock/channel.rb, line 144
def direct(name, opts = {})
  exchange name, opts.merge(type: :direct)
end
exchange(name, opts = {}) click to toggle source

Mocks an exchange

@param [String] name Exchange name @param [Hash] opts Exchange parameters

@option opts [Symbol,String] :type Type of exchange @option opts [Boolean] :durable @option opts [Boolean] :auto_delete @option opts [Hash] :arguments

@return [BunnyMock::Exchange] Mocked exchange instance @api public

# File lib/bunny_mock/channel.rb, line 102
def exchange(name, opts = {})
  @connection.register_exchange xchg_find_or_create(name, opts)
end
fanout(name, opts = {}) click to toggle source

Mocks a fanout exchange

@param [String] name Exchange name @param [Hash] opts Exchange parameters

@option opts [Boolean] :durable @option opts [Boolean] :auto_delete @option opts [Hash] :arguments

@return [BunnyMock::Exchange] Mocked exchange instance @api public

# File lib/bunny_mock/channel.rb, line 127
def fanout(name, opts = {})
  exchange name, opts.merge(type: :fanout)
end
generate_consumer_tag(name = 'bunny') click to toggle source

Unique string supposed to be used as a consumer tag.

@return [String] Unique string. @api plugin

# File lib/bunny_mock/channel.rb, line 110
def generate_consumer_tag(name = 'bunny')
  "#{name}-#{Time.now.to_i * 1000}-#{Kernel.rand(999_999_999_999)}"
end
header(name, opts = {}) click to toggle source

Mocks a headers exchange

@param [String] name Exchange name @param [Hash] opts Exchange parameters

@option opts [Boolean] :durable @option opts [Boolean] :auto_delete @option opts [Hash] :arguments

@return [BunnyMock::Exchange] Mocked exchange instance @api public

# File lib/bunny_mock/channel.rb, line 178
def header(name, opts = {})
  exchange name, opts.merge(type: :header)
end
inspect()
Alias for: to_s
nack(delivery_tag, multiple = false, requeue = false) click to toggle source

Unacknowledge message.

@param [Integer] delivery_tag Delivery tag to acknowledge @param [Boolean] multiple (false) Should all unacknowledged messages up to this be rejected as well? @param [Boolean] requeue (false) Should this message be requeued instead of dropping it?

@return nil @api public

# File lib/bunny_mock/channel.rb, line 301
def nack(delivery_tag, multiple = false, requeue = false)
  if multiple
    @acknowledged_state[:pending].keys.each do |key|
      nack(key, false, requeue) if key <= delivery_tag
    end
  elsif @acknowledged_state[:pending].key?(delivery_tag)
    delivery, header, body = update_acknowledgement_state(delivery_tag, :nacked)
    delivery.queue.publish(body, header.to_hash) if requeue
  end
  nil
end
open() click to toggle source

Sets status to open

@return [BunnyMock::Channel] self @api public

# File lib/bunny_mock/channel.rb, line 62
def open
  @status = :open

  self
end
open?() click to toggle source

@return [Boolean] true if status is open, false otherwise @api public

# File lib/bunny_mock/channel.rb, line 46
def open?
  @status == :open
end
prefetch(*) click to toggle source

Does nothing atm.

@return nil @api public

# File lib/bunny_mock/channel.rb, line 256
def prefetch(*)
  # noop
end
queue(name = '', opts = {}) click to toggle source

Create a new {BunnyMock::Queue} instance, or find in channel cache

@param [String] name Name of queue @param [Hash] opts Queue creation options

@return [BunnyMock::Queue] Queue that was mocked or looked up @api public

# File lib/bunny_mock/channel.rb, line 222
def queue(name = '', opts = {})
  queue = @connection.find_queue(name) || Queue.new(self, name, opts)
  @connection.register_queue queue
end
queue_bind(queue, key, xchg) click to toggle source

@private

# File lib/bunny_mock/channel.rb, line 348
def queue_bind(queue, key, xchg)
  exchange = @connection.find_exchange xchg
  raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange

  exchange.add_route key, queue
end
queue_unbind(queue, key, xchg) click to toggle source

@private

# File lib/bunny_mock/channel.rb, line 356
def queue_unbind(queue, key, xchg)
  exchange = @connection.find_exchange xchg
  raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange

  exchange.remove_route key, queue
end
reject(delivery_tag, requeue = false) click to toggle source

Rejects a message. A rejected message can be requeued or dropped by RabbitMQ.

@param [Integer] delivery_tag Delivery tag to reject @param [Boolean] requeue Should this message be requeued instead of dropping it?

@return nil @api public

# File lib/bunny_mock/channel.rb, line 323
def reject(delivery_tag, requeue = false)
  if @acknowledged_state[:pending].key?(delivery_tag)
    delivery, header, body = update_acknowledgement_state(delivery_tag, :rejected)
    delivery.queue.publish(body, header.to_hash) if requeue
  end
  nil
end
temporary_queue(opts = {}) click to toggle source

Create a new {BunnyMock::Queue} instance with no name

@param [Hash] opts Queue creation options

@return [BunnyMock::Queue] Queue that was mocked or looked up @see queue @api public

# File lib/bunny_mock/channel.rb, line 236
def temporary_queue(opts = {})
  queue '', opts.merge(exclusive: true)
end
to_s() click to toggle source

@return [String] Object representation

# File lib/bunny_mock/channel.rb, line 81
def to_s
  "#<#{self.class.name}:#{object_id} @id=#{@id} @open=#{open?}>"
end
Also aliased as: inspect
topic(name, opts = {}) click to toggle source

Mocks a topic exchange

@param [String] name Exchange name @param [Hash] opts Exchange parameters

@option opts [Boolean] :durable @option opts [Boolean] :auto_delete @option opts [Hash] :arguments

@return [BunnyMock::Exchange] Mocked exchange instance @api public

# File lib/bunny_mock/channel.rb, line 161
def topic(name, opts = {})
  exchange name, opts.merge(type: :topic)
end
wait_for_confirms(*) click to toggle source

Does not actually wait, but always return true.

@return true @api public

# File lib/bunny_mock/channel.rb, line 266
def wait_for_confirms(*)
  true
end
xchg_bind(receiver, routing_key, name) click to toggle source

@private

# File lib/bunny_mock/channel.rb, line 380
def xchg_bind(receiver, routing_key, name)
  source = @connection.find_exchange name
  raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source

  source.add_route routing_key, receiver
end
xchg_bound_to?(receiver, key, name) click to toggle source

@private

# File lib/bunny_mock/channel.rb, line 364
def xchg_bound_to?(receiver, key, name)
  source = @connection.find_exchange name
  raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source

  source.routes_to? receiver, routing_key: key
end
xchg_routes_to?(queue, key, xchg) click to toggle source

@private

# File lib/bunny_mock/channel.rb, line 372
def xchg_routes_to?(queue, key, xchg)
  exchange = @connection.find_exchange xchg
  raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange

  exchange.routes_to? queue, routing_key: key
end
xchg_unbind(routing_key, name, exchange) click to toggle source

@private

# File lib/bunny_mock/channel.rb, line 388
def xchg_unbind(routing_key, name, exchange)
  source = @connection.find_exchange name
  raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source

  source.remove_route routing_key, exchange
end

Private Instance Methods

update_acknowledgement_state(delivery_tag, new_state) click to toggle source

@private

# File lib/bunny_mock/channel.rb, line 403
def update_acknowledgement_state(delivery_tag, new_state)
  @acknowledged_state[new_state][delivery_tag] = @acknowledged_state[:pending].delete(delivery_tag)
end
xchg_find_or_create(name, opts = {}) click to toggle source

@private

# File lib/bunny_mock/channel.rb, line 398
def xchg_find_or_create(name, opts = {})
  @connection.find_exchange(name) || Exchange.declare(self, name, opts)
end