Skip to content

Commit

Permalink
Insert in TAIL and remove from HEAD off list
Browse files Browse the repository at this point in the history
  • Loading branch information
henrich-m committed Jun 25, 2018
1 parent d98f732 commit af481a8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--format documentation
--color
--require spec_helper
--order random
2 changes: 1 addition & 1 deletion lib/upperkut/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(worker, redis)
def push_items(items = [])
items = [items] if items.is_a?(Hash)
return false if items.empty?
redis.lpush(key, encode_json_items(items))
redis.rpush(key, encode_json_items(items))
end

def fetch_items(batch_size = 1000)
Expand Down
29 changes: 26 additions & 3 deletions spec/upperkut/strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@ class DummyWorker
describe '.push_items' do
it 'insert items in the queue' do
expect do
strategy.push_items(['event' => 'open'])
end.to change { strategy.size }.from(0).to(1)
strategy.push_items([{ 'event' => 'open' }, { 'event' => 'click' }])
end.to change { strategy.size }.from(0).to(2)
end

context 'when items isnt a array' do
it 'insert items in the tail' do
strategy.push_items([{ 'event' => 'open' }])
strategy.push_items('event' => 'click')

items = strategy.fetch_items.collect do |item|
item['body']
end

expect(items.last).to eq('event' => 'click')
end

context 'when items isn\'t a array' do
it 'inserts item in the queue' do
expect do
strategy.push_items('event' => 'open', 'k' => 1)
Expand All @@ -30,6 +41,18 @@ class DummyWorker
end
end

describe '.fetch_items' do
it 'returns the head items off queue' do
strategy.push_items([{ 'event' => 'open' }, { 'event' => 'click' }])

items = strategy.fetch_items.collect do |item|
item['body']
end

expect(items).to eq([{ 'event' => 'open' }, { 'event' => 'click' }])
end
end

describe '.clear' do
it 'deletes the queue' do
strategy.push_items(['event' => 'open'])
Expand Down
2 changes: 1 addition & 1 deletion spec/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class DummyWorker
item['body']
end

expect(items_saved).to match_array(items)
expect(items_saved).to eq(items)
end
end

0 comments on commit af481a8

Please sign in to comment.