lib/bunny_mock/channel.rb in bunny-mock-1.2.2 vs lib/bunny_mock/channel.rb in bunny-mock-1.3.0

- old
+ new

@@ -1,9 +1,8 @@ # frozen_string_literal: true module BunnyMock class Channel - # # API # # @return [Integer] Channel identifier @@ -22,11 +21,10 @@ # @param [Integer] id Channel identifier # # @api public # def initialize(connection = nil, id = nil) - # store channel id @id = id # store connection information @connection = connection @@ -56,11 +54,10 @@ # # @return [BunnyMock::Channel] self # @api public # def open - @status = :open self end @@ -69,11 +66,10 @@ # # @return [BunnyMock::Channel] self # @api public # def close - @status = :closed self end @@ -98,14 +94,11 @@ # # @return [BunnyMock::Exchange] Mocked exchange instance # @api public # def exchange(name, opts = {}) - - xchg = @connection.find_exchange(name) || Exchange.declare(self, name, opts) - - @connection.register_exchange xchg + @connection.register_exchange xchg_find_or_create(name, opts) end ## # Mocks a fanout exchange # @@ -182,10 +175,27 @@ # def default_exchange direct '', no_declare: true end + ## + # Mocks Bunny::Channel#basic_publish + # + # @param [String] payload Message payload. It will never be modified by Bunny or RabbitMQ in any way. + # @param [String] exchange Exchange to publish to + # @param [String] routing_key Routing key + # @param [Hash] opts Publishing options + + # @return [BunnyMock::Channel] Self + 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 + # @endgroup # @group Queue API ## @@ -196,13 +206,11 @@ # # @return [BunnyMock::Queue] Queue that was mocked or looked up # @api public # def queue(name = '', opts = {}) - queue = @connection.find_queue(name) || Queue.new(self, name, opts) - @connection.register_queue queue end ## # Create a new {BunnyMock::Queue} instance with no name @@ -212,14 +220,23 @@ # @return [BunnyMock::Queue] Queue that was mocked or looked up # @see #queue # @api public # def temporary_queue(opts = {}) - queue '', opts.merge(exclusive: true) end + ## + # Does nothing atm. + # + # @return nil + # @api public + # + def confirm_select(callback = nil) + # noop + end + # @endgroup # # Implementation # @@ -234,64 +251,59 @@ @connection.deregister_exchange xchg.name end # @private 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 # @private def queue_unbind(key, xchg) - exchange = @connection.find_exchange xchg - raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange exchange.remove_route key end # @private 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 # @private def xchg_routes_to?(key, xchg) - exchange = @connection.find_exchange xchg - raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange exchange.routes_to? key end # @private 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 # @private def xchg_unbind(routing_key, name) - source = @connection.find_exchange name - raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source source.remove_route routing_key + end + + private + + # @private + def xchg_find_or_create(name, opts = {}) + @connection.find_exchange(name) || Exchange.declare(self, name, opts) end end end