@@ -170,6 +170,7 @@ def test_constructor_both_admin_and_read_only(self):
170170
171171 def test_constructor_with_emulator_host (self ):
172172 from google .cloud .environment_vars import BIGTABLE_EMULATOR
173+ from google .cloud .bigtable .client import _GRPC_CHANNEL_OPTIONS
173174
174175 credentials = _make_credentials ()
175176 emulator_host = "localhost:8081"
@@ -183,13 +184,9 @@ def test_constructor_with_emulator_host(self):
183184 client .table_data_client
184185
185186 self .assertEqual (client ._emulator_host , emulator_host )
186- options = {
187- "grpc.max_send_message_length" : - 1 ,
188- "grpc.max_receive_message_length" : - 1 ,
189- "grpc.keepalive_time_ms" : 30000 ,
190- "grpc.keepalive_timeout_ms" : 10000 ,
191- }.items ()
192- factory .assert_called_once_with (emulator_host , credentials , options = options )
187+ factory .assert_called_once_with (
188+ emulator_host , credentials , options = _GRPC_CHANNEL_OPTIONS ,
189+ )
193190
194191 def test__get_scopes_default (self ):
195192 from google .cloud .bigtable .client import DATA_SCOPE
@@ -215,6 +212,140 @@ def test__get_scopes_read_only(self):
215212 )
216213 self .assertEqual (client ._get_scopes (), (READ_ONLY_SCOPE ,))
217214
215+ def test__emulator_channel_sync (self ):
216+ emulator_host = "localhost:8081"
217+ transport_name = "GrpcTransportTesting"
218+ transport = mock .Mock (spec = ["__name__" ], __name__ = transport_name )
219+ options = mock .Mock (spec = [])
220+ client = self ._make_one (
221+ project = self .PROJECT , credentials = _make_credentials (), read_only = True
222+ )
223+ client ._emulator_host = emulator_host
224+ lcc = client ._local_composite_credentials = mock .Mock (spec = [])
225+
226+ with mock .patch ("grpc.secure_channel" ) as patched :
227+ channel = client ._emulator_channel (transport , options )
228+
229+ assert channel is patched .return_value
230+ patched .assert_called_once_with (
231+ emulator_host , lcc .return_value , options = options ,
232+ )
233+
234+ def test__emulator_channel_async (self ):
235+ emulator_host = "localhost:8081"
236+ transport_name = "GrpcAsyncIOTransportTesting"
237+ transport = mock .Mock (spec = ["__name__" ], __name__ = transport_name )
238+ options = mock .Mock (spec = [])
239+ client = self ._make_one (
240+ project = self .PROJECT , credentials = _make_credentials (), read_only = True
241+ )
242+ client ._emulator_host = emulator_host
243+ lcc = client ._local_composite_credentials = mock .Mock (spec = [])
244+
245+ with mock .patch ("grpc.aio.secure_channel" ) as patched :
246+ channel = client ._emulator_channel (transport , options )
247+
248+ assert channel is patched .return_value
249+ patched .assert_called_once_with (
250+ emulator_host , lcc .return_value , options = options ,
251+ )
252+
253+ def test__local_composite_credentials (self ):
254+ client = self ._make_one (
255+ project = self .PROJECT , credentials = _make_credentials (), read_only = True
256+ )
257+
258+ wsir_patch = mock .patch ("google.auth.credentials.with_scopes_if_required" )
259+ request_patch = mock .patch ("google.auth.transport.requests.Request" )
260+ amp_patch = mock .patch ("google.auth.transport.grpc.AuthMetadataPlugin" )
261+ grpc_patches = mock .patch .multiple (
262+ "grpc" ,
263+ metadata_call_credentials = mock .DEFAULT ,
264+ local_channel_credentials = mock .DEFAULT ,
265+ composite_channel_credentials = mock .DEFAULT ,
266+ )
267+ with wsir_patch as wsir_patched :
268+ with request_patch as request_patched :
269+ with amp_patch as amp_patched :
270+ with grpc_patches as grpc_patched :
271+ credentials = client ._local_composite_credentials ()
272+
273+ grpc_mcc = grpc_patched ["metadata_call_credentials" ]
274+ grpc_lcc = grpc_patched ["local_channel_credentials" ]
275+ grpc_ccc = grpc_patched ["composite_channel_credentials" ]
276+
277+ self .assertIs (credentials , grpc_ccc .return_value )
278+
279+ wsir_patched .assert_called_once_with (client ._credentials , None )
280+ request_patched .assert_called_once_with ()
281+ amp_patched .assert_called_once_with (
282+ wsir_patched .return_value , request_patched .return_value ,
283+ )
284+ grpc_mcc .assert_called_once_with (amp_patched .return_value )
285+ grpc_lcc .assert_called_once_with ()
286+ grpc_ccc .assert_called_once_with (grpc_lcc .return_value , grpc_mcc .return_value )
287+
288+ def _create_gapic_client_channel_helper (
289+ self , endpoint = None , emulator_host = None ,
290+ ):
291+ from google .cloud .bigtable .client import _GRPC_CHANNEL_OPTIONS
292+
293+ client_class = mock .Mock (spec = ["DEFAULT_ENDPOINT" ])
294+ credentials = _make_credentials ()
295+ client = self ._make_one (project = self .PROJECT , credentials = credentials )
296+
297+ if endpoint is not None :
298+ client ._client_options = mock .Mock (
299+ spec = ["api_endpoint" ], api_endpoint = endpoint ,
300+ )
301+ expected_host = endpoint
302+ else :
303+ expected_host = client_class .DEFAULT_ENDPOINT
304+
305+ if emulator_host is not None :
306+ client ._emulator_host = emulator_host
307+ client ._emulator_channel = mock .Mock (spec = [])
308+ expected_host = emulator_host
309+
310+ grpc_transport = mock .Mock (spec = ["create_channel" ])
311+
312+ transport = client ._create_gapic_client_channel (client_class , grpc_transport )
313+
314+ self .assertIs (transport , grpc_transport .return_value )
315+
316+ if emulator_host is not None :
317+ client ._emulator_channel .assert_called_once_with (
318+ transport = grpc_transport , options = _GRPC_CHANNEL_OPTIONS ,
319+ )
320+ grpc_transport .assert_called_once_with (
321+ channel = client ._emulator_channel .return_value , host = expected_host ,
322+ )
323+ else :
324+ grpc_transport .create_channel .assert_called_once_with (
325+ host = expected_host ,
326+ credentials = client ._credentials ,
327+ options = _GRPC_CHANNEL_OPTIONS ,
328+ )
329+ grpc_transport .assert_called_once_with (
330+ channel = grpc_transport .create_channel .return_value , host = expected_host ,
331+ )
332+
333+ def test__create_gapic_client_channel_w_defaults (self ):
334+ self ._create_gapic_client_channel_helper ()
335+
336+ def test__create_gapic_client_channel_w_endpoint (self ):
337+ endpoint = "api.example.com"
338+ self ._create_gapic_client_channel_helper (endpoint = endpoint )
339+
340+ def test__create_gapic_client_channel_w_emulator_host (self ):
341+ host = "api.example.com:1234"
342+ self ._create_gapic_client_channel_helper (emulator_host = host )
343+
344+ def test__create_gapic_client_channel_w_endpoint_w_emulator_host (self ):
345+ endpoint = "api.example.com"
346+ host = "other.example.com:1234"
347+ self ._create_gapic_client_channel_helper (endpoint = endpoint , emulator_host = host )
348+
218349 def test_project_path_property (self ):
219350 credentials = _make_credentials ()
220351 project = "PROJECT"
0 commit comments