@@ -73,3 +73,56 @@ async def raise_runtime_error() -> NoReturn:
7373 )
7474 assert len (exceptions ) == N
7575 assert all (isinstance (exc , RuntimeError ) for exc in exceptions )
76+
77+
78+ @pytest .mark .parametrize ("N" , (1 , 2 , 4 , 16 , 64 ))
79+ async def test_gready_cache_identity (
80+ N : int ,
81+ pgconn : asyncpg .Connection ,
82+ ) -> None :
83+ statistics = collections .Counter [str ]()
84+ listener = listeners .PGEventQueue ()
85+ await listener .connect (
86+ pgconn ,
87+ models .PGChannel ("test_gready_cache_decorator_exceptions" ),
88+ )
89+
90+ @decorators .cache (
91+ strategy = strategies .Gready (listener = listener ),
92+ statistics_callback = lambda x : statistics .update ([x ]),
93+ )
94+ async def identity (x : int ) -> int :
95+ return x
96+
97+ results = await asyncio .gather (* [identity (n ) for n in range (N )])
98+
99+ assert sorted (results ) == list (range (N ))
100+ assert statistics ["miss" ] == N
101+ assert statistics ["hit" ] == 0
102+
103+
104+ @pytest .mark .parametrize ("N" , (1 , 2 , 4 , 16 , 64 ))
105+ async def test_gready_cache_sleepy (
106+ N : int ,
107+ pgconn : asyncpg .Connection ,
108+ ) -> None :
109+ statistics = collections .Counter [str ]()
110+ listener = listeners .PGEventQueue ()
111+ await listener .connect (
112+ pgconn ,
113+ models .PGChannel ("test_gready_cache_decorator_exceptions" ),
114+ )
115+
116+ @decorators .cache (
117+ strategy = strategies .Gready (listener = listener ),
118+ statistics_callback = lambda x : statistics .update ([x ]),
119+ )
120+ async def now () -> datetime .datetime :
121+ await asyncio .sleep (0.01 )
122+ return datetime .datetime .now ()
123+
124+ results = await asyncio .gather (* [now () for _ in range (N )])
125+
126+ assert len (set (results )) == 1
127+ assert statistics ["miss" ] == 1
128+ assert statistics ["hit" ] == N - 1
0 commit comments