Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions gcloud/bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ def __init__(self, project=None, credentials=None,
self.timeout_seconds = timeout_seconds

# These will be set in start().
self._data_stub = None
self._cluster_stub = None
self._operations_stub = None
self._table_stub = None
self._data_stub_internal = None
self._cluster_stub_internal = None
self._operations_stub_internal = None
self._table_stub_internal = None

@property
def credentials(self):
Expand Down Expand Up @@ -179,20 +179,20 @@ def project_name(self):
return 'projects/' + self.project

@property
def data_stub(self):
def _data_stub(self):
"""Getter for the gRPC stub used for the Data API.

:rtype: :class:`grpc.early_adopter.implementations._Stub`
:returns: A gRPC stub object.
:raises: :class:`ValueError <exceptions.ValueError>` if the current
client has not been :meth:`start`-ed.
"""
if self._data_stub is None:
if self._data_stub_internal is None:
raise ValueError('Client has not been started.')
return self._data_stub
return self._data_stub_internal

@property
def cluster_stub(self):
def _cluster_stub(self):
"""Getter for the gRPC stub used for the Cluster Admin API.

:rtype: :class:`grpc.early_adopter.implementations._Stub`
Expand All @@ -203,12 +203,12 @@ def cluster_stub(self):
"""
if not self._admin:
raise ValueError('Client is not an admin client.')
if self._cluster_stub is None:
if self._cluster_stub_internal is None:
raise ValueError('Client has not been started.')
return self._cluster_stub
return self._cluster_stub_internal

@property
def operations_stub(self):
def _operations_stub(self):
"""Getter for the gRPC stub used for the Operations API.

:rtype: :class:`grpc.early_adopter.implementations._Stub`
Expand All @@ -219,12 +219,12 @@ def operations_stub(self):
"""
if not self._admin:
raise ValueError('Client is not an admin client.')
if self._operations_stub is None:
if self._operations_stub_internal is None:
raise ValueError('Client has not been started.')
return self._operations_stub
return self._operations_stub_internal

@property
def table_stub(self):
def _table_stub(self):
"""Getter for the gRPC stub used for the Table Admin API.

:rtype: :class:`grpc.early_adopter.implementations._Stub`
Expand All @@ -235,9 +235,9 @@ def table_stub(self):
"""
if not self._admin:
raise ValueError('Client is not an admin client.')
if self._table_stub is None:
if self._table_stub_internal is None:
raise ValueError('Client has not been started.')
return self._table_stub
return self._table_stub_internal

def _make_data_stub(self):
"""Creates gRPC stub to make requests to the Data API.
Expand Down Expand Up @@ -284,7 +284,7 @@ def is_started(self):
:rtype: bool
:returns: Boolean indicating if the client has been started.
"""
return self._data_stub is not None
return self._data_stub_internal is not None

def start(self):
"""Prepare the client to make requests.
Expand All @@ -300,16 +300,16 @@ def start(self):
# connection created. We don't want to immediately close
# those connections since the client will make many
# requests with it over HTTP/2.
self._data_stub = self._make_data_stub()
self._data_stub.__enter__()
self._data_stub_internal = self._make_data_stub()
self._data_stub_internal.__enter__()
if self._admin:
self._cluster_stub = self._make_cluster_stub()
self._operations_stub = self._make_operations_stub()
self._table_stub = self._make_table_stub()
self._cluster_stub_internal = self._make_cluster_stub()
self._operations_stub_internal = self._make_operations_stub()
self._table_stub_internal = self._make_table_stub()

self._cluster_stub.__enter__()
self._operations_stub.__enter__()
self._table_stub.__enter__()
self._cluster_stub_internal.__enter__()
self._operations_stub_internal.__enter__()
self._table_stub_internal.__enter__()

def stop(self):
"""Closes all the open gRPC clients."""
Expand All @@ -318,13 +318,13 @@ def stop(self):

# When exit-ing, we pass None as the exception type, value and
# traceback to __exit__.
self._data_stub.__exit__(None, None, None)
self._data_stub_internal.__exit__(None, None, None)
if self._admin:
self._cluster_stub.__exit__(None, None, None)
self._operations_stub.__exit__(None, None, None)
self._table_stub.__exit__(None, None, None)

self._data_stub = None
self._cluster_stub = None
self._operations_stub = None
self._table_stub = None
self._cluster_stub_internal.__exit__(None, None, None)
self._operations_stub_internal.__exit__(None, None, None)
self._table_stub_internal.__exit__(None, None, None)

self._data_stub_internal = None
self._cluster_stub_internal = None
self._operations_stub_internal = None
self._table_stub_internal = None
81 changes: 41 additions & 40 deletions gcloud/bigtable/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def _constructor_test_helper(self, expected_scopes, creds,
self.assertEqual(client.timeout_seconds, timeout_seconds)
self.assertEqual(client.user_agent, user_agent)
# Check stubs are set (but null)
self.assertEqual(client._data_stub, None)
self.assertEqual(client._cluster_stub, None)
self.assertEqual(client._operations_stub, None)
self.assertEqual(client._table_stub, None)
self.assertEqual(client._data_stub_internal, None)
self.assertEqual(client._cluster_stub_internal, None)
self.assertEqual(client._operations_stub_internal, None)
self.assertEqual(client._table_stub_internal, None)

def test_constructor_default_scopes(self):
from gcloud.bigtable import client as MUT
Expand Down Expand Up @@ -121,87 +121,88 @@ def test_data_stub_getter(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials)
client._data_stub = object()
self.assertTrue(client.data_stub is client._data_stub)
client._data_stub_internal = object()
self.assertTrue(client._data_stub is client._data_stub_internal)

def test_data_stub_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials)
with self.assertRaises(ValueError):
getattr(client, 'data_stub')
getattr(client, '_data_stub')

def test_cluster_stub_getter(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
client._cluster_stub = object()
self.assertTrue(client.cluster_stub is client._cluster_stub)
client._cluster_stub_internal = object()
self.assertTrue(client._cluster_stub is client._cluster_stub_internal)

def test_cluster_stub_non_admin_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=False)
with self.assertRaises(ValueError):
getattr(client, 'cluster_stub')
getattr(client, '_cluster_stub')

def test_cluster_stub_unset_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
with self.assertRaises(ValueError):
getattr(client, 'cluster_stub')
getattr(client, '_cluster_stub')

def test_operations_stub_getter(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
client._operations_stub = object()
self.assertTrue(client.operations_stub is client._operations_stub)
client._operations_stub_internal = object()
self.assertTrue(client._operations_stub is
client._operations_stub_internal)

def test_operations_stub_non_admin_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=False)
with self.assertRaises(ValueError):
getattr(client, 'operations_stub')
getattr(client, '_operations_stub')

def test_operations_stub_unset_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
with self.assertRaises(ValueError):
getattr(client, 'operations_stub')
getattr(client, '_operations_stub')

def test_table_stub_getter(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
client._table_stub = object()
self.assertTrue(client.table_stub is client._table_stub)
client._table_stub_internal = object()
self.assertTrue(client._table_stub is client._table_stub_internal)

def test_table_stub_non_admin_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=False)
with self.assertRaises(ValueError):
getattr(client, 'table_stub')
getattr(client, '_table_stub')

def test_table_stub_unset_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
with self.assertRaises(ValueError):
getattr(client, 'table_stub')
getattr(client, '_table_stub')

def test__make_data_stub(self):
from gcloud._testing import _Monkey
Expand Down Expand Up @@ -333,9 +334,9 @@ def test_is_started(self):
client = self._makeOne(project=project, credentials=credentials)

self.assertFalse(client.is_started())
client._data_stub = object()
client._data_stub_internal = object()
self.assertTrue(client.is_started())
client._data_stub = None
client._data_stub_internal = None
self.assertFalse(client.is_started())

def _start_method_helper(self, admin):
Expand All @@ -357,17 +358,17 @@ def mock_make_stub(*args):
with _Monkey(MUT, make_stub=mock_make_stub):
client.start()

self.assertTrue(client._data_stub is stub)
self.assertTrue(client._data_stub_internal is stub)
if admin:
self.assertTrue(client._cluster_stub is stub)
self.assertTrue(client._operations_stub is stub)
self.assertTrue(client._table_stub is stub)
self.assertTrue(client._cluster_stub_internal is stub)
self.assertTrue(client._operations_stub_internal is stub)
self.assertTrue(client._table_stub_internal is stub)
self.assertEqual(stub._entered, 4)
self.assertEqual(len(make_stub_args), 4)
else:
self.assertTrue(client._cluster_stub is None)
self.assertTrue(client._operations_stub is None)
self.assertTrue(client._table_stub is None)
self.assertTrue(client._cluster_stub_internal is None)
self.assertTrue(client._operations_stub_internal is None)
self.assertTrue(client._table_stub_internal is None)
self.assertEqual(stub._entered, 1)
self.assertEqual(len(make_stub_args), 1)
self.assertEqual(stub._exited, [])
Expand All @@ -382,12 +383,12 @@ def test_start_while_started(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials)
client._data_stub = data_stub = object()
client._data_stub_internal = data_stub = object()
self.assertTrue(client.is_started())
client.start()

# Make sure the stub did not change.
self.assertEqual(client._data_stub, data_stub)
self.assertEqual(client._data_stub_internal, data_stub)

def _stop_method_helper(self, admin):
credentials = _Credentials()
Expand All @@ -397,15 +398,15 @@ def _stop_method_helper(self, admin):

stub1 = _FakeStub()
stub2 = _FakeStub()
client._data_stub = stub1
client._cluster_stub = stub2
client._operations_stub = stub2
client._table_stub = stub2
client._data_stub_internal = stub1
client._cluster_stub_internal = stub2
client._operations_stub_internal = stub2
client._table_stub_internal = stub2
client.stop()
self.assertTrue(client._data_stub is None)
self.assertTrue(client._cluster_stub is None)
self.assertTrue(client._operations_stub is None)
self.assertTrue(client._table_stub is None)
self.assertTrue(client._data_stub_internal is None)
self.assertTrue(client._cluster_stub_internal is None)
self.assertTrue(client._operations_stub_internal is None)
self.assertTrue(client._table_stub_internal is None)
self.assertEqual(stub1._entered, 0)
self.assertEqual(stub2._entered, 0)
exc_none_triple = (None, None, None)
Expand All @@ -430,10 +431,10 @@ def test_stop_while_stopped(self):
# This is a bit hacky. We set the cluster stub protected value
# since it isn't used in is_started() and make sure that stop
# doesn't reset this value to None.
client._cluster_stub = cluster_stub = object()
client._cluster_stub_internal = cluster_stub = object()
client.stop()
# Make sure the cluster stub did not change.
self.assertEqual(client._cluster_stub, cluster_stub)
self.assertEqual(client._cluster_stub_internal, cluster_stub)


class _Credentials(object):
Expand Down