arempe93/bunny-mock

channel.default_exchange doesn't behave like default exchange

Opened this issue · 3 comments

The default exchange should (as I understand - please correct if I'm wrong) route a message to a queue specified by the routing_key even if it is not explicitly bound (ala https://www.rabbitmq.com/tutorials/amqp-concepts.html under "Default Exchange")

So something like:

channel.default_exchange.publish('message', routing_key: 'test_queue', persistent: true)

should always send the message 'message' to the queue 'test_queue', even with no explicit binding, assuming that that queue exists.

This doesn't seem to happen with BunnyMock.

A possible solution (at least for my usage):

  • make a subclass of direct exchange for the default exchange and return that from channel.default_exchange
  • override the publish method to look for the queue specified by the routing key before doing anything else - if found, bind it there. This would at least produce the illusion of all queues being automatically bound to the default exchange for publishing purposes.

Another possibility:

  • make a singleton subclass of direct exchange for the default exchange and return that from channel.default_exchange
  • on queue creation automatically bind to that exchange

If I get some time I'll see if I can work one of those up and see if it works for me and if so put in a pull request. If not, happy for any sort of solution.

Sorry for reviving and old (but open) issue. Here's how I "solved" the issue:

# Override `BunnyMock::Queue#subscribe` to mimic the default exchange behavior
# of RabbitMQ. Also add the missing opposite `BunnyMock::Queue#cancel`.
module BunnyMock
  class Queue
    alias original_subscribe subscribe

    def subscribe(*args, &block)
      channel.default_exchange.add_route(name, self)

      original_subscribe(*args, &block)
    end

    def cancel
      channel.default_exchange.remove_route(name, self)
    end
  end
end

Can I open a PR to include this functionality in BunnyMock?

of course @arnodirlam - make a PR and we can talk about migration