Skip to content

Commit 8651252

Browse files
committed
Merge pull request googleapis#1474 from dhermes/bigtable-connection-open
Implementing open()/close() on HappyBase connection.
2 parents df3c4e4 + f844da4 commit 8651252

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

gcloud/bigtable/happybase/connection.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class Connection(object):
127127
parameters are specified with a value other than the defaults.
128128
"""
129129

130+
_cluster = None
131+
130132
def __init__(self, timeout=None, autoconnect=True, table_prefix=None,
131133
table_prefix_separator='_', cluster=None, **kwargs):
132134
self._handle_legacy_args(kwargs)
@@ -140,7 +142,6 @@ def __init__(self, timeout=None, autoconnect=True, table_prefix=None,
140142
'received', table_prefix_separator,
141143
type(table_prefix_separator))
142144

143-
self.autoconnect = autoconnect
144145
self.table_prefix = table_prefix
145146
self.table_prefix_separator = table_prefix_separator
146147

@@ -152,6 +153,11 @@ def __init__(self, timeout=None, autoconnect=True, table_prefix=None,
152153
'cluster is passed')
153154
self._cluster = cluster.copy()
154155

156+
if autoconnect:
157+
self.open()
158+
159+
self._initialized = True
160+
155161
@staticmethod
156162
def _handle_legacy_args(arguments_dict):
157163
"""Check legacy HappyBase arguments and warn if set.
@@ -174,3 +180,25 @@ def _handle_legacy_args(arguments_dict):
174180
if arguments_dict:
175181
unexpected_names = arguments_dict.keys()
176182
raise TypeError('Received unexpected arguments', unexpected_names)
183+
184+
def open(self):
185+
"""Open the underlying transport to Cloud Bigtable.
186+
187+
This method opens the underlying HTTP/2 gRPC connection using a
188+
:class:`.Client` bound to the :class:`.Cluster` owned by
189+
this connection.
190+
"""
191+
self._cluster._client.start()
192+
193+
def close(self):
194+
"""Close the underlying transport to Cloud Bigtable.
195+
196+
This method closes the underlying HTTP/2 gRPC connection using a
197+
:class:`.Client` bound to the :class:`.Cluster` owned by
198+
this connection.
199+
"""
200+
self._cluster._client.stop()
201+
202+
def __del__(self):
203+
if self._cluster is not None:
204+
self.close()

gcloud/bigtable/happybase/test_connection.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,23 @@ def _makeOne(self, *args, **kwargs):
8181

8282
def test_constructor_defaults(self):
8383
cluster = _Cluster() # Avoid implicit environ check.
84+
self.assertEqual(cluster._client.start_calls, 0)
8485
connection = self._makeOne(cluster=cluster)
86+
self.assertEqual(cluster._client.start_calls, 1)
87+
self.assertEqual(cluster._client.stop_calls, 0)
8588

86-
self.assertTrue(connection.autoconnect)
8789
self.assertEqual(connection._cluster, cluster)
8890
self.assertEqual(connection.table_prefix, None)
8991
self.assertEqual(connection.table_prefix_separator, '_')
9092

93+
def test_constructor_no_autoconnect(self):
94+
cluster = _Cluster() # Avoid implicit environ check.
95+
connection = self._makeOne(autoconnect=False, cluster=cluster)
96+
self.assertEqual(cluster._client.start_calls, 0)
97+
self.assertEqual(cluster._client.stop_calls, 0)
98+
self.assertEqual(connection.table_prefix, None)
99+
self.assertEqual(connection.table_prefix_separator, '_')
100+
91101
def test_constructor_missing_cluster(self):
92102
from gcloud._testing import _Monkey
93103
from gcloud.bigtable.happybase import connection as MUT
@@ -121,7 +131,6 @@ def test_constructor_explicit(self):
121131
table_prefix=table_prefix,
122132
table_prefix_separator=table_prefix_separator,
123133
cluster=cluster)
124-
self.assertFalse(connection.autoconnect)
125134
self.assertEqual(connection.table_prefix, table_prefix)
126135
self.assertEqual(connection.table_prefix_separator,
127136
table_prefix_separator)
@@ -172,6 +181,37 @@ def test_constructor_non_string_prefix_separator(self):
172181
self._makeOne(autoconnect=False,
173182
table_prefix_separator=table_prefix_separator)
174183

184+
def test_open(self):
185+
cluster = _Cluster() # Avoid implicit environ check.
186+
connection = self._makeOne(autoconnect=False, cluster=cluster)
187+
self.assertEqual(cluster._client.start_calls, 0)
188+
connection.open()
189+
self.assertEqual(cluster._client.start_calls, 1)
190+
self.assertEqual(cluster._client.stop_calls, 0)
191+
192+
def test_close(self):
193+
cluster = _Cluster() # Avoid implicit environ check.
194+
connection = self._makeOne(autoconnect=False, cluster=cluster)
195+
self.assertEqual(cluster._client.stop_calls, 0)
196+
connection.close()
197+
self.assertEqual(cluster._client.stop_calls, 1)
198+
self.assertEqual(cluster._client.start_calls, 0)
199+
200+
def test___del__with_cluster(self):
201+
cluster = _Cluster() # Avoid implicit environ check.
202+
connection = self._makeOne(autoconnect=False, cluster=cluster)
203+
self.assertEqual(cluster._client.stop_calls, 0)
204+
connection.__del__()
205+
self.assertEqual(cluster._client.stop_calls, 1)
206+
207+
def test___del__no_cluster(self):
208+
cluster = _Cluster() # Avoid implicit environ check.
209+
connection = self._makeOne(autoconnect=False, cluster=cluster)
210+
self.assertEqual(cluster._client.stop_calls, 0)
211+
del connection._cluster
212+
connection.__del__()
213+
self.assertEqual(cluster._client.stop_calls, 0)
214+
175215

176216
class _Client(object):
177217

0 commit comments

Comments
 (0)