From e93a95d0cb92d91225bdf30abb8e661503fde6c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20W=C3=B3jcik?= Date: Sat, 25 Feb 2017 14:09:10 -0500 Subject: [PATCH] Test and document controlling the size of the connection pool (#1489) --- mongoengine/connection.py | 9 +++++++-- tests/test_connection.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/mongoengine/connection.py b/mongoengine/connection.py index c7c16ca3..7eae810f 100644 --- a/mongoengine/connection.py +++ b/mongoengine/connection.py @@ -51,7 +51,9 @@ def register_connection(alias, name=None, host=None, port=None, MONGODB-CR (MongoDB Challenge Response protocol) for older servers. :param is_mock: explicitly use mongomock for this connection (can also be done by using `mongomock://` as db host prefix) - :param kwargs: allow ad-hoc parameters to be passed into the pymongo driver + :param kwargs: ad-hoc parameters to be passed into the pymongo driver, + for example maxpoolsize, tz_aware, etc. See the documentation + for pymongo's `MongoClient` for a full list. .. versionchanged:: 0.10.6 - added mongomock support """ @@ -241,9 +243,12 @@ def connect(db=None, alias=DEFAULT_CONNECTION_NAME, **kwargs): running on the default port on localhost. If authentication is needed, provide username and password arguments as well. - Multiple databases are supported by using aliases. Provide a separate + Multiple databases are supported by using aliases. Provide a separate `alias` to connect to a different instance of :program:`mongod`. + See the docstring for `register_connection` for more details about all + supported kwargs. + .. versionchanged:: 0.6 - added multiple database support. """ if alias not in _connections: diff --git a/tests/test_connection.py b/tests/test_connection.py index cc75f1e7..a1d3bfb6 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -285,8 +285,7 @@ class ConnectionTest(unittest.TestCase): self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient)) def test_connection_kwargs(self): - """Ensure that connection kwargs get passed to pymongo. - """ + """Ensure that connection kwargs get passed to pymongo.""" connect('mongoenginetest', alias='t1', tz_aware=True) conn = get_connection('t1') @@ -296,6 +295,32 @@ class ConnectionTest(unittest.TestCase): conn = get_connection('t2') self.assertFalse(get_tz_awareness(conn)) + def test_connection_pool_via_kwarg(self): + """Ensure we can specify a max connection pool size using + a connection kwarg. + """ + # Use "max_pool_size" or "maxpoolsize" depending on PyMongo version + # (former was changed to the latter as described in + # https://jira.mongodb.org/browse/PYTHON-854). + # TODO remove once PyMongo < 3.0 support is dropped + if pymongo.version_tuple[0] >= 3: + pool_size_kwargs = {'maxpoolsize': 100} + else: + pool_size_kwargs = {'max_pool_size': 100} + + conn = connect('mongoenginetest', alias='max_pool_size_via_kwarg', **pool_size_kwargs) + self.assertEqual(conn.max_pool_size, 100) + + def test_connection_pool_via_uri(self): + """Ensure we can specify a max connection pool size using + an option in a connection URI. + """ + if pymongo.version_tuple[0] == 2 and pymongo.version_tuple[1] < 9: + raise SkipTest('maxpoolsize as a URI option is only supported in PyMongo v2.9+') + + conn = connect(host='mongodb://localhost/test?maxpoolsize=100', alias='max_pool_size_via_uri') + self.assertEqual(conn.max_pool_size, 100) + def test_write_concern(self): """Ensure write concern can be specified in connect() via a kwarg or as part of the connection URI.