Skip to content

Commit

Permalink
Merge pull request #529 from DripEmail/master
Browse files Browse the repository at this point in the history
Memoization of boolean causes extra calls to SQS
  • Loading branch information
phstc authored Oct 30, 2018
2 parents 7da1c2a + bbd1ee9 commit ef585d6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/shoryuken/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def receive_messages(options)
end

def fifo?
@_fifo ||= queue_attributes.attributes[FIFO_ATTR] == 'true'
# Make sure the memoization work with boolean to avoid multiple calls to SQS
# see https://github.com/phstc/shoryuken/pull/529
return @_fifo if defined?(@_fifo)
@_fifo = queue_attributes.attributes[FIFO_ATTR] == 'true'
end

private
Expand Down
21 changes: 19 additions & 2 deletions spec/shoryuken/queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,8 @@ def call(options)
end

describe '#fifo?' do
let(:attribute_response) { double 'Aws::SQS::Types::GetQueueAttributesResponse' }
before do
attribute_response = double 'Aws::SQS::Types::GetQueueAttributesResponse'

allow(attribute_response).to(
receive(:attributes).and_return('FifoQueue' => fifo.to_s, 'ContentBasedDeduplication' => 'true')
)
Expand All @@ -209,12 +208,30 @@ def call(options)
let(:fifo) { true }

specify { expect(subject.fifo?).to be }

it 'memoizes response' do
expect(sqs).to(
receive(:get_queue_attributes).with(queue_url: queue_url, attribute_names: ['All']).and_return(attribute_response)
).exactly(1).times

subject.fifo?
subject.fifo?
end
end

context 'when queue is not FIFO' do
let(:fifo) { false }

specify { expect(subject.fifo?).to_not be }

it 'memoizes response' do
expect(sqs).to(
receive(:get_queue_attributes).with(queue_url: queue_url, attribute_names: ['All']).and_return(attribute_response)
).exactly(1).times

subject.fifo?
subject.fifo?
end
end
end
end

0 comments on commit ef585d6

Please sign in to comment.