From 05dfef2f118e089a8c9e8cd6578c215310015702 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Tue, 10 May 2011 15:45:32 -0400 Subject: [PATCH] RUBY-267 Cursor#alive? --- lib/mongo/cursor.rb | 7 +++++++ test/cursor_test.rb | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 652d7af..9b61818 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -307,6 +307,13 @@ module Mongo # @return [Boolean] def closed?; @closed; end + # Is this cursor alive on the server? + # + # @return [Boolean] + def alive? + @cursor_id && @cursor_id != 0 + end + # Returns an integer indicating which query options have been selected. # # @return [Integer] diff --git a/test/cursor_test.rb b/test/cursor_test.rb index 7942169..2813ba9 100644 --- a/test/cursor_test.rb +++ b/test/cursor_test.rb @@ -32,6 +32,21 @@ class CursorTest < Test::Unit::TestCase assert_kind_of Numeric, explaination['nscanned'] end + def test_alive + batch = [] + 5000.times do |n| + batch << {:a => n} + end + @@coll.insert(batch) + cursor = @@coll.find + assert !cursor.alive? + cursor.next + assert cursor.alive? + cursor.close + assert !cursor.alive? + @@coll.remove + end + def test_count @@coll.remove