Skip to content

Commit

Permalink
Update specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jterapin committed Sep 28, 2023
1 parent 2cdfca8 commit b9c5ece
Showing 1 changed file with 139 additions and 105 deletions.
244 changes: 139 additions & 105 deletions gems/aws-sdk-sqs/spec/queue_poller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,13 @@ def sample_message(n = nil)
end

it 'yields received messages to the block' do
client.stub_responses(:receive_message, [
{ messages: [sample_message] },
{ messages: [] }
])
client.stub_responses(
:receive_message,
[
{ messages: [sample_message] },
{ messages: [] }
]
)
yielded = nil
poller.poll(idle_timeout: 0) do |msg|
yielded = msg
Expand All @@ -142,13 +145,16 @@ def sample_message(n = nil)
end

it 'yields an array when max messages is greater than 1' do
client.stub_responses(:receive_message, [
{ messages: [
sample_message(1),
sample_message(2)
] },
{ messages: [] }
])
client.stub_responses(
:receive_message,
[
{ messages: [
sample_message(1),
sample_message(2)
] },
{ messages: [] }
]
)
yielded = nil
poller.poll(idle_timeout: 0, max_number_of_messages: 2) do |messages|
yielded = messages
Expand All @@ -160,18 +166,18 @@ def sample_message(n = nil)
'most recently received duplicated message' do
message_one = sample_message(1)
message_dup = message_one.dup
client.stub_responses(:receive_message, [
{ messages: [
message_one,
message_dup
] },
{ messages: [] }
])
client.stub_responses(
:receive_message,
[{ messages: [
message_one,
message_dup
] },
{ messages: [] }]
)
yielded_arr = []
poller.poll(idle_timeout: 0) do |msg|
yielded_arr << msg
end
puts yielded_arr
expect(yielded_arr.count).to eq(1)
expect(yielded_arr[0].receipt_handle).to eq(message_dup[:receipt_handle])
end
Expand All @@ -183,47 +189,57 @@ def sample_message(n = nil)
queue_url: queue_url,
entries: [{ id: 'id', receipt_handle: 'rh' }]
)
client.stub_responses(:receive_message, [
{ messages: [sample_message] },
{ messages: [] }
])
client.stub_responses(
:receive_message,
[
{ messages: [sample_message] },
{ messages: [] }
]
)
poller.poll(idle_timeout: 0) { |msg| }
end

it 'supports batch deletion' do
expect(client).to receive(:delete_message_batch)
.with({
queue_url: queue_url,
entries: [
{ id: 'id-1', receipt_handle: 'rh-1' },
{ id: 'id-2', receipt_handle: 'rh-2' }
]
})
client.stub_responses(:receive_message, [
{ messages: [
sample_message(1),
sample_message(2)
] },
{ messages: [] }
])
.with(
queue_url: queue_url,
entries: [
{ id: 'id-1', receipt_handle: 'rh-1' },
{ id: 'id-2', receipt_handle: 'rh-2' }
]
)
client.stub_responses(
:receive_message,
[
{ messages: [
sample_message(1),
sample_message(2)
] },
{ messages: [] }
]
)
poller.poll(idle_timeout: 0) { |msg| }
end

it 'can skip default delete behavior' do
expect(client).not_to receive(:delete_message_batch)
client.stub_responses(:receive_message, [
{ messages: [sample_message] },
{ messages: [] }
])
client.stub_responses(
:receive_message, [
{ messages: [sample_message] },
{ messages: [] }
]
)
poller.poll(idle_timeout: 0, skip_delete: true) { |msg| }
end

it 'skips delete when :skip_delete is thrown' do
expect(client).not_to receive(:delete_message_batch)
client.stub_responses(:receive_message, [
{ messages: [sample_message] },
{ messages: [] }
])
client.stub_responses(
:receive_message, [
{ messages: [sample_message] },
{ messages: [] }
]
)
poller.poll(idle_timeout: 0) { |_msg| throw :skip_delete }
end

Expand All @@ -232,10 +248,12 @@ def sample_message(n = nil)
queue_url: queue_url,
receipt_handle: 'rh'
)
client.stub_responses(:receive_message, [
{ messages: [sample_message] },
{ messages: [] }
])
client.stub_responses(
:receive_message, [
{ messages: [sample_message] },
{ messages: [] }
]
)
poller.poll(idle_timeout: 0, skip_delete: true) do |msg|
poller.delete_message(msg)
end
Expand All @@ -250,13 +268,15 @@ def sample_message(n = nil)
{ id: 'id-2', receipt_handle: 'rh-2' }
]
)
client.stub_responses(:receive_message, [
{ messages: [
sample_message(1),
sample_message(2)
] },
{ messages: [] }
])
client.stub_responses(
:receive_message, [
{ messages: [
sample_message(1),
sample_message(2)
] },
{ messages: [] }
]
)
poller.poll(idle_timeout: 0, max_number_of_messages: 10, skip_delete: true) do |messages|
poller.delete_messages(messages)
end
Expand All @@ -265,10 +285,12 @@ def sample_message(n = nil)

describe 'visibility timeouts' do
it 'provides a method to update the visibility timeout of a message' do
client.stub_responses(:receive_message, [
{ messages: [sample_message] },
{ messages: [] }
])
client.stub_responses(
:receive_message, [
{ messages: [sample_message] },
{ messages: [] }
]
)
resp = nil
poller.poll(idle_timeout: 0) do |msg|
resp = poller.change_message_visibility_timeout(msg, 60)
Expand Down Expand Up @@ -313,13 +335,15 @@ def sample_message(n = nil)

describe 'tracking stats' do
it 'counts the number of requests made' do
client.stub_responses(:receive_message, [
{ messages: [sample_message] },
{ messages: [] },
{ messages: [sample_message] },
{ messages: [] },
{ messages: [sample_message] }
])
client.stub_responses(
:receive_message, [
{ messages: [sample_message] },
{ messages: [] },
{ messages: [sample_message] },
{ messages: [] },
{ messages: [sample_message] }
]
)
poller.before_request do |stats|
throw :stop_polling if stats.received_message_count == 3
end
Expand All @@ -328,42 +352,48 @@ def sample_message(n = nil)
end

it 'counts the number of messages yielded in single mode' do
client.stub_responses(:receive_message, [
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [] }
])
client.stub_responses(
:receive_message, [
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [] }
]
)
stats = poller.poll(idle_timeout: 0) { |msg| }
expect(stats.received_message_count).to eq(5)
end

it 'counts the number of messages yielded in batch mode' do
client.stub_responses(:receive_message, [
{ messages: [
sample_message,
sample_message,
sample_message
] },
{ messages: [
sample_message,
sample_message,
sample_message
] },
{ messages: [] }
])
client.stub_responses(
:receive_message, [
{ messages: [
sample_message,
sample_message,
sample_message
] },
{ messages: [
sample_message,
sample_message,
sample_message
] },
{ messages: [] }
]
)
stats = poller.poll(idle_timeout: 0, max_number_of_messages: 3) { |msgs| }
expect(stats.received_message_count).to eq(6)
end

it 'tracks when a message was most recently received' do
client.stub_responses(:receive_message,
[
{ messages: [sample_message] },
{ messages: [] }
])
client.stub_responses(
:receive_message,
[
{ messages: [sample_message] },
{ messages: [] }
]
)
stats = poller.poll(idle_timeout: 0) { |msg| }
expect(stats.last_message_received_at).to be_kind_of(Time)
end
Expand All @@ -388,12 +418,14 @@ def sample_message(n = nil)
end

it 'yields a stats object to #poll' do
client.stub_responses(:receive_message, [
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [] }
])
client.stub_responses(
:receive_message, [
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [] }
]
)
yielded = nil
poller.poll(idle_timeout: 0) do |_msg, stats|
yielded = stats
Expand All @@ -405,12 +437,14 @@ def sample_message(n = nil)
end

it 'returns a stats object' do
client.stub_responses(:receive_message, [
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [] }
])
client.stub_responses(
:receive_message, [
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [sample_message] },
{ messages: [] }
]
)
stats = poller.poll(idle_timeout: 0) { |msg| }
expect(stats).to be_kind_of(QueuePoller::PollerStats)
expect(stats.request_count).to eq(4)
Expand Down

0 comments on commit b9c5ece

Please sign in to comment.