Browse files

Allow connection retries on cursor iteration. Closes #1609.

  • Loading branch information...
1 parent 0a54fac commit 9dd24779ee940d487d8ea53a3470594db27c6d5f @durran durran committed Jan 22, 2012
Showing with 52 additions and 7 deletions.
  1. +3 −0 CHANGELOG.md
  2. +4 −2 lib/mongoid/cursor.rb
  3. +1 −1 mongoid.gemspec
  4. +44 −4 spec/unit/mongoid/cursor_spec.rb
View
3 CHANGELOG.md
@@ -16,6 +16,9 @@ For instructions on upgrading to newer versions, visit
* \#1610 When versioning paranoid documents and max version is set, hard
delete old versions from the embedded relation.
+* \#1609 Allow connection retry during cursor iteration as well as all other
+ operations.
+
* \#1608 Guard against no method errors when passing ids in nested attributes
and the documents do not exist.
View
6 lib/mongoid/cursor.rb
@@ -45,8 +45,10 @@ def #{name}(*args)
# @example Iterate over the cursor.
# cursor.each { |doc| p doc.title }
def each
- cursor.each do |document|
- yield Mongoid::Factory.from_db(klass, document)
+ retry_on_connection_failure do
+ while document = cursor.next_document
+ yield Factory.from_db(klass, document)
+ end
end
end
View
2 mongoid.gemspec
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.add_development_dependency("rdoc", ["~> 3.5.0"])
s.add_development_dependency("bson_ext", ["~> 1.3"])
- s.add_development_dependency("mocha", ["~> 0.9.12"])
+ s.add_development_dependency("mocha", ["~> 0.10"])
s.add_development_dependency("rspec", ["~> 2.6"])
s.add_development_dependency("guard-rspec", ["~> 0.6"])
s.add_development_dependency("ammeter", ["~> 0.1.3"])
View
48 spec/unit/mongoid/cursor_spec.rb
@@ -49,12 +49,52 @@
describe "#each" do
before do
- proxy.expects(:each).yields({})
+ Mongoid.logger = ::Logger.new($stdout)
end
- it "yields to the next document" do
- cursor.each do |doc|
- doc.attributes.except("_id").should == Person.instantiate.attributes.except("_id")
+ after do
+ Mongoid.logger = nil
+ end
+
+ context "when no error occurs" do
+
+ before do
+ proxy.expects(:next_document).yields({})
+ end
+
+ it "yields to the next document" do
+ cursor.each do |doc|
+ doc.attributes.except("_id").should == Person.instantiate.attributes.except("_id")
+ end
+ end
+ end
+
+ context "when a connection error occurs" do
+
+ before do
+ Mongoid.max_retries_on_connection_failure = 1
+ end
+
+ after do
+ Mongoid.max_retries_on_connection_failure = 0
+ end
+
+ let(:seq) do
+ sequence("cursor")
+ end
+
+ before do
+ proxy.expects(:next_document).in_sequence(seq).raises(
+ Mongo::ConnectionFailure.new
+ )
+ proxy.expects(:next_document).in_sequence(seq).returns({})
+ proxy.expects(:next_document).in_sequence(seq).returns(nil)
+ end
+
+ it "retries the yield to the next document" do
+ cursor.each do |doc|
+ doc.should be_a(Person)
+ end
end
end
end

0 comments on commit 9dd2477

Please sign in to comment.