class BunnyMock::Session

Mocks Bunny::Session

Attributes

exchanges[R]

@return [Hash<String, BunnyMock::Exchange>] Exchanges created by this channel

queues[R]

@return [Hash<String, BunnyMock::Queue>] Queues created by this channel

status[R]

@return [Symbol] Current session status

Public Class Methods

new(*) click to toggle source

Creates a new {BunnyMock::Session} instance

@api public

# File lib/bunny_mock/session.rb, line 22
def initialize(*)
  # not connected until {BunnyMock::Session#start} is called
  @status = :not_connected

  # create channel hash
  @channels = {}

  # create storage for queues and exchanges
  @queues    = {}
  @exchanges = {}
end

Public Instance Methods

channel(n = nil, _pool_size = 1, *_args)
Alias for: create_channel
close()
Alias for: stop
closed?() click to toggle source

Tests if connection is closed

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

# File lib/bunny_mock/session.rb, line 70
def closed?
  @status == :closed
end
closing?() click to toggle source

Tests if connection is closing

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

# File lib/bunny_mock/session.rb, line 79
def closing?
  @status == :closing
end
connected?()
Alias for: open?
create_channel(n = nil, _pool_size = 1, *_args) click to toggle source

Creates a new {BunnyMock::Channel} instance

@param [Integer] n Channel identifier @param [Integer] pool_size Work pool size (insignificant)

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

# File lib/bunny_mock/session.rb, line 91
def create_channel(n = nil, _pool_size = 1, *_args)
  # raise same error as {Bunny::Session#create_channel}
  raise ArgumentError, 'channel number 0 is reserved in the protocol and cannot be used' if n && n.zero?

  # return cached channel if exists
  return @channels[n] if n && @channels.key?(n)

  # create and open channel
  channel = Channel.new self, n
  channel.open

  # return channel
  @channels[n] = channel
end
Also aliased as: channel
deregister_exchange(xchg) click to toggle source

@private

# File lib/bunny_mock/session.rb, line 180
def deregister_exchange(xchg)
  @exchanges.delete xchg
end
deregister_queue(queue) click to toggle source

@private

# File lib/bunny_mock/session.rb, line 165
def deregister_queue(queue)
  @queues.delete queue
end
exchange_exists?(name) click to toggle source

Test if exchange exists in channel cache

@param [String] name Name of exchange

@return [Boolean] true if exchange exists, false otherwise @api public

# File lib/bunny_mock/session.rb, line 146
def exchange_exists?(name)
  !find_exchange(name).nil?
end
find_exchange(name) click to toggle source

@private

# File lib/bunny_mock/session.rb, line 170
def find_exchange(name)
  @exchanges[name]
end
find_queue(name) click to toggle source

@private

# File lib/bunny_mock/session.rb, line 155
def find_queue(name)
  @queues[name]
end
open?() click to toggle source

Tests if connection is available

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

# File lib/bunny_mock/session.rb, line 60
def open?
  @status == :connected
end
Also aliased as: connected?
queue_exists?(name) click to toggle source

Test if queue exists in channel cache

@param [String] name Name of queue

@return [Boolean] true if queue exists, false otherwise @api public

# File lib/bunny_mock/session.rb, line 134
def queue_exists?(name)
  !find_queue(name).nil?
end
register_exchange(xchg) click to toggle source

@private

# File lib/bunny_mock/session.rb, line 175
def register_exchange(xchg)
  @exchanges[xchg.name] = xchg
end
register_queue(queue) click to toggle source

@private

# File lib/bunny_mock/session.rb, line 160
def register_queue(queue)
  @queues[queue.name] = queue
end
start() click to toggle source

Sets status to connected

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

# File lib/bunny_mock/session.rb, line 39
def start
  @status = :connected
  self
end
stop() click to toggle source

Sets status to closed

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

# File lib/bunny_mock/session.rb, line 49
def stop
  @status = :closed
  self
end
Also aliased as: close
with_channel(n = nil) { |ch| ... } click to toggle source

Creates a temporary {BunnyMock::Channel} instance, yields it to the block given, then closes it

@param [Integer] n Channel identifier

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

# File lib/bunny_mock/session.rb, line 115
def with_channel(n = nil)
  ch = create_channel(n)
  begin
    yield ch
  ensure
    ch.close if ch.open?
  end

  self
end