Skip to content

Commit c984af4

Browse files
committed
Fixed bug passing :timeout to Cursor. Added tests.
1 parent 9882dc6 commit c984af4

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

lib/mongo/collection.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ def hint=(hint=nil)
141141
# @option opts [Array] :sort an array of [key, direction] pairs to sort by. Direction should
142142
# be specified as Mongo::ASCENDING (or :ascending / :asc) or Mongo::DESCENDING (or :descending / :desc)
143143
# @option opts [String, Array, OrderedHash] :hint hint for query optimizer, usually not necessary if using MongoDB > 1.1
144-
# @option opts [Boolean] :snapshot ('false') if true, snapshot mode will be used for this query.
144+
# @option opts [Boolean] :snapshot (false) if true, snapshot mode will be used for this query.
145145
# Snapshot mode assures no duplicates are returned, or objects missed, which were preset at both the start and
146146
# end of the query's execution. For details see http://www.mongodb.org/display/DOCS/How+to+do+Snapshotting+in+the+Mongo+Database
147147
# @option opts [Boolean] :batch_size (100) the number of documents to returned by the database per GETMORE operation. A value of 0
148148
# will let the database server decide how many results to returns. This option can be ignored for most use cases.
149-
# @option opts [Boolean] :timeout ('true') when +true+, the returned cursor will be subject to
149+
# @option opts [Boolean] :timeout (true) when +true+, the returned cursor will be subject to
150150
# the normal cursor timeout behavior of the mongod process. When +false+, the returned cursor will never timeout. Note
151151
# that disabling timeout will only work when #find is invoked with a block. This is to prevent any inadvertant failure to
152152
# close the cursor, as the cursor is explicitly closed when block code finishes.
@@ -165,13 +165,12 @@ def find(selector={}, opts={})
165165
limit = opts.delete(:limit) || 0
166166
sort = opts.delete(:sort)
167167
hint = opts.delete(:hint)
168-
snapshot = opts.delete(:snapshot)
168+
snapshot = opts.delete(:snapshot)
169169
batch_size = opts.delete(:batch_size)
170+
timeout = (opts.delete(:timeout) == false) ? false : true
170171

171-
if opts[:timeout] == false && !block_given?
172-
raise ArgumentError, "Timeout can be set to false only when #find is invoked with a block."
173-
else
174-
timeout = opts.delete(:timeout) || false
172+
if !timeout
173+
raise ArgumentError, "Collection#find must be invoked with a block when timeout is disabled."
175174
end
176175

177176
if hint
@@ -187,7 +186,7 @@ def find(selector={}, opts={})
187186

188187
if block_given?
189188
yield cursor
190-
cursor.close()
189+
cursor.close
191190
nil
192191
else
193192
cursor

lib/mongo/cursor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def initialize(collection, options={})
4646
@order = options[:order]
4747
@hint = options[:hint]
4848
@snapshot = options[:snapshot]
49-
@timeout = options[:timeout] || true
49+
@timeout = options.has_key?(:timeout) ? options[:timeout] : true
5050
@explain = options[:explain]
5151
@socket = options[:socket]
5252
@tailable = options[:tailable] || false

test/cursor_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,32 @@ def test_limit
154154
assert_equal 5, results.length
155155
end
156156

157+
def test_timeout_options
158+
cursor = Cursor.new(@@coll)
159+
assert_equal true, cursor.timeout
160+
161+
cursor = @@coll.find
162+
assert_equal true, cursor.timeout
163+
164+
cursor = @@coll.find({}, :timeout => nil)
165+
assert_equal true, cursor.timeout
166+
167+
cursor = Cursor.new(@@coll, :timeout => false)
168+
assert_equal false, cursor.timeout
169+
170+
cursor = @@coll.find({}, :timeout => false)
171+
assert_equal false, cursor.timeout
172+
end
173+
174+
def test_timeout
175+
opts = Cursor.new(@@coll).query_opts
176+
assert_equal 0, opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT
177+
178+
opts = Cursor.new(@@coll, :timeout => false).query_opts
179+
assert_equal Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT,
180+
opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT
181+
end
182+
157183
def test_limit_exceptions
158184
assert_raise ArgumentError do
159185
cursor = @@coll.find().limit('not-an-integer')

0 commit comments

Comments
 (0)