Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesluedke committed Oct 26, 2023
1 parent 13a401b commit d485518
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
1 change: 0 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Metrics/ClassLength:

Metrics/MethodLength:
Enabled: true
Max: 12
Exclude:
- 'spec/**/*.rb'
- 'test/**/*.rb'
Expand Down
16 changes: 10 additions & 6 deletions lib/ears/consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ def self.configure(opts = {})
self.queue = opts.fetch(:queue)
self.exchange = opts.fetch(:exchange)
self.routing_key = opts.fetch(:routing_key)
self.queue_options = {
durable: opts.fetch(:durable_queue, true),
retry_queue: opts.fetch(:retry_queue, false),
retry_delay: opts.fetch(:retry_delay, 5000),
error_queue: opts.fetch(:error_queue, false),
}
self.queue_options = queue_options_from(opts: opts)
self.durable_exchange = opts.fetch(:durable_exchange, true)
self.exchange_type = opts.fetch(:exchange_type, :topic)
end
Expand Down Expand Up @@ -139,6 +134,15 @@ class << self

private

def queue_options_from(opts:)
{
durable: opts.fetch(:durable_queue, true),
retry_queue: opts.fetch(:retry_queue, false),
retry_delay: opts.fetch(:retry_delay, 5000),
error_queue: opts.fetch(:error_queue, false),
}
end

attr_writer :queue,
:exchange,
:routing_key,
Expand Down
26 changes: 14 additions & 12 deletions lib/ears/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,24 @@ def consumer(queue, consumer_class, threads = 1, args = {})
#
# @param [Array<Class<Ears::Consumer>>] consumer_classes An array of subclasses of {Ears::Consumer} that call {Ears::Consumer#configure} in their class definition.
def setup_consumers(*consumer_classes)
consumer_classes.each do |consumer_class|
exchange =
exchange(
consumer_class.exchange,
consumer_class.exchange_type,
durable: consumer_class.durable_exchange,
)
configured_queue =
queue(consumer_class.queue, consumer_class.queue_options)
configured_queue.bind(exchange, routing_key: consumer_class.routing_key)
consumer(configured_queue, consumer_class)
end
consumer_classes.each { |consumer_class| setup_consumer(consumer_class) }
end

private

def setup_consumer(consumer_class)
exchange =
exchange(
consumer_class.exchange,
consumer_class.exchange_type,
durable: consumer_class.durable_exchange,
)
configured_queue =
queue(consumer_class.queue, consumer_class.queue_options)
configured_queue.bind(exchange, routing_key: consumer_class.routing_key)
consumer(configured_queue, consumer_class)
end

def queue_options(bunny_opts, retry_arguments)
return bunny_opts unless retry_arguments

Expand Down
46 changes: 46 additions & 0 deletions spec/ears/setup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,52 @@

before { allow(Ears).to receive(:channel).and_return(ears_channel) }

# rubocop:disable RSpec/SubjectStub
describe '#setup_consumers' do
let(:consumers) do
[
class_double(
Ears::Consumer,
exchange: 'my-exchange',
queue: 'my-queue',
exchange_type: :topic,
durable_exchange: true,
queue_options: {
my_option: true,
},
routing_key: 'my_key',
),
]
end
let(:an_exchange) { instance_double(Bunny::Exchange) }
let(:a_queue) { instance_double(Bunny::Queue, bind: nil) }

before do
allow(setup).to receive_messages(exchange: an_exchange, queue: a_queue)
allow(setup).to receive(:consumer)
end

it 'sets up the consumers accordingly including its queues etc.' do
setup.setup_consumers(*consumers)

expect(setup).to have_received(:exchange).with(
'my-exchange',
:topic,
durable: true,
)
expect(setup).to have_received(:queue).with(
'my-queue',
{ my_option: true },
)
expect(a_queue).to have_received(:bind).with(
an_exchange,
routing_key: 'my_key',
)
expect(setup).to have_received(:consumer).with(a_queue, consumers[0])
end
end
# rubocop:enable RSpec/SubjectStub

describe '#exchange' do
it 'creates a new Bunny exchange with the given options' do
expect(Bunny::Exchange).to receive(:new).with(
Expand Down

0 comments on commit d485518

Please sign in to comment.