-
-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strict priority polling w/ backmerge #288
Changes from all commits
05b467b
160bba7
5cb87b0
37256ae
298f560
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,3 +98,142 @@ | |
end | ||
end | ||
end | ||
|
||
describe Shoryuken::Polling::StrictPriority do | ||
let(:queue1) { 'shoryuken' } | ||
let(:queue2) { 'uppercut' } | ||
let(:queue3) { 'other' } | ||
let(:queues) { Array.new } | ||
subject { Shoryuken::Polling::StrictPriority.new(queues) } | ||
|
||
describe '#next_queue' do | ||
it 'cycles when declared desc' do | ||
# [shoryuken, 2] | ||
# [uppercut, 1] | ||
queues << queue1 | ||
queues << queue1 | ||
queues << queue2 | ||
|
||
expect(subject.next_queue).to eq(queue1) | ||
expect(subject.next_queue).to eq(queue2) | ||
expect(subject.next_queue).to eq(queue1) | ||
expect(subject.next_queue).to eq(queue2) | ||
end | ||
|
||
it 'cycles when declared asc' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why looks the same as previous one, while priorities are different? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The priorities aren't different, the priorities are the number. They are declaration order independent. Therefore
and
Have the same meaning, because they have the same priority numbers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, did not see that queues are reversed! |
||
# [uppercut, 1] | ||
# [shoryuken, 2] | ||
queues << queue2 | ||
queues << queue1 | ||
queues << queue1 | ||
|
||
expect(subject.next_queue).to eq(queue1) | ||
expect(subject.next_queue).to eq(queue2) | ||
expect(subject.next_queue).to eq(queue1) | ||
expect(subject.next_queue).to eq(queue2) | ||
end | ||
|
||
it 'returns nil if there are no active queues' do | ||
expect(subject.next_queue).to eq(nil) | ||
end | ||
|
||
it 'unpauses queues whose pause is expired' do | ||
# [shoryuken, 3] | ||
# [uppercut, 2] | ||
# [other, 1] | ||
queues << queue1 | ||
queues << queue1 | ||
queues << queue1 | ||
queues << queue2 | ||
queues << queue2 | ||
queues << queue3 | ||
|
||
allow(subject).to receive(:delay).and_return(10) | ||
|
||
now = Time.now | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good practice 👍 |
||
allow(Time).to receive(:now).and_return(now) | ||
|
||
# pause the second queue, see it loop between 1 and 3 | ||
subject.messages_found(queue2, 0) | ||
expect(subject.next_queue).to eq(queue1) | ||
expect(subject.next_queue).to eq(queue3) | ||
expect(subject.next_queue).to eq(queue1) | ||
|
||
now += 5 | ||
allow(Time).to receive(:now).and_return(now) | ||
|
||
# pause the first queue, see it repeat 3 | ||
subject.messages_found(queue1, 0) | ||
expect(subject.next_queue).to eq(queue3) | ||
expect(subject.next_queue).to eq(queue3) | ||
|
||
# pause the third queue, see it have nothing | ||
subject.messages_found(queue3, 0) | ||
expect(subject.next_queue).to eq(nil) | ||
|
||
# unpause queue 2 | ||
now += 6 | ||
allow(Time).to receive(:now).and_return(now) | ||
expect(subject.next_queue).to eq(queue2) | ||
|
||
# unpause queues 1 and 3 | ||
now += 6 | ||
allow(Time).to receive(:now).and_return(now) | ||
expect(subject.next_queue).to eq(queue1) | ||
expect(subject.next_queue).to eq(queue2) | ||
expect(subject.next_queue).to eq(queue3) | ||
end | ||
end | ||
|
||
describe '#messages_found' do | ||
it 'pauses a queue if there are no messages found' do | ||
# [shoryuken, 2] | ||
# [uppercut, 1] | ||
queues << queue1 | ||
queues << queue1 | ||
queues << queue2 | ||
|
||
expect(subject.active_queues).to eq([[queue1, 2], [queue2, 1]]) | ||
expect(subject).to receive(:pause).with(queue1).and_call_original | ||
subject.messages_found(queue1, 0) | ||
expect(subject.active_queues).to eq([[queue2, 1]]) | ||
end | ||
|
||
it 'continues to queue the highest priority queue if messages are found' do | ||
# [shoryuken, 3] | ||
# [uppercut, 2] | ||
# [other, 1] | ||
queues << queue1 | ||
queues << queue1 | ||
queues << queue1 | ||
queues << queue2 | ||
queues << queue2 | ||
queues << queue3 | ||
|
||
expect(subject.next_queue).to eq(queue1) | ||
subject.messages_found(queue1, 1) | ||
expect(subject.next_queue).to eq(queue1) | ||
subject.messages_found(queue1, 1) | ||
expect(subject.next_queue).to eq(queue1) | ||
end | ||
|
||
it 'resets the priorities if messages are found part way' do | ||
# [shoryuken, 3] | ||
# [uppercut, 2] | ||
# [other, 1] | ||
queues << queue1 | ||
queues << queue1 | ||
queues << queue1 | ||
queues << queue2 | ||
queues << queue2 | ||
queues << queue3 | ||
|
||
expect(subject.next_queue).to eq(queue1) | ||
expect(subject.next_queue).to eq(queue2) | ||
subject.messages_found(queue2, 1) | ||
expect(subject.next_queue).to eq(queue1) | ||
expect(subject.next_queue).to eq(queue2) | ||
expect(subject.next_queue).to eq(queue3) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍