|
56 | 56 |
|
57 | 57 |
|
58 | 58 | CREATE_INSTANCE = os.getenv("GOOGLE_CLOUD_TESTS_CREATE_SPANNER_INSTANCE") is not None |
| 59 | +USE_RESOURCE_ROUTING = ( |
| 60 | + os.getenv("GOOGLE_CLOUD_SPANNER_ENABLE_RESOURCE_BASED_ROUTING") == "true" |
| 61 | +) |
59 | 62 |
|
60 | 63 | if CREATE_INSTANCE: |
61 | 64 | INSTANCE_ID = "google-cloud" + unique_resource_id("-") |
@@ -282,6 +285,61 @@ def tearDown(self): |
282 | 285 | for doomed in self.to_delete: |
283 | 286 | doomed.drop() |
284 | 287 |
|
| 288 | + @unittest.skipUnless(USE_RESOURCE_ROUTING, "requires enabling resource routing") |
| 289 | + def test_spanner_api_use_user_specified_endpoint(self): |
| 290 | + # Clear cache. |
| 291 | + Client._endpoint_cache = {} |
| 292 | + api = Config.CLIENT.instance_admin_api |
| 293 | + resp = api.get_instance( |
| 294 | + Config.INSTANCE.name, field_mask={"paths": ["endpoint_uris"]} |
| 295 | + ) |
| 296 | + if not resp or not resp.endpoint_uris: |
| 297 | + return # no resolved endpoint. |
| 298 | + resolved_endpoint = resp.endpoint_uris[0] |
| 299 | + |
| 300 | + client = Client(client_options={"api_endpoint": resolved_endpoint}) |
| 301 | + |
| 302 | + instance = client.instance(Config.INSTANCE.instance_id) |
| 303 | + temp_db_id = "temp_db" + unique_resource_id("_") |
| 304 | + temp_db = instance.database(temp_db_id) |
| 305 | + temp_db.spanner_api |
| 306 | + |
| 307 | + # No endpoint cache - Default endpoint used. |
| 308 | + self.assertEqual(client._endpoint_cache, {}) |
| 309 | + |
| 310 | + @unittest.skipUnless(USE_RESOURCE_ROUTING, "requires enabling resource routing") |
| 311 | + def test_spanner_api_use_resolved_endpoint(self): |
| 312 | + # Clear cache. |
| 313 | + Client._endpoint_cache = {} |
| 314 | + api = Config.CLIENT.instance_admin_api |
| 315 | + resp = api.get_instance( |
| 316 | + Config.INSTANCE.name, field_mask={"paths": ["endpoint_uris"]} |
| 317 | + ) |
| 318 | + if not resp or not resp.endpoint_uris: |
| 319 | + return # no resolved endpoint. |
| 320 | + resolved_endpoint = resp.endpoint_uris[0] |
| 321 | + |
| 322 | + client = Client( |
| 323 | + client_options=Config.CLIENT._client_options |
| 324 | + ) # Use same endpoint as main client. |
| 325 | + |
| 326 | + instance = client.instance(Config.INSTANCE.instance_id) |
| 327 | + temp_db_id = "temp_db" + unique_resource_id("_") |
| 328 | + temp_db = instance.database(temp_db_id) |
| 329 | + temp_db.spanner_api |
| 330 | + |
| 331 | + # Endpoint is cached - resolved endpoint used. |
| 332 | + self.assertIn(Config.INSTANCE.name, client._endpoint_cache) |
| 333 | + self.assertEqual( |
| 334 | + client._endpoint_cache[Config.INSTANCE.name], resolved_endpoint |
| 335 | + ) |
| 336 | + |
| 337 | + # Endpoint is cached at a class level. |
| 338 | + self.assertIn(Config.INSTANCE.name, Config.CLIENT._endpoint_cache) |
| 339 | + self.assertEqual( |
| 340 | + Config.CLIENT._endpoint_cache[Config.INSTANCE.name], resolved_endpoint |
| 341 | + ) |
| 342 | + |
285 | 343 | def test_list_databases(self): |
286 | 344 | # Since `Config.INSTANCE` is newly created in `setUpModule`, the |
287 | 345 | # database created in `setUpClass` here will be the only one. |
|
0 commit comments