Skip to content

Commit

Permalink
Replacing client with cluster in HappyBase connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Sep 5, 2015
1 parent 86091f0 commit 5de060c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
26 changes: 13 additions & 13 deletions gcloud_bigtable/happybase/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ class Connection(object):
HappyBase, but irrelevant for Cloud Bigtable since the
protocol is fixed.
:type client: :class:`.Client`
:param client: (Optional) A client for making gRPC requests to the
Cloud Bigtable API. If not passed in, defaults to creating
client with ``admin=True`` and using the ``timeout`` for
the ``timeout_seconds``. The credentials for the client
will be the implicit ones loaded from the environment.
:type cluster: :class:`gcloud_bigtable.cluster.Cluster`
:param cluster: (Optional) A Cloud Bigtable cluster. The instance also
owns a client for making gRPC requests to the Cloud
Bigtable API. If not passed in, defaults to creating client
with ``admin=True`` and using the ``timeout`` for the
``timeout_seconds``. The credentials for the client
will be the implicit ones loaded from the environment.
Then that client is used to retrieve all the clusters
owned by the client's project.
:raises: :class:`ValueError <exceptions.ValueError>` if any of the unused
parameters are specified with a value other than the defaults.
Expand All @@ -126,7 +129,7 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, timeout=None,
autoconnect=True, table_prefix=None,
table_prefix_separator='_', compat=DEFAULT_COMPAT,
transport=DEFAULT_TRANSPORT, protocol=DEFAULT_PROTOCOL,
client=None):
cluster=None):
if host is not DEFAULT_HOST:
raise ValueError('Host cannot be set for gcloud HappyBase module')
if port is not DEFAULT_PORT:
Expand Down Expand Up @@ -154,12 +157,9 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, timeout=None,
self.table_prefix = table_prefix
self.table_prefix_separator = table_prefix_separator

self._client = client
if self._client is None:
client_kwargs = {'admin': True}
if timeout is not None:
client_kwargs['timeout_seconds'] = timeout / 1000.0
self._client = Client(**client_kwargs)
self._cluster = cluster
if self._cluster is None:
self._cluster = _get_cluster(timeout=timeout)

if autoconnect:
self.open()
Expand Down
29 changes: 11 additions & 18 deletions gcloud_bigtable/happybase/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def _getTargetClass(self):
return Connection

def _makeOne(self, *args, **kwargs):
if 'client' not in kwargs:
kwargs['client'] = object()
if 'cluster' not in kwargs:
kwargs['cluster'] = object()
return self._getTargetClass()(*args, **kwargs)

def test_constructor_defaults(self):
Expand All @@ -90,29 +90,22 @@ def test_constructor_no_autoconnect(self):
self.assertEqual(connection.table_prefix, None)
self.assertEqual(connection.table_prefix_separator, '_')

def _constructor_missing_client_helper(self, timeout=None):
def test_constructor_missing_cluster(self):
from gcloud_bigtable._testing import _MockCalled
from gcloud_bigtable._testing import _Monkey
from gcloud_bigtable.happybase import connection as MUT

with _Monkey(MUT, Client=_Client):
connection = self._makeOne(autoconnect=False, client=None,
cluster = object()
timeout = object()
mock_get_cluster = _MockCalled(cluster)
with _Monkey(MUT, _get_cluster=mock_get_cluster):
connection = self._makeOne(autoconnect=False, cluster=None,
timeout=timeout)
self.assertEqual(connection.table_prefix, None)
self.assertEqual(connection.table_prefix_separator, '_')
client = connection._client
self.assertTrue(isinstance(client, _Client))
self.assertEqual(client.args, ())
expected_kwargs = {'admin': True}
if timeout is not None:
expected_kwargs['timeout_seconds'] = timeout / 1000.0

self.assertEqual(client.kwargs, expected_kwargs)

def test_constructor_missing_client(self):
self._constructor_missing_client_helper()
self.assertEqual(connection._cluster, cluster)

def test_constructor_missing_client_with_timeout(self):
self._constructor_missing_client_helper(timeout=2103)
mock_get_cluster.check_called(self, [()], [{'timeout': timeout}])

def test_constructor_explicit(self):
timeout = object()
Expand Down
14 changes: 7 additions & 7 deletions gcloud_bigtable/happybase/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def _getTargetClass(self):
return ConnectionPool

def _makeOne(self, *args, **kwargs):
if 'client' not in kwargs:
kwargs['client'] = object()
if 'cluster' not in kwargs:
kwargs['cluster'] = object()
return self._getTargetClass()(*args, **kwargs)

def test_constructor_defaults(self):
Expand Down Expand Up @@ -73,20 +73,20 @@ def open(self):
self._open_called = True

# First make sure the custom Connection class does as expected.
client = object()
connection = ConnectionWithOpen(autoconnect=False, client=client)
cluster = object()
connection = ConnectionWithOpen(autoconnect=False, cluster=cluster)
self.assertFalse(connection._open_called)
connection = ConnectionWithOpen(autoconnect=True, client=client)
connection = ConnectionWithOpen(autoconnect=True, cluster=cluster)
self.assertTrue(connection._open_called)

# Then make sure autoconnect=True is ignored in a pool.
size = 1
with _Monkey(MUT, Connection=ConnectionWithOpen):
pool = self._makeOne(size, autoconnect=True, client=client)
pool = self._makeOne(size, autoconnect=True, cluster=cluster)

for connection in pool._queue.queue:
self.assertTrue(isinstance(connection, ConnectionWithOpen))
self.assertTrue(connection._client is client)
self.assertTrue(connection._cluster is cluster)
self.assertFalse(connection._open_called)

def test_constructor_non_integer_size(self):
Expand Down

0 comments on commit 5de060c

Please sign in to comment.