Permalink
Browse files

Allow a defaults argument to be passed into create_cluster

  • Loading branch information...
1 parent 4f9a81e commit 1bd1406b57ecb30493ccf1ad584430d8b5250ac6 @dcramer dcramer committed May 18, 2012
Showing with 71 additions and 15 deletions.
  1. +4 −0 README.rst
  2. +30 −10 nydus/db/base.py
  3. +37 −5 tests/nydus/db/connections/tests.py
View
@@ -10,6 +10,10 @@ The following example creates a Redis connection cluster which will distribute r
>>> redis = create_cluster({
>>> 'engine': 'nydus.db.backends.redis.Redis',
>>> 'router': 'nydus.db.routers.keyvalue.PartitionRouter',
+ >>> 'defaults': {
+ >>> 'host': 'localhost',
+ >>> 'port': 6379,
+ >>> },
>>> 'hosts': {
>>> 0: {'db': 0},
>>> 1: {'db': 1},
View
@@ -6,22 +6,40 @@
:license: Apache License 2.0, see LICENSE for more details.
"""
+__all__ = ('LazyConnectionHandler', 'create_cluster', 'Cluster')
+
from collections import defaultdict
from nydus.db.routers import BaseRouter, routing_params
from nydus.utils import import_string, ThreadPool
+def apply_defaults(host, defaults):
+ for key, value in defaults.iteritems():
+ if key not in host:
+ host[key] = value
+ return host
+
+
def create_cluster(settings):
"""
- redis = create_cluster({
- 'engine': 'nydus.db.backends.redis.Redis',
- 'router': 'nydus.db.routers.redis.PartitionRouter',
- 'hosts': {
- 0: {'db': 0},
- 1: {'db': 1},
- 2: {'db': 2},
- }
- })
+ Creates a new Nydus cluster from the given settings.
+
+ :param settings: Dictionary of the cluster settings.
+ :returns: Configured instance of ``nydus.db.base.Cluster``.
+
+ >>> redis = create_cluster({
+ >>> 'engine': 'nydus.db.backends.redis.Redis',
+ >>> 'router': 'nydus.db.routers.redis.PartitionRouter',
+ >>> 'defaults': {
+ >>> 'host': 'localhost',
+ >>> 'port': 6379,
+ >>> },
+ >>> 'hosts': {
+ >>> 0: {'db': 0},
+ >>> 1: {'db': 1},
+ >>> 2: {'db': 2},
+ >>> }
+ >>> })
"""
# Pull in our client
if isinstance(settings['engine'], basestring):
@@ -38,11 +56,13 @@ def create_cluster(settings):
else:
router = BaseRouter
+ defaults = settings.get('defaults', {})
+
# Build the connection cluster
return Cluster(
router=router,
hosts=dict(
- (conn_number, Conn(num=conn_number, **host_settings))
+ (conn_number, Conn(num=conn_number, **apply_defaults(host_settings, defaults)))
for conn_number, host_settings
in settings['hosts'].iteritems()
),
@@ -7,9 +7,9 @@
"""
from __future__ import absolute_import
-from mock import Mock
+import mock
-from nydus.db.base import Cluster, create_cluster, EventualCommand
+from nydus.db.base import Cluster, create_cluster, EventualCommand, apply_defaults
from nydus.db.routers.base import BaseRouter
from nydus.db.routers.keyvalue import get_key
from nydus.db.backends.base import BaseConnection
@@ -34,8 +34,8 @@ def get_dbs(self, attr, args, kwargs, **fkwargs):
return [0]
-class ClusterTest(BaseTest):
- def test_create_cluster(self):
+class CreateClusterTest(BaseTest):
+ def test_creates_cluster(self):
c = create_cluster({
'engine': DummyConnection,
'router': DummyRouter,
@@ -45,6 +45,19 @@ def test_create_cluster(self):
})
self.assertEquals(len(c), 1)
+ @mock.patch('nydus.db.base.apply_defaults')
+ def test_does_call_apply_defaults(self, apply_defaults):
+ create_cluster({
+ 'engine': DummyConnection,
+ 'defaults': {'foo': 'baz'},
+ 'hosts': {
+ 0: {'resp': 'bar'},
+ }
+ })
+ apply_defaults.assert_called_once_with({'resp': 'bar'}, {'foo': 'baz'})
+
+
+class ClusterTest(BaseTest):
def test_init(self):
c = Cluster(
hosts={0: BaseConnection(num=1)},
@@ -59,7 +72,7 @@ def test_proxy(self):
self.assertEquals(p.foo(), 'bar')
def test_disconnect(self):
- c = Mock()
+ c = mock.Mock()
p = Cluster(
hosts={0: c},
)
@@ -232,3 +245,22 @@ def test_evaled_unicode(self):
ec._set_value('biz')
self.assertEquals(unicode(ec), u'biz')
+
+
+class ApplyDefaultsTest(BaseTest):
+ def test_does_apply(self):
+ host = {'port': 6379}
+ defaults = {'host': 'localhost'}
+ results = apply_defaults(host, defaults)
+ self.assertEquals(results, {
+ 'port': 6379,
+ 'host': 'localhost',
+ })
+
+ def test_does_not_overwrite(self):
+ host = {'port': 6379}
+ defaults = {'port': 9000}
+ results = apply_defaults(host, defaults)
+ self.assertEquals(results, {
+ 'port': 6379,
+ })

0 comments on commit 1bd1406

Please sign in to comment.