Skip to content

Commit

Permalink
kill cursor after a find()
Browse files Browse the repository at this point in the history
  • Loading branch information
dcrosta committed Nov 9, 2011
1 parent faff840 commit 1a3668f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
12 changes: 12 additions & 0 deletions asyncmongo/cursor.py
Expand Up @@ -397,6 +397,18 @@ def _handle_response(self, result, error=None, orig_callback=None):
orig_callback(result['data'][0], error=None)
else:
orig_callback(result['data'], error=None)

if result.get('cursor_id'):
# logging.debug('killing cursor %s', result['cursor_id'])
connection = self.__pool.connection()
try:
connection.send_message(
message.kill_cursors([result['cursor_id']]),
callback=None)
except Exception, e:
logging.error('Error killing cursor %s: %s' % (result['cursor_id'], e))
connection.close()
raise

def __query_options(self):
"""Get the query options string to use for this query."""
Expand Down
41 changes: 41 additions & 0 deletions test/test_query.py
@@ -0,0 +1,41 @@
import tornado.ioloop
import logging
import time

import test_shunt
import asyncmongo
import pymongo


class QueryTest(test_shunt.MongoTest):
mongod_options = [('--port', '27999')]

@property
def pymongo_conn(self):
if not hasattr(self, '_pymongo_conn'):
self._pymongo_conn = pymongo.Connection(port=int(self.mongod_options[0][1]))
return self._pymongo_conn

def get_open_cursors(self):
output = self.pymongo_conn.admin.command('serverStatus')
return output.get('cursors', {}).get('totalOpen')

def setUp(self):
super(QueryTest, self).setUp()
self.pymongo_conn.test.foo.insert([{'i': i} for i in xrange(200)])

def test_query(self):
db = asyncmongo.Client(pool_id='test_query', host='127.0.0.1', port=27999, dbname='test', mincached=3)

def noop_callback(response, error):
tornado.ioloop.IOLoop.instance().stop()

before = self.get_open_cursors()
db.foo.find(limit=20, callback=noop_callback)
tornado.ioloop.IOLoop.instance().start()
after = self.get_open_cursors()
self.assertEquals(before, after, "%d cursors left open (should be 0)" % (after - before))

if __name__ == '__main__':
import unittest
unittest.main()

0 comments on commit 1a3668f

Please sign in to comment.