11import asyncio
2+ import warnings
23
3- from async_lru import alru_cache
4+ import pytest
45
6+ from async_lru import AlruCacheLoopResetWarning , alru_cache
57
8+ @pytest .mark .filterwarnings ("ignore::async_lru.AlruCacheLoopResetWarning" )
69def test_cross_loop_auto_resets_cache () -> None :
710 @alru_cache (maxsize = 100 )
811 async def cached_func (key : str ) -> str :
@@ -24,7 +27,7 @@ async def cached_func(key: str) -> str:
2427 assert cached_func .cache_info ().hits == 0
2528 assert cached_func .cache_info ().misses == 1
2629
27-
30+ @ pytest . mark . filterwarnings ( "ignore::async_lru.AlruCacheLoopResetWarning" )
2831def test_cross_loop_preserves_stats_reset () -> None :
2932 @alru_cache (maxsize = 100 )
3033 async def cached_func (key : str ) -> str :
@@ -46,7 +49,7 @@ async def cached_func(key: str) -> str:
4649 assert cached_func .cache_info ().hits == 0
4750 assert cached_func .cache_info ().misses == 1
4851
49-
52+ @ pytest . mark . filterwarnings ( "ignore::async_lru.AlruCacheLoopResetWarning" )
5053def test_invalid_key_does_not_bind_loop () -> None :
5154 @alru_cache (maxsize = 100 )
5255 async def cached_func (key : object ) -> str :
@@ -139,7 +142,7 @@ async def run_concurrent() -> list[str]:
139142 assert results == ["data_test" ] * 3
140143 assert cached_func .cache_info ().hits == 2
141144
142-
145+ @ pytest . mark . filterwarnings ( "ignore::async_lru.AlruCacheLoopResetWarning" )
143146def test_multiple_loop_transitions () -> None :
144147 @alru_cache (maxsize = 100 )
145148 async def cached_func (key : str ) -> str :
@@ -150,3 +153,45 @@ async def cached_func(key: str) -> str:
150153 result = loop .run_until_complete (cached_func ("test" ))
151154 loop .close ()
152155 assert result == "data_test"
156+
157+
158+ def test_loop_change_emits_warning () -> None :
159+ @alru_cache (maxsize = 100 )
160+ async def cached_func (key : str ) -> str :
161+ return f"data_{ key } "
162+
163+ loop1 = asyncio .new_event_loop ()
164+ loop1 .run_until_complete (cached_func ("test" ))
165+ loop1 .close ()
166+
167+ loop2 = asyncio .new_event_loop ()
168+ with warnings .catch_warnings (record = True ) as w :
169+ warnings .simplefilter ("always" )
170+ loop2 .run_until_complete (cached_func ("test" ))
171+ loop2 .close ()
172+
173+ assert len (w ) == 1
174+ assert issubclass (w [0 ].category , AlruCacheLoopResetWarning )
175+ assert "event loop change" in str (w [0 ].message )
176+
177+
178+ def test_loop_change_warns_only_once () -> None :
179+ @alru_cache (maxsize = 100 )
180+ async def cached_func (key : str ) -> str :
181+ return f"data_{ key } "
182+
183+ all_warnings : list [warnings .WarningMessage ] = []
184+ for _ in range (4 ):
185+ loop = asyncio .new_event_loop ()
186+ with warnings .catch_warnings (record = True ) as w :
187+ warnings .simplefilter ("always" )
188+ loop .run_until_complete (cached_func ("test" ))
189+ loop .close ()
190+ all_warnings .extend (w )
191+
192+ reset_warnings = [
193+ w for w in all_warnings
194+ if issubclass (w .category , AlruCacheLoopResetWarning )
195+ ]
196+ assert len (reset_warnings ) == 1
197+
0 commit comments