lib/bunny_mock/session.rb in bunny-mock-1.1.0 vs lib/bunny_mock/session.rb in bunny-mock-1.2.0
- old
+ new
@@ -1,153 +1,173 @@
+# frozen_string_literal: true
module BunnyMock
- # Mocks Bunny::Session
- class Session
+ # Mocks Bunny::Session
+ class Session
- #
- # API
- #
+ #
+ # API
+ #
- # @return [Symbol] Current session status
- attr_reader :status
+ # @return [Symbol] Current session status
+ attr_reader :status
- # @return [Hash<String, BunnyMock::Exchange>] Exchanges created by this channel
- attr_reader :exchanges
+ # @return [Hash<String, BunnyMock::Exchange>] Exchanges created by this channel
+ attr_reader :exchanges
- # @return [Hash<String, BunnyMock::Queue>] Queues created by this channel
- attr_reader :queues
+ # @return [Hash<String, BunnyMock::Queue>] Queues created by this channel
+ attr_reader :queues
- ##
- # Creates a new {BunnyMock::Session} instance
- #
- # @api public
- def initialize(*args)
+ ##
+ # Creates a new {BunnyMock::Session} instance
+ #
+ # @api public
+ def initialize(*)
- # not connected until {BunnyMock::Session#start} is called
- @status = :not_connected
+ # not connected until {BunnyMock::Session#start} is called
+ @status = :not_connected
- # create channel hash
- @channels = Hash.new
+ # create channel hash
+ @channels = {}
- # create storage for queues and exchanges
- @queues = Hash.new
- @exchanges = Hash.new
- end
+ # create storage for queues and exchanges
+ @queues = {}
+ @exchanges = {}
+ end
- ##
- # Sets status to connected
- #
- # @return [BunnyMock::Session] self
- # @api public
- def start
+ ##
+ # Sets status to connected
+ #
+ # @return [BunnyMock::Session] self
+ # @api public
+ def start
- @status = :connected
+ @status = :connected
- self
- end
+ self
+ end
- ##
- # Sets status to closed
- #
- # @return [BunnyMock::Session] self
- # @api public
- def stop
+ ##
+ # Sets status to closed
+ #
+ # @return [BunnyMock::Session] self
+ # @api public
+ def stop
- @status = :closed
+ @status = :closed
- self
- end
- alias close stop
+ self
+ end
+ alias close stop
- ##
- # Tests if connection is available
- #
- # @return [Boolean] true if status is connected, false otherwise
- # @api public
- def open?
+ ##
+ # Tests if connection is available
+ #
+ # @return [Boolean] true if status is connected, false otherwise
+ # @api public
+ def open?
- @status == :connected
- end
+ @status == :connected
+ end
- ##
- # 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
- def create_channel(n = nil, pool_size = 1)
+ ##
+ # 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
+ def create_channel(n = nil, _pool_size = 1)
- # raise same error as {Bunny::Session#create_channel}
- raise ArgumentError, "channel number 0 is reserved in the protocol and cannot be used" if n == 0
+ # raise same error as {Bunny::Session#create_channel}
+ raise ArgumentError, 'channel number 0 is reserved in the protocol and cannot be used' if n == 0
- # return cached channel if exists
- return @channels[n] if n and @channels.key?(n)
+ # return cached channel if exists
+ return @channels[n] if n && @channels.key?(n)
- # create and open channel
- channel = Channel.new self, n
- channel.open
+ # create and open channel
+ channel = Channel.new self, n
+ channel.open
- # return channel
- @channels[n] = channel
- end
- alias channel create_channel
+ # return channel
+ @channels[n] = channel
+ end
+ alias channel create_channel
- ##
- # Test if queue exists in channel cache
- #
- # @param [String] name Name of queue
- #
- # @return [Boolean] true if queue exists, false otherwise
- # @api public
- #
- def queue_exists?(name)
- !!find_queue(name)
- end
+ ##
+ # 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
+ def with_channel(n = nil)
+ ch = create_channel(n)
+ begin
+ yield ch
+ ensure
+ ch.close if ch.open?
+ end
- ##
- # Test if exchange exists in channel cache
- #
- # @param [String] name Name of exchange
- #
- # @return [Boolean] true if exchange exists, false otherwise
- # @api public
- #
- def exchange_exists?(name)
- !!find_exchange(name)
- end
+ self
+ end
- #
- # Implementation
- #
+ ##
+ # Test if queue exists in channel cache
+ #
+ # @param [String] name Name of queue
+ #
+ # @return [Boolean] true if queue exists, false otherwise
+ # @api public
+ #
+ def queue_exists?(name)
+ !find_queue(name).nil?
+ end
- # @private
- def find_queue(name)
- @queues[name]
- end
+ ##
+ # Test if exchange exists in channel cache
+ #
+ # @param [String] name Name of exchange
+ #
+ # @return [Boolean] true if exchange exists, false otherwise
+ # @api public
+ #
+ def exchange_exists?(name)
+ !find_exchange(name).nil?
+ end
- # @private
- def register_queue(queue)
- @queues[queue.name] = queue
- end
+ #
+ # Implementation
+ #
- # @private
- def deregister_queue(queue)
- @queues.delete queue
- end
+ # @private
+ def find_queue(name)
+ @queues[name]
+ end
- # @private
- def find_exchange(name)
- @exchanges[name]
- end
+ # @private
+ def register_queue(queue)
+ @queues[queue.name] = queue
+ end
- # @private
- def register_exchange(xchg)
- @exchanges[xchg.name] = xchg
- end
+ # @private
+ def deregister_queue(queue)
+ @queues.delete queue
+ end
- # @private
- def deregister_exchange(xchg)
- @exchanges.delete xchg
- end
- end
+ # @private
+ def find_exchange(name)
+ @exchanges[name]
+ end
+
+ # @private
+ def register_exchange(xchg)
+ @exchanges[xchg.name] = xchg
+ end
+
+ # @private
+ def deregister_exchange(xchg)
+ @exchanges.delete xchg
+ end
+ end
end