Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 4e13783

Browse files
committed
fixed tests
1 parent e3ec02b commit 4e13783

5 files changed

Lines changed: 59 additions & 43 deletions

File tree

google/cloud/bigtable/data/_async/_replaceable_channel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
13-
# limitations under the License
13+
# limitations under the License.
1414
#
1515
from __future__ import annotations
1616

google/cloud/bigtable/data/_async/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ async def _manage_channel(
404404
else:
405405
if grace_period:
406406
self._is_closed.wait(grace_period) # type: ignore
407-
old_channel.close() # type: ignore
407+
old_channel.close() # type: ignore
408408
# subtract the time spent waiting for the channel to be replaced
409409
next_refresh = random.uniform(refresh_interval_min, refresh_interval_max)
410410
next_sleep = max(next_refresh - (time.monotonic() - start_timestamp), 0)

google/cloud/bigtable/data/_sync_autogen/_replaceable_channel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
13-
# limitations under the License
13+
# limitations under the License.
1414
#
1515

1616
# This file is automatically generated by CrossSync. Do not edit manually.

tests/unit/data/_async/test_client.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@
5151
if CrossSync.is_async:
5252
from google.api_core import grpc_helpers_async
5353
from google.cloud.bigtable.data._async.client import TableAsync
54+
from google.cloud.bigtable.data._async._replaceable_channel import _AsyncReplaceableChannel
5455

5556
CrossSync.add_mapping("grpc_helpers", grpc_helpers_async)
57+
CrossSync.add_mapping("ReplaceableChannel", _AsyncReplaceableChannel)
5658
else:
5759
from google.api_core import grpc_helpers
5860
from google.cloud.bigtable.data._sync_autogen.client import Table # noqa: F401
61+
from google.cloud.bigtable.data._sync_autogen._replaceable_channel import _ReplaceableChannel
5962

6063
CrossSync.add_mapping("grpc_helpers", grpc_helpers)
64+
CrossSync.add_mapping("ReplaceableChannel", _ReplaceableChannel)
6165

6266
__CROSS_SYNC_OUTPUT__ = "tests.unit.data._sync_autogen.test_client"
6367

@@ -223,11 +227,12 @@ async def test__start_background_channel_refresh_task_exists(self):
223227
@CrossSync.pytest
224228
async def test__start_background_channel_refresh(self):
225229
# should create background tasks for each channel
226-
client = self._make_client(project="project-id", use_emulator=False)
230+
client = self._make_client(project="project-id")
227231
with mock.patch.object(
228232
client, "_ping_and_warm_instances", CrossSync.Mock()
229233
) as ping_and_warm:
230234
client._emulator_host = None
235+
client.transport._grpc_channel = CrossSync.ReplaceableChannel(mock.Mock)
231236
client._start_background_channel_refresh()
232237
assert client._channel_refresh_task is not None
233238
assert isinstance(client._channel_refresh_task, CrossSync.Task)
@@ -366,7 +371,7 @@ async def test__manage_channel_first_sleep(
366371
with mock.patch.object(CrossSync, "event_wait") as sleep:
367372
sleep.side_effect = asyncio.CancelledError
368373
try:
369-
client = self._make_client(project="project-id", use_emulator=False)
374+
client = self._make_client(project="project-id")
370375
client._channel_init_time = -wait_time
371376
await client._manage_channel(refresh_interval, refresh_interval)
372377
except asyncio.CancelledError:
@@ -472,7 +477,7 @@ async def test__manage_channel_random(self):
472477
uniform.return_value = 0
473478
try:
474479
uniform.side_effect = asyncio.CancelledError
475-
client = self._make_client(project="project-id", use_emulator=False)
480+
client = self._make_client(project="project-id")
476481
except asyncio.CancelledError:
477482
uniform.side_effect = None
478483
uniform.reset_mock()
@@ -499,26 +504,27 @@ async def test__manage_channel_refresh(self, num_cycles):
499504
expected_refresh = 0.5
500505
grpc_lib = grpc.aio if CrossSync.is_async else grpc
501506
new_channel = grpc_lib.insecure_channel("localhost:8080")
507+
create_channel_mock = mock.Mock()
508+
create_channel_mock.return_value = new_channel
509+
refreshable_channel = CrossSync.ReplaceableChannel(create_channel_mock)
502510

503511
with mock.patch.object(CrossSync, "event_wait") as sleep:
504512
sleep.side_effect = [None for i in range(num_cycles)] + [RuntimeError]
505-
with mock.patch.object(
506-
CrossSync.grpc_helpers, "create_channel"
507-
) as create_channel:
508-
create_channel.return_value = new_channel
509-
client = self._make_client(project="project-id", use_emulator=False)
510-
create_channel.reset_mock()
511-
try:
512-
await client._manage_channel(
513-
refresh_interval_min=expected_refresh,
514-
refresh_interval_max=expected_refresh,
515-
grace_period=0,
516-
)
517-
except RuntimeError:
518-
pass
519-
assert sleep.call_count == num_cycles + 1
520-
assert create_channel.call_count == num_cycles
521-
await client.close()
513+
client = self._make_client(project="project-id")
514+
client.transport._grpc_channel = refreshable_channel
515+
create_channel_mock.reset_mock()
516+
sleep.reset_mock()
517+
try:
518+
await client._manage_channel(
519+
refresh_interval_min=expected_refresh,
520+
refresh_interval_max=expected_refresh,
521+
grace_period=0,
522+
)
523+
except RuntimeError:
524+
pass
525+
assert sleep.call_count == num_cycles + 1
526+
assert create_channel_mock.call_count == num_cycles
527+
await client.close()
522528

523529
@CrossSync.pytest
524530
async def test__register_instance(self):

tests/unit/data/_sync_autogen/test_client.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@
4545
str_val,
4646
)
4747
from google.api_core import grpc_helpers
48+
from google.cloud.bigtable.data._sync_autogen._replaceable_channel import (
49+
_ReplaceableChannel,
50+
)
4851

4952
CrossSync._Sync_Impl.add_mapping("grpc_helpers", grpc_helpers)
53+
CrossSync._Sync_Impl.add_mapping("ReplaceableChannel", _ReplaceableChannel)
5054

5155

5256
@CrossSync._Sync_Impl.add_mapping_decorator("TestBigtableDataClient")
@@ -176,11 +180,14 @@ def test__start_background_channel_refresh_task_exists(self):
176180
client.close()
177181

178182
def test__start_background_channel_refresh(self):
179-
client = self._make_client(project="project-id", use_emulator=False)
183+
client = self._make_client(project="project-id")
180184
with mock.patch.object(
181185
client, "_ping_and_warm_instances", CrossSync._Sync_Impl.Mock()
182186
) as ping_and_warm:
183187
client._emulator_host = None
188+
client.transport._grpc_channel = CrossSync._Sync_Impl.ReplaceableChannel(
189+
mock.Mock
190+
)
184191
client._start_background_channel_refresh()
185192
assert client._channel_refresh_task is not None
186193
assert isinstance(client._channel_refresh_task, CrossSync._Sync_Impl.Task)
@@ -282,7 +289,7 @@ def test__manage_channel_first_sleep(
282289
with mock.patch.object(CrossSync._Sync_Impl, "event_wait") as sleep:
283290
sleep.side_effect = asyncio.CancelledError
284291
try:
285-
client = self._make_client(project="project-id", use_emulator=False)
292+
client = self._make_client(project="project-id")
286293
client._channel_init_time = -wait_time
287294
client._manage_channel(refresh_interval, refresh_interval)
288295
except asyncio.CancelledError:
@@ -364,7 +371,7 @@ def test__manage_channel_random(self):
364371
uniform.return_value = 0
365372
try:
366373
uniform.side_effect = asyncio.CancelledError
367-
client = self._make_client(project="project-id", use_emulator=False)
374+
client = self._make_client(project="project-id")
368375
except asyncio.CancelledError:
369376
uniform.side_effect = None
370377
uniform.reset_mock()
@@ -389,25 +396,28 @@ def test__manage_channel_refresh(self, num_cycles):
389396
expected_refresh = 0.5
390397
grpc_lib = grpc.aio if CrossSync._Sync_Impl.is_async else grpc
391398
new_channel = grpc_lib.insecure_channel("localhost:8080")
399+
create_channel_mock = mock.Mock()
400+
create_channel_mock.return_value = new_channel
401+
refreshable_channel = CrossSync._Sync_Impl.ReplaceableChannel(
402+
create_channel_mock
403+
)
392404
with mock.patch.object(CrossSync._Sync_Impl, "event_wait") as sleep:
393405
sleep.side_effect = [None for i in range(num_cycles)] + [RuntimeError]
394-
with mock.patch.object(
395-
CrossSync._Sync_Impl.grpc_helpers, "create_channel"
396-
) as create_channel:
397-
create_channel.return_value = new_channel
398-
client = self._make_client(project="project-id", use_emulator=False)
399-
create_channel.reset_mock()
400-
try:
401-
client._manage_channel(
402-
refresh_interval_min=expected_refresh,
403-
refresh_interval_max=expected_refresh,
404-
grace_period=0,
405-
)
406-
except RuntimeError:
407-
pass
408-
assert sleep.call_count == num_cycles + 1
409-
assert create_channel.call_count == num_cycles
410-
client.close()
406+
client = self._make_client(project="project-id")
407+
client.transport._grpc_channel = refreshable_channel
408+
create_channel_mock.reset_mock()
409+
sleep.reset_mock()
410+
try:
411+
client._manage_channel(
412+
refresh_interval_min=expected_refresh,
413+
refresh_interval_max=expected_refresh,
414+
grace_period=0,
415+
)
416+
except RuntimeError:
417+
pass
418+
assert sleep.call_count == num_cycles + 1
419+
assert create_channel_mock.call_count == num_cycles
420+
client.close()
411421

412422
def test__register_instance(self):
413423
"""test instance registration"""

0 commit comments

Comments
 (0)