make Connection paramater defaults configurable with class attributes: thanks to kless for the original implementation of this

This commit is contained in:
Mike Dirolf 2009-05-07 12:27:08 -04:00
parent a4f70292e3
commit 74bf4122e0
3 changed files with 47 additions and 9 deletions

View File

@ -37,8 +37,13 @@ _CONNECT_TIMEOUT = 20.0
class Connection(object):
"""A connection to Mongo.
"""
def __init__(self, host="localhost", port=27017,
pool_size=1, auto_start_request=True, _connect=True):
HOST = "localhost"
PORT = 27017
POOL_SIZE = 1
AUTO_START_REQUEST = True
def __init__(self, host=None, port=None, pool_size=None,
auto_start_request=None, _connect=True):
"""Open a new connection to a Mongo instance at host:port.
The resultant connection object has connection-pooling built in. It also
@ -64,6 +69,15 @@ class Connection(object):
- `auto_start_request` (optional): automatically start a request
on every operation - see documentation for `start_request`.
"""
if host is None:
host = self.HOST
if port is None:
port = self.PORT
if pool_size is None:
pool_size = self.POOL_SIZE
if auto_start_request is None:
auto_start_request = self.AUTO_START_REQUEST
if not isinstance(host, types.StringType):
raise TypeError("host must be an instance of str")
if not isinstance(port, types.IntType):
@ -117,8 +131,8 @@ class Connection(object):
self.__find_master()
def paired(cls, left, right=("localhost", 27017),
pool_size=1, auto_start_request=True):
def paired(cls, left, right=None,
pool_size=None, auto_start_request=None):
"""Open a new paired connection to Mongo.
Raises TypeError if either `left` or `right` is not a tuple of the form
@ -130,6 +144,13 @@ class Connection(object):
- `pool_size` (optional): same as argument to `__init__`
- `auto_start_request` (optional): same as argument to `__init__`
"""
if right is None:
right = (self.HOST, self.PORT)
if pool_size is None:
pool_size = self.POOL_SIZE
if auto_start_request is None:
auto_start_request = self.AUTO_START_REQUEST
connection = cls(left[0], left[1], pool_size, auto_start_request,
_connect=False)
connection.__pair_with(*right)
@ -155,7 +176,7 @@ class Connection(object):
else:
strings = result["remote"].split(":", 1)
if len(strings) == 1:
port = 27017
port = self.PORT
else:
port = int(strings[1])
return (strings[0], port)

View File

@ -36,13 +36,24 @@ class TestConnection(unittest.TestCase):
def test_types(self):
self.assertRaises(TypeError, Connection, 1)
self.assertRaises(TypeError, Connection, 1.14)
self.assertRaises(TypeError, Connection, None)
self.assertRaises(TypeError, Connection, [])
self.assertRaises(TypeError, Connection, "localhost", "27017")
self.assertRaises(TypeError, Connection, "localhost", 1.14)
self.assertRaises(TypeError, Connection, "localhost", None)
self.assertRaises(TypeError, Connection, "localhost", [])
def test_constants(self):
Connection.HOST = self.host
Connection.PORT = self.port
self.assert_(Connection())
Connection.HOST = "somedomainthatdoesntexist.org"
Connection.PORT = 123456789
self.assertRaises(ConnectionFailure, Connection)
Connection.HOST = self.host
Connection.PORT = self.port
self.assert_(Connection())
def test_connect(self):
self.assertRaises(ConnectionFailure, Connection, "somedomainthatdoesntexist.org")
self.assertRaises(ConnectionFailure, Connection, self.host, 123456789)

View File

@ -40,8 +40,6 @@ class TestPooling(unittest.TestCase):
self.no_auto_pooled_db = no_auto_pooled_connection["pymongo_test"]
def test_exceptions(self):
self.assertRaises(TypeError, Connection, self.host, self.port,
pool_size=None)
self.assertRaises(TypeError, Connection, self.host, self.port,
pool_size="one")
self.assertRaises(TypeError, Connection, self.host, self.port,
@ -51,6 +49,14 @@ class TestPooling(unittest.TestCase):
self.assertRaises(ValueError, Connection, self.host, self.port,
pool_size=0)
def test_constants(self):
Connection.POOL_SIZE = -1
self.assertRaises(ValueError, Connection, self.host, self.port)
Connection.POOL_SIZE = 1
self.assert_(Connection(self.host, self.port))
# NOTE this test is non-deterministic
def test_end_request(self):
count = 0