diff --git a/pymongo/common.py b/pymongo/common.py index bf5919d84..bff0d7540 100644 --- a/pymongo/common.py +++ b/pymongo/common.py @@ -98,12 +98,22 @@ def validate_integer(option, value): def validate_positive_integer(option, value): - """Validate that 'value' is a positive integer. + """Validate that 'value' is a positive integer, which does not include 0. + """ + val = validate_integer(option, value) + if val <= 0: + raise ConfigurationError("The value of %s must be " + "a positive integer" % (option,)) + return val + + +def validate_non_negative_integer(option, value): + """Validate that 'value' is a positive integer or 0. """ val = validate_integer(option, value) if val < 0: raise ConfigurationError("The value of %s must be " - "a positive integer" % (option,)) + "a non negative integer" % (option,)) return val @@ -138,6 +148,14 @@ def validate_cert_reqs(option, value): % (option,)) +def validate_non_negative_integer_or_none(option, value): + """Validate that 'value' is a positive integer or 0 or None. + """ + if value is None: + return value + return validate_non_negative_integer(option, value) + + def validate_positive_integer_or_none(option, value): """Validate that 'value' is a positive integer or None. """ @@ -316,7 +334,7 @@ VALIDATORS = { 'connecttimeoutms': validate_timeout_or_none, 'sockettimeoutms': validate_timeout_or_none, 'waitqueuetimeoutms': validate_timeout_or_none, - 'waitqueuemultiple': validate_positive_integer_or_none, + 'waitqueuemultiple': validate_non_negative_integer_or_none, 'ssl': validate_boolean, 'ssl_keyfile': validate_readable, 'ssl_certfile': validate_readable, diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 41b3a8004..8f47d4c94 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -130,7 +130,7 @@ class MongoClient(common.BaseObject): - `max_pool_size` (optional): The maximum number of connections that the pool will open simultaneously. If this is set, operations will block if there are `max_pool_size` outstanding connections - from the pool. Defaults to 100. + from the pool. Defaults to 100. Cannot be 0. - `document_class` (optional): default class to use for documents returned from queries on this client - `tz_aware` (optional): if ``True``, @@ -316,8 +316,8 @@ class MongoClient(common.BaseObject): self.__max_pool_size = options.get( 'maxpoolsize', - common.validate_positive_integer_or_none('max_pool_size', - max_pool_size)) + common.validate_positive_integer_or_none( + 'max_pool_size', max_pool_size)) self.__cursor_manager = CursorManager(self) diff --git a/pymongo/mongo_replica_set_client.py b/pymongo/mongo_replica_set_client.py index 32be49edd..5ba7ef497 100644 --- a/pymongo/mongo_replica_set_client.py +++ b/pymongo/mongo_replica_set_client.py @@ -471,7 +471,7 @@ class MongoReplicaSetClient(common.BaseObject): - `max_pool_size` (optional): The maximum number of connections each pool will open simultaneously. If this is set, operations will block if there are `max_pool_size` outstanding connections - from the pool. Defaults to 100. + from the pool. Defaults to 100. Cannot be 0. - `document_class` (optional): default class to use for documents returned from queries on this client - `tz_aware` (optional): if ``True``, @@ -635,8 +635,8 @@ class MongoReplicaSetClient(common.BaseObject): self.__max_pool_size = self.__opts.get( 'maxpoolsize', - common.validate_positive_integer_or_none('max_pool_size', - max_pool_size)) + common.validate_positive_integer_or_none( + 'max_pool_size', max_pool_size)) common.validate_boolean('tz_aware', tz_aware) uuid_representation = options.pop('uuidrepresentation', PYTHON_LEGACY) diff --git a/test/test_client.py b/test/test_client.py index 2f4f2fe2a..69089059e 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -106,6 +106,9 @@ class TestClient(unittest.TestCase, TestRequestMixin): self.assertRaises(ConfigurationError, MongoClient, []) + def test_max_pool_size_zero(self): + self.assertRaises(ConfigurationError, MongoClient, maxPoolSize=0) + def test_constants(self): MongoClient.HOST = host MongoClient.PORT = port diff --git a/test/test_replica_set_client.py b/test/test_replica_set_client.py index cb63fd78e..a3f07b9e5 100644 --- a/test/test_replica_set_client.py +++ b/test/test_replica_set_client.py @@ -165,6 +165,10 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin): self.assertEqual(None, client._MongoReplicaSetClient__ssl_certfile) self.assertEqual(None, client._MongoReplicaSetClient__ssl_ca_certs) + def test_repl_max_pool_size_zero(self): + self.assertRaises(ConfigurationError, MongoReplicaSetClient, + maxPoolSize=0) + def test_init_disconnected(self): c = self._get_client(_connect=False)