@@ -238,3 +238,60 @@ async def test_MonitoredCache_purge_closed_sockets(
238238 # call _purge_closed_sockets and verify socket is removed
239239 monitored_cache ._purge_closed_sockets ()
240240 assert len (monitored_cache .sockets ) == 0
241+
242+
243+ async def test_MonitoredCache_OptimizationForImmutableNames (
244+ fake_client : CloudSQLClient ,
245+ ) -> None :
246+ """
247+ Test that MonitoredCache disables DNS polling for immutable instance DNS names
248+ but enables it for custom DNS names and mutable global instance DNS names.
249+ """
250+ # 1. Custom DNS name -> should enable ticker
251+ conn_name_custom = ConnectionName (
252+ "test-project" , "test-region" , "test-instance" , "db.example.com"
253+ )
254+ cache_custom = LazyRefreshCache (
255+ conn_name_custom ,
256+ client = fake_client ,
257+ keys = asyncio .create_task (generate_keys ()),
258+ enable_iam_auth = False ,
259+ )
260+ monitored_cache_custom = MonitoredCache (cache_custom , 30 , DnsResolver ())
261+ assert monitored_cache_custom .domain_name_ticker is not None
262+ await monitored_cache_custom .close ()
263+
264+ # 2. Immutable instance DNS name -> should NOT enable ticker
265+ conn_name_immutable = ConnectionName (
266+ "test-project" ,
267+ "test-region" ,
268+ "test-instance" ,
269+ "0123456789ab.fedcba9876543.us-central1.sql-psc.goog" ,
270+ )
271+ cache_immutable = LazyRefreshCache (
272+ conn_name_immutable ,
273+ client = fake_client ,
274+ keys = asyncio .create_task (generate_keys ()),
275+ enable_iam_auth = False ,
276+ )
277+ monitored_cache_immutable = MonitoredCache (cache_immutable , 30 , DnsResolver ())
278+ assert monitored_cache_immutable .domain_name_ticker is None
279+ await monitored_cache_immutable .close ()
280+
281+ # 3. Mutable global instance DNS name -> should enable ticker
282+ conn_name_global = ConnectionName (
283+ "test-project" ,
284+ "test-region" ,
285+ "test-instance" ,
286+ "0123456789ab.fedcba9876543.global.sql-psc.goog" ,
287+ )
288+ cache_global = LazyRefreshCache (
289+ conn_name_global ,
290+ client = fake_client ,
291+ keys = asyncio .create_task (generate_keys ()),
292+ enable_iam_auth = False ,
293+ )
294+ monitored_cache_global = MonitoredCache (cache_global , 30 , DnsResolver ())
295+ assert monitored_cache_global .domain_name_ticker is not None
296+ await monitored_cache_global .close ()
297+
0 commit comments