Permalink
Browse files

update test code to init/teardown a mongod instance appropriately

  • Loading branch information...
1 parent 288a794 commit d27388f8a1837f7d878325cfc88022d6d1c8e14d @jehiah jehiah committed Sep 9, 2011
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
@@ -7,27 +7,29 @@
TEST_TIMESTAMP = int(time.time())
-def test_query():
- test_shunt.setup()
- db = asyncmongo.Client(pool_id='test_query', host='127.0.0.1', port=27017, dbname='test', mincached=3)
-
- def insert_callback(response, error):
- logging.info(response)
- assert len(response) == 1
- test_shunt.register_called('inserted')
- tornado.ioloop.IOLoop.instance().stop()
-
- db.test_users.insert({"_id" : "test_connection.%d" % TEST_TIMESTAMP}, safe=True, callback=insert_callback)
-
- tornado.ioloop.IOLoop.instance().start()
- test_shunt.assert_called('inserted')
-
- def callback(response, error):
- assert len(response) == 1
- test_shunt.register_called('got_record')
- tornado.ioloop.IOLoop.instance().stop()
-
- db.test_users.find({}, limit=1, callback=callback)
-
- tornado.ioloop.IOLoop.instance().start()
- test_shunt.assert_called("got_record")
+class ConnectionTest(test_shunt.MongoTest):
+ def test_query(self):
+ logging.info('in test_query')
+ test_shunt.setup()
+ db = asyncmongo.Client(pool_id='test_query', host='127.0.0.1', port=27017, dbname='test', mincached=3)
+
+ def insert_callback(response, error):
+ logging.info(response)
+ assert len(response) == 1
+ test_shunt.register_called('inserted')
+ tornado.ioloop.IOLoop.instance().stop()
+
+ db.test_users.insert({"_id" : "test_connection.%d" % TEST_TIMESTAMP}, safe=True, callback=insert_callback)
+
+ tornado.ioloop.IOLoop.instance().start()
+ test_shunt.assert_called('inserted')
+
+ def callback(response, error):
+ assert len(response) == 1
+ test_shunt.register_called('got_record')
+ tornado.ioloop.IOLoop.instance().stop()
+
+ db.test_users.find({}, limit=1, callback=callback)
+
+ tornado.ioloop.IOLoop.instance().start()
+ test_shunt.assert_called("got_record")
@@ -7,30 +7,30 @@
TEST_TIMESTAMP = int(time.time())
-def test_duplicate_insert():
- test_shunt.setup()
- db = asyncmongo.Client(pool_id='dup_insert', host='127.0.0.1', port=27017, dbname='test')
-
- def insert_callback(response, error):
- logging.info(response)
- assert len(response) == 1
- test_shunt.register_called('inserted')
- tornado.ioloop.IOLoop.instance().stop()
-
- db.test_users.insert({"_id" : "duplicate_insert.%d" % TEST_TIMESTAMP}, callback=insert_callback)
-
- tornado.ioloop.IOLoop.instance().start()
- test_shunt.assert_called('inserted')
-
- def duplicate_callback(response, error):
- logging.info(response)
- if error:
- test_shunt.register_called('dupe')
- tornado.ioloop.IOLoop.instance().stop()
-
- db.test_users.insert({"_id" : "duplicate_insert.%d" % TEST_TIMESTAMP}, callback=duplicate_callback)
-
- tornado.ioloop.IOLoop.instance().start()
- test_shunt.assert_called('dupe')
-
+class DuplicateInsertTest(test_shunt.MongoTest):
+ def test_duplicate_insert(self):
+ test_shunt.setup()
+ db = asyncmongo.Client(pool_id='dup_insert', host='127.0.0.1', port=27017, dbname='test')
+
+ def insert_callback(response, error):
+ logging.info(response)
+ assert len(response) == 1
+ test_shunt.register_called('inserted')
+ tornado.ioloop.IOLoop.instance().stop()
+
+ db.test_users.insert({"_id" : "duplicate_insert.%d" % TEST_TIMESTAMP}, callback=insert_callback)
+
+ tornado.ioloop.IOLoop.instance().start()
+ test_shunt.assert_called('inserted')
+
+ def duplicate_callback(response, error):
+ logging.info(response)
+ if error:
+ test_shunt.register_called('dupe')
+ tornado.ioloop.IOLoop.instance().stop()
+
+ db.test_users.insert({"_id" : "duplicate_insert.%d" % TEST_TIMESTAMP}, callback=duplicate_callback)
+
+ tornado.ioloop.IOLoop.instance().start()
+ test_shunt.assert_called('dupe')
View
@@ -7,39 +7,40 @@
TEST_TIMESTAMP = int(time.time())
-def test_insert():
- test_shunt.setup()
- db = asyncmongo.Client(pool_id='testinsert', host='127.0.0.1', port=27017, dbname='test')
-
- def insert_callback(response, error):
- logging.info(response)
- assert len(response) == 1
- test_shunt.register_called('inserted')
- tornado.ioloop.IOLoop.instance().stop()
-
- db.test_users.insert({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=insert_callback)
-
- tornado.ioloop.IOLoop.instance().start()
- test_shunt.assert_called('inserted')
-
- def query_callback(response, error):
- logging.info(response)
- assert len(response) == 1
- test_shunt.register_called('retrieved')
- tornado.ioloop.IOLoop.instance().stop()
-
- db.test_users.find_one({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=query_callback)
- tornado.ioloop.IOLoop.instance().start()
- test_shunt.assert_called('retrieved')
+class InsertDeleteTest(test_shunt.MongoTest):
+ def test_insert(self):
+ test_shunt.setup()
+ db = asyncmongo.Client(pool_id='testinsert', host='127.0.0.1', port=27017, dbname='test')
+
+ def insert_callback(response, error):
+ logging.info(response)
+ assert len(response) == 1
+ test_shunt.register_called('inserted')
+ tornado.ioloop.IOLoop.instance().stop()
+
+ db.test_users.insert({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=insert_callback)
+
+ tornado.ioloop.IOLoop.instance().start()
+ test_shunt.assert_called('inserted')
+
+ def query_callback(response, error):
+ logging.info(response)
+ assert len(response) == 1
+ test_shunt.register_called('retrieved')
+ tornado.ioloop.IOLoop.instance().stop()
+
+ db.test_users.find_one({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=query_callback)
+ tornado.ioloop.IOLoop.instance().start()
+ test_shunt.assert_called('retrieved')
-
- def delete_callback(response, error):
- logging.info(response)
- assert len(response) == 1
- test_shunt.register_called('deleted')
- tornado.ioloop.IOLoop.instance().stop()
-
- db.test_users.remove({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=delete_callback)
- tornado.ioloop.IOLoop.instance().start()
- test_shunt.assert_called('deleted')
+
+ def delete_callback(response, error):
+ logging.info(response)
+ assert len(response) == 1
+ test_shunt.register_called('deleted')
+ tornado.ioloop.IOLoop.instance().stop()
+
+ db.test_users.remove({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=delete_callback)
+ tornado.ioloop.IOLoop.instance().start()
+ test_shunt.assert_called('deleted')
View
@@ -6,44 +6,45 @@
import asyncmongo
TEST_TIMESTAMP = int(time.time())
-def test_pooled_db():
- """
- This tests simply verifies that we can grab two different connections from the pool
- and use them independantly.
- """
- print asyncmongo.__file__
- test_shunt.setup()
- client = asyncmongo.Client('id1', maxconnections=5, host='127.0.0.1', port=27017, dbname='test')
- test_users_collection = client.connection('test_users')
-
- def insert_callback(response, error):
- logging.info(response)
- assert len(response) == 1
- test_shunt.register_called('inserted')
- tornado.ioloop.IOLoop.instance().stop()
-
- test_users_collection.insert({"_id" : "record_test.%d" % TEST_TIMESTAMP}, safe=True, callback=insert_callback)
-
- tornado.ioloop.IOLoop.instance().start()
- test_shunt.assert_called('inserted')
-
- def pool_callback(response, error):
- assert len(response) == 1
- test_shunt.register_called('pool1')
- if test_shunt.is_called('pool2'):
- tornado.ioloop.IOLoop.instance().stop()
-
- def pool_callback2(response, error):
- assert len(response) == 1
- test_shunt.register_called('pool2')
+class PooledDBTest(test_shunt.MongoTest):
+ def test_pooled_db(self):
+ """
+ This tests simply verifies that we can grab two different connections from the pool
+ and use them independantly.
+ """
+ print asyncmongo.__file__
+ test_shunt.setup()
+ client = asyncmongo.Client('id1', maxconnections=5, host='127.0.0.1', port=27017, dbname='test')
+ test_users_collection = client.connection('test_users')
- if test_shunt.is_called('pool1'):
- # don't expect 2 finishes second
+ def insert_callback(response, error):
+ logging.info(response)
+ assert len(response) == 1
+ test_shunt.register_called('inserted')
tornado.ioloop.IOLoop.instance().stop()
-
- test_users_collection.find({}, limit=1, callback=pool_callback)
- test_users_collection.find({}, limit=1, callback=pool_callback2)
-
- tornado.ioloop.IOLoop.instance().start()
- test_shunt.assert_called('pool1')
- test_shunt.assert_called('pool2')
+
+ test_users_collection.insert({"_id" : "record_test.%d" % TEST_TIMESTAMP}, safe=True, callback=insert_callback)
+
+ tornado.ioloop.IOLoop.instance().start()
+ test_shunt.assert_called('inserted')
+
+ def pool_callback(response, error):
+ assert len(response) == 1
+ test_shunt.register_called('pool1')
+ if test_shunt.is_called('pool2'):
+ tornado.ioloop.IOLoop.instance().stop()
+
+ def pool_callback2(response, error):
+ assert len(response) == 1
+ test_shunt.register_called('pool2')
+
+ if test_shunt.is_called('pool1'):
+ # don't expect 2 finishes second
+ tornado.ioloop.IOLoop.instance().stop()
+
+ test_users_collection.find({}, limit=1, callback=pool_callback)
+ test_users_collection.find({}, limit=1, callback=pool_callback2)
+
+ tornado.ioloop.IOLoop.instance().start()
+ test_shunt.assert_called('pool1')
+ test_shunt.assert_called('pool2')
View
@@ -7,36 +7,37 @@
TEST_TIMESTAMP = int(time.time())
-def test_update_safe():
- test_shunt.setup()
- db = asyncmongo.Client(pool_id='testinsert', host='127.0.0.1', port=27017, dbname='test', maxconnections=2)
-
- def update_callback(response, error):
- logging.info(response)
- assert len(response) == 1
- test_shunt.register_called('update')
- tornado.ioloop.IOLoop.instance().stop()
-
- # all of these should be called, but only one should have a callback
- # we also are checking that connections in the pool never increases >1 with max_connections=2
- # this is because connections for safe=False calls get put back in the pool immediated
- db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
- db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
- db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
- db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
- db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, upsert=True, callback=update_callback)
-
- tornado.ioloop.IOLoop.instance().start()
- test_shunt.assert_called('update')
-
- def query_callback(response, error):
- logging.info(response)
- assert isinstance(response, dict)
- assert response['_id'] == TEST_TIMESTAMP
- assert response['test_count'] == 5
- test_shunt.register_called('retrieved')
- tornado.ioloop.IOLoop.instance().stop()
-
- db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
- tornado.ioloop.IOLoop.instance().start()
- test_shunt.assert_called('retrieved')
+class SafeUpdatesTest(test_shunt.MongoTest):
+ def test_update_safe(self):
+ test_shunt.setup()
+ db = asyncmongo.Client(pool_id='testinsert', host='127.0.0.1', port=27017, dbname='test', maxconnections=2)
+
+ def update_callback(response, error):
+ logging.info(response)
+ assert len(response) == 1
+ test_shunt.register_called('update')
+ tornado.ioloop.IOLoop.instance().stop()
+
+ # all of these should be called, but only one should have a callback
+ # we also are checking that connections in the pool never increases >1 with max_connections=2
+ # this is because connections for safe=False calls get put back in the pool immediated
+ db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
+ db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
+ db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
+ db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
+ db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, upsert=True, callback=update_callback)
+
+ tornado.ioloop.IOLoop.instance().start()
+ test_shunt.assert_called('update')
+
+ def query_callback(response, error):
+ logging.info(response)
+ assert isinstance(response, dict)
+ assert response['_id'] == TEST_TIMESTAMP
+ assert response['test_count'] == 5
+ test_shunt.register_called('retrieved')
+ tornado.ioloop.IOLoop.instance().stop()
+
+ db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
+ tornado.ioloop.IOLoop.instance().start()
+ test_shunt.assert_called('retrieved')
View
@@ -1,6 +1,10 @@
import logging
import sys
import os
+import unittest
+import subprocess
+import signal
+import time
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG,
format='%(asctime)s %(process)d %(filename)s %(lineno)d %(levelname)s #| %(message)s',
@@ -10,14 +14,44 @@
# there is probably a better way to do this that doesn't require magic
app_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
if app_dir not in sys.path:
- logging.info('adding %r to sys.path' % app_dir)
+ logging.debug('adding %r to sys.path' % app_dir)
sys.path.insert(0, app_dir)
import asyncmongo
# make sure we get the local asyncmongo
assert asyncmongo.__file__.startswith(app_dir)
+class MongoTest(unittest.TestCase):
+ mongo_options = [('--port', str(27017))]
+ def setUp(self):
+ """setup method that starts up mongod instances using `self.mongo_options`"""
+ self.temp_dirs = []
+ self.mongods = []
+ for options in self.mongo_options:
+ dirname = os.tempnam()
+ os.makedirs(dirname)
+ self.temp_dirs.append(dirname)
+
+ options = ['mongod', '--bind_ip', '127.0.0.1', '--oplogSize', '10', '--dbpath', dirname] + list(options)
+ logging.debug(options)
+ pipe = subprocess.Popen(options)
+ self.mongods.append(pipe)
+ logging.debug('started mongod %s' % pipe.pid)
+ logging.info('waiting for mongod to start')
+ time.sleep(3)
+
+ def tearDown(self):
+ """teardown method that cleans up child mongod instances, and removes their temporary data files"""
+ for mongod in self.mongods:
+ logging.debug('killing mongod %s' % mongod.pid)
+ os.kill(mongod.pid, signal.SIGKILL)
+ mongod.wait()
+ for dirname in self.temp_dirs:
+ logging.debug('cleaning up %s' % dirname)
+ pipe = subprocess.Popen(['rm', '-rf', dirname])
+ pipe.wait()
+
results = {}
def setup():

0 comments on commit d27388f

Please sign in to comment.