diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index b3d9a7c..84c8485 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -141,12 +141,12 @@ module Mongo # @option opts [Array] :sort an array of [key, direction] pairs to sort by. Direction should # be specified as Mongo::ASCENDING (or :ascending / :asc) or Mongo::DESCENDING (or :descending / :desc) # @option opts [String, Array, OrderedHash] :hint hint for query optimizer, usually not necessary if using MongoDB > 1.1 - # @option opts [Boolean] :snapshot ('false') if true, snapshot mode will be used for this query. + # @option opts [Boolean] :snapshot (false) if true, snapshot mode will be used for this query. # Snapshot mode assures no duplicates are returned, or objects missed, which were preset at both the start and # end of the query's execution. For details see http://www.mongodb.org/display/DOCS/How+to+do+Snapshotting+in+the+Mongo+Database # @option opts [Boolean] :batch_size (100) the number of documents to returned by the database per GETMORE operation. A value of 0 # will let the database server decide how many results to returns. This option can be ignored for most use cases. - # @option opts [Boolean] :timeout ('true') when +true+, the returned cursor will be subject to + # @option opts [Boolean] :timeout (true) when +true+, the returned cursor will be subject to # the normal cursor timeout behavior of the mongod process. When +false+, the returned cursor will never timeout. Note # that disabling timeout will only work when #find is invoked with a block. This is to prevent any inadvertant failure to # close the cursor, as the cursor is explicitly closed when block code finishes. @@ -165,13 +165,12 @@ module Mongo limit = opts.delete(:limit) || 0 sort = opts.delete(:sort) hint = opts.delete(:hint) - snapshot = opts.delete(:snapshot) + snapshot = opts.delete(:snapshot) batch_size = opts.delete(:batch_size) + timeout = (opts.delete(:timeout) == false) ? false : true - if opts[:timeout] == false && !block_given? - raise ArgumentError, "Timeout can be set to false only when #find is invoked with a block." - else - timeout = opts.delete(:timeout) || false + if !timeout + raise ArgumentError, "Collection#find must be invoked with a block when timeout is disabled." end if hint @@ -187,7 +186,7 @@ module Mongo if block_given? yield cursor - cursor.close() + cursor.close nil else cursor diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index efdba91..96766a8 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -46,7 +46,7 @@ module Mongo @order = options[:order] @hint = options[:hint] @snapshot = options[:snapshot] - @timeout = options[:timeout] || true + @timeout = options.has_key?(:timeout) ? options[:timeout] : true @explain = options[:explain] @socket = options[:socket] @tailable = options[:tailable] || false diff --git a/test/cursor_test.rb b/test/cursor_test.rb index b3c637c..f904e82 100644 --- a/test/cursor_test.rb +++ b/test/cursor_test.rb @@ -154,6 +154,32 @@ class CursorTest < Test::Unit::TestCase assert_equal 5, results.length end + def test_timeout_options + cursor = Cursor.new(@@coll) + assert_equal true, cursor.timeout + + cursor = @@coll.find + assert_equal true, cursor.timeout + + cursor = @@coll.find({}, :timeout => nil) + assert_equal true, cursor.timeout + + cursor = Cursor.new(@@coll, :timeout => false) + assert_equal false, cursor.timeout + + cursor = @@coll.find({}, :timeout => false) + assert_equal false, cursor.timeout + end + + def test_timeout + opts = Cursor.new(@@coll).query_opts + assert_equal 0, opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT + + opts = Cursor.new(@@coll, :timeout => false).query_opts + assert_equal Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT, + opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT + end + def test_limit_exceptions assert_raise ArgumentError do cursor = @@coll.find().limit('not-an-integer')