Skip to content
This repository
Browse code

Merge pull request #32 from ajdavis/close-test-sockets

Harden the unittests a little: close connections between tests, and stop the IOLoop before asserting.
  • Loading branch information...
commit 0b0d48f7c884df76dfcd09424f93999c7b0d041f 2 parents ff4d21a + 07e56f0
Jehiah Czebotar authored December 14, 2011
5  asyncmongo/pool.py
@@ -39,6 +39,9 @@ def get_connection_pool(self, pool_id, *args, **kwargs):
39 39
     @classmethod
40 40
     def close_idle_connections(self, pool_id=None):
41 41
         """close idle connections to mongo"""
  42
+        if not hasattr(self, '_pools'):
  43
+            return
  44
+
42 45
         if pool_id:
43 46
             if pool_id not in self._pools:
44 47
                 raise ProgrammingError("pool %r does not exist" % pool_id)
@@ -46,7 +49,7 @@ def close_idle_connections(self, pool_id=None):
46 49
                 pool = self._pools[pool_id]
47 50
                 pool.close()
48 51
         else:
49  
-            for pool in self._pools.items():
  52
+            for pool_id, pool in self._pools.items():
50 53
                 pool.close()
51 54
 
52 55
 class ConnectionPool(object):
8  test/test_authentication.py
@@ -21,17 +21,18 @@ def test_authentication(self):
21 21
             db = asyncmongo.Client(pool_id='testauth', host='127.0.0.1', port=27017, dbname='test', dbuser='testuser', dbpass='testpass', maxconnections=2)
22 22
         
23 23
             def update_callback(response, error):
  24
+                tornado.ioloop.IOLoop.instance().stop()
24 25
                 logging.info(response)
25 26
                 assert len(response) == 1
26 27
                 test_shunt.register_called('update')
27  
-                tornado.ioloop.IOLoop.instance().stop()
28  
-        
  28
+
29 29
             db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, upsert=True, callback=update_callback)
30 30
         
31 31
             tornado.ioloop.IOLoop.instance().start()
32 32
             test_shunt.assert_called('update')
33 33
 
34 34
             def query_callback(response, error):
  35
+                tornado.ioloop.IOLoop.instance().stop()
35 36
                 logging.info(response)
36 37
                 logging.info(error)
37 38
                 assert error is None
@@ -39,8 +40,7 @@ def query_callback(response, error):
39 40
                 assert response['_id'] == TEST_TIMESTAMP
40 41
                 assert response['test_count'] == 1
41 42
                 test_shunt.register_called('retrieved')
42  
-                tornado.ioloop.IOLoop.instance().stop()
43  
-        
  43
+
44 44
             db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
45 45
             tornado.ioloop.IOLoop.instance().start()
46 46
             test_shunt.assert_called('retrieved')
8  test/test_connection.py
@@ -14,21 +14,21 @@ def test_query(self):
14 14
         db = asyncmongo.Client(pool_id='test_query', host='127.0.0.1', port=27017, dbname='test', mincached=3)
15 15
         
16 16
         def insert_callback(response, error):
  17
+            tornado.ioloop.IOLoop.instance().stop()
17 18
             logging.info(response)
18 19
             assert len(response) == 1
19 20
             test_shunt.register_called('inserted')
20  
-            tornado.ioloop.IOLoop.instance().stop()
21  
-        
  21
+
22 22
         db.test_users.insert({"_id" : "test_connection.%d" % TEST_TIMESTAMP}, safe=True, callback=insert_callback)
23 23
         
24 24
         tornado.ioloop.IOLoop.instance().start()
25 25
         test_shunt.assert_called('inserted')
26 26
         
27 27
         def callback(response, error):
  28
+            tornado.ioloop.IOLoop.instance().stop()
28 29
             assert len(response) == 1
29 30
             test_shunt.register_called('got_record')
30  
-            tornado.ioloop.IOLoop.instance().stop()
31  
-        
  31
+
32 32
         db.test_users.find({}, limit=1, callback=callback)
33 33
         
34 34
         tornado.ioloop.IOLoop.instance().start()
8  test/test_duplicate_insert.py
@@ -13,22 +13,22 @@ def test_duplicate_insert(self):
13 13
         db = asyncmongo.Client(pool_id='dup_insert', host='127.0.0.1', port=27017, dbname='test')
14 14
         
15 15
         def insert_callback(response, error):
  16
+            tornado.ioloop.IOLoop.instance().stop()
16 17
             logging.info(response)
17 18
             assert len(response) == 1
18 19
             test_shunt.register_called('inserted')
19  
-            tornado.ioloop.IOLoop.instance().stop()
20  
-        
  20
+
21 21
         db.test_users.insert({"_id" : "duplicate_insert.%d" % TEST_TIMESTAMP}, callback=insert_callback)
22 22
         
23 23
         tornado.ioloop.IOLoop.instance().start()
24 24
         test_shunt.assert_called('inserted')
25 25
         
26 26
         def duplicate_callback(response, error):
  27
+            tornado.ioloop.IOLoop.instance().stop()
27 28
             logging.info(response)
28 29
             if error:
29 30
                 test_shunt.register_called('dupe')
30  
-            tornado.ioloop.IOLoop.instance().stop()
31  
-        
  31
+
32 32
         db.test_users.insert({"_id" : "duplicate_insert.%d" % TEST_TIMESTAMP}, callback=duplicate_callback)
33 33
         
34 34
         tornado.ioloop.IOLoop.instance().start()
12  test/test_insert_delete.py
@@ -13,33 +13,33 @@ def test_insert(self):
13 13
         db = asyncmongo.Client(pool_id='testinsert', host='127.0.0.1', port=27017, dbname='test')
14 14
         
15 15
         def insert_callback(response, error):
  16
+            tornado.ioloop.IOLoop.instance().stop()
16 17
             logging.info(response)
17 18
             assert len(response) == 1
18 19
             test_shunt.register_called('inserted')
19  
-            tornado.ioloop.IOLoop.instance().stop()
20  
-        
  20
+
21 21
         db.test_users.insert({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=insert_callback)
22 22
         
23 23
         tornado.ioloop.IOLoop.instance().start()
24 24
         test_shunt.assert_called('inserted')
25 25
         
26 26
         def query_callback(response, error):
  27
+            tornado.ioloop.IOLoop.instance().stop()
27 28
             logging.info(response)
28 29
             assert len(response) == 1
29 30
             test_shunt.register_called('retrieved')
30  
-            tornado.ioloop.IOLoop.instance().stop()
31  
-        
  31
+
32 32
         db.test_users.find_one({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=query_callback)
33 33
         tornado.ioloop.IOLoop.instance().start()
34 34
         test_shunt.assert_called('retrieved')
35 35
 
36 36
         
37 37
         def delete_callback(response, error):
  38
+            tornado.ioloop.IOLoop.instance().stop()
38 39
             logging.info(response)
39 40
             assert len(response) == 1
40 41
             test_shunt.register_called('deleted')
41  
-            tornado.ioloop.IOLoop.instance().stop()
42  
-        
  42
+
43 43
         db.test_users.remove({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=delete_callback)
44 44
         tornado.ioloop.IOLoop.instance().start()
45 45
         test_shunt.assert_called('deleted')
17  test/test_pooled_db.py
@@ -18,30 +18,29 @@ def test_pooled_db(self):
18 18
         test_users_collection = client.connection('test_users')
19 19
         
20 20
         def insert_callback(response, error):
  21
+            tornado.ioloop.IOLoop.instance().stop()
21 22
             logging.info(response)
22 23
             assert len(response) == 1
23 24
             test_shunt.register_called('inserted')
24  
-            tornado.ioloop.IOLoop.instance().stop()
25  
-        
  25
+
26 26
         test_users_collection.insert({"_id" : "record_test.%d" % TEST_TIMESTAMP}, safe=True, callback=insert_callback)
27 27
         
28 28
         tornado.ioloop.IOLoop.instance().start()
29 29
         test_shunt.assert_called('inserted')
30 30
         
31 31
         def pool_callback(response, error):
32  
-            assert len(response) == 1
33  
-            test_shunt.register_called('pool1')
34 32
             if test_shunt.is_called('pool2'):
35 33
                 tornado.ioloop.IOLoop.instance().stop()
36  
-        
37  
-        def pool_callback2(response, error):
38 34
             assert len(response) == 1
39  
-            test_shunt.register_called('pool2')
40  
-            
  35
+            test_shunt.register_called('pool1')
  36
+
  37
+        def pool_callback2(response, error):
41 38
             if test_shunt.is_called('pool1'):
42 39
                 # don't expect 2 finishes second
43 40
                 tornado.ioloop.IOLoop.instance().stop()
44  
-        
  41
+            assert len(response) == 1
  42
+            test_shunt.register_called('pool2')
  43
+
45 44
         test_users_collection.find({}, limit=1, callback=pool_callback)
46 45
         test_users_collection.find({}, limit=1, callback=pool_callback2)
47 46
         
8  test/test_safe_updates.py
@@ -13,11 +13,11 @@ def test_update_safe(self):
13 13
         db = asyncmongo.Client(pool_id='testinsert', host='127.0.0.1', port=27017, dbname='test', maxconnections=2)
14 14
         
15 15
         def update_callback(response, error):
  16
+            tornado.ioloop.IOLoop.instance().stop()
16 17
             logging.info(response)
17 18
             assert len(response) == 1
18 19
             test_shunt.register_called('update')
19  
-            tornado.ioloop.IOLoop.instance().stop()
20  
-        
  20
+
21 21
         # all of these should be called, but only one should have a callback
22 22
         # we also are checking that connections in the pool never increases >1 with max_connections=2
23 23
         # this is because connections for safe=False calls get put back in the pool immediated
@@ -31,13 +31,13 @@ def update_callback(response, error):
31 31
         test_shunt.assert_called('update')
32 32
         
33 33
         def query_callback(response, error):
  34
+            tornado.ioloop.IOLoop.instance().stop()
34 35
             logging.info(response)
35 36
             assert isinstance(response, dict)
36 37
             assert response['_id'] == TEST_TIMESTAMP
37 38
             assert response['test_count'] == 5
38 39
             test_shunt.register_called('retrieved')
39  
-            tornado.ioloop.IOLoop.instance().stop()
40  
-        
  40
+
41 41
         db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
42 42
         tornado.ioloop.IOLoop.instance().start()
43 43
         test_shunt.assert_called('retrieved')
2  test/test_shunt.py
@@ -18,6 +18,7 @@
18 18
     sys.path.insert(0, app_dir)
19 19
 
20 20
 import asyncmongo
  21
+import asyncmongo.pool
21 22
 # make sure we get the local asyncmongo
22 23
 assert asyncmongo.__file__.startswith(app_dir)
23 24
 
@@ -41,6 +42,7 @@ def setUp(self):
41 42
         sleep_time = 1 + (len(self.mongods) * 2)
42 43
         logging.info('waiting for mongod to start (sleeping %d seconds)' % sleep_time)
43 44
         time.sleep(sleep_time)
  45
+        asyncmongo.pool.ConnectionPools.close_idle_connections()
44 46
     
45 47
     def tearDown(self):
46 48
         """teardown method that cleans up child mongod instances, and removes their temporary data files"""
8  test/test_slave_only.py
@@ -21,11 +21,11 @@ def test_query_slave(self):
21 21
             time.sleep(4)
22 22
         
23 23
             def update_callback(response, error):
  24
+                tornado.ioloop.IOLoop.instance().stop()
24 25
                 logging.info(response)
25 26
                 assert len(response) == 1
26 27
                 test_shunt.register_called('update')
27  
-                tornado.ioloop.IOLoop.instance().stop()
28  
-        
  28
+
29 29
             masterdb.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, upsert=True, callback=update_callback)
30 30
         
31 31
             tornado.ioloop.IOLoop.instance().start()
@@ -35,6 +35,7 @@ def update_callback(response, error):
35 35
             time.sleep(2.5)
36 36
         
37 37
             def query_callback(response, error):
  38
+                tornado.ioloop.IOLoop.instance().stop()
38 39
                 logging.info(response)
39 40
                 logging.info(error)
40 41
                 assert error is None
@@ -42,8 +43,7 @@ def query_callback(response, error):
42 43
                 assert response['_id'] == TEST_TIMESTAMP
43 44
                 assert response['test_count'] == 1
44 45
                 test_shunt.register_called('retrieved')
45  
-                tornado.ioloop.IOLoop.instance().stop()
46  
-        
  46
+
47 47
             slavedb.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
48 48
             tornado.ioloop.IOLoop.instance().start()
49 49
             test_shunt.assert_called('retrieved')

0 notes on commit 0b0d48f

Please sign in to comment.
Something went wrong with that request. Please try again.