forked from stripe-archive/mongoriver
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to break out of tailer when some conditions are met inside of t…
…he block (stripe-archive#17) * Allow to break out of tailer when some conditions are met inside of the block * Switch to ruby 2.2 in travis and set fixed version for mongo Mongo 2.0 has breaking changes * Address code review feedback
- Loading branch information
1 parent
70b4dad
commit ae548a5
Showing
8 changed files
with
140 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
language: ruby | ||
rvm: | ||
- "1.8.7" | ||
- "1.9.3" | ||
- "2.1.2" | ||
- "2.2.4" | ||
# command to run tests | ||
script: "bundle exec rake test" | ||
script: "bundle exec rake test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Mongoriver | ||
class TailerStreamState | ||
attr_reader :count | ||
|
||
def initialize(limit=nil) | ||
@break = false | ||
@limit = limit | ||
@count = 0 | ||
end | ||
|
||
def break? | ||
return @break | ||
end | ||
|
||
def break | ||
@break = true | ||
end | ||
|
||
def increment() | ||
@count += 1 | ||
if !@limit.nil? && @count >= @limit | ||
self.break | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Mongoriver | ||
VERSION = "0.4.3" | ||
VERSION = "0.4.4" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
require 'minitest/autorun' | ||
require 'mocha/setup' | ||
require 'mongo' | ||
require 'mongoriver' | ||
|
||
describe 'Mongoriver::Tailer' do | ||
class CursorStub | ||
def initialize | ||
@events = [] | ||
@index = 0 | ||
end | ||
|
||
def add_option(opt) end | ||
|
||
def generate_ops(max) | ||
@index = 0 | ||
(1..max).map do |id| | ||
@events << { | ||
'ts' => BSON::Timestamp.new(Time.now.to_i, 0), | ||
'ns' => 'foo.bar', | ||
'op' => 'i', | ||
'o' => { | ||
'_id' => id.to_s | ||
} | ||
} | ||
end | ||
end | ||
|
||
def has_next? | ||
@index < @events.length | ||
end | ||
|
||
def next | ||
@index += 1 | ||
@events[@index - 1] | ||
end | ||
end | ||
|
||
before do | ||
cursor = CursorStub.new | ||
collection = stub do | ||
expects(:find).yields(cursor) | ||
end | ||
db = stub do | ||
expects(:collection).with('oplog.rs').returns(collection) | ||
end | ||
conn = stub(server_info: {}) do | ||
expects(:db).with('local').returns(db) | ||
end | ||
@cursor = cursor | ||
@tailer = Mongoriver::Tailer.new([conn], :existing) | ||
@tailer.tail | ||
end | ||
|
||
it 'tailer stream with limit' do | ||
@cursor.generate_ops(10) | ||
count = 0 | ||
has_more = @tailer.stream(5) do |_| | ||
count += 1 | ||
end | ||
assert(has_more) | ||
assert_equal(5, count) | ||
end | ||
|
||
it 'tailer stream without limit' do | ||
@cursor.generate_ops(10) | ||
count = 0 | ||
has_more = @tailer.stream do |_| | ||
count += 1 | ||
end | ||
assert(!has_more) | ||
assert_equal(10, count) | ||
end | ||
|
||
it 'tailer stream allow to break out' do | ||
@cursor.generate_ops(10) | ||
count = 0 | ||
has_more = @tailer.stream do |_, state| | ||
count += 1 | ||
state.break if count == 5 | ||
assert_equal(count, state.count) | ||
end | ||
assert(has_more) | ||
assert_equal(5, count) | ||
end | ||
|
||
it 'tailer stream allow to break out before limit' do | ||
@cursor.generate_ops(10) | ||
count = 0 | ||
has_more = @tailer.stream(7) do |_, state| | ||
count += 1 | ||
state.break if count == 5 | ||
assert_equal(count, state.count) | ||
end | ||
assert(has_more) | ||
assert_equal(5, count) | ||
end | ||
end |