5151if 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 )
5658else :
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 ):
0 commit comments