1
+ from memoize .coerced import _timeout_error_type
1
2
from tests .py310workaround import fix_python_3_10_compatibility
2
3
3
4
fix_python_3_10_compatibility ()
@@ -160,6 +161,69 @@ async def get_value(arg, kwarg=None):
160
161
self .assertEqual (0 , res1 )
161
162
self .assertEqual (1 , res2 )
162
163
164
+ @gen_test
165
+ async def test_should_return_exception_for_all_concurrent_callers (self ):
166
+ # given
167
+ value = 0
168
+
169
+ @memoize ()
170
+ async def get_value (arg , kwarg = None ):
171
+ raise ValueError (f'stub{ value } ' )
172
+
173
+ # when
174
+ res1 = get_value ('test' , kwarg = 'args1' )
175
+ res2 = get_value ('test' , kwarg = 'args1' )
176
+ res3 = get_value ('test' , kwarg = 'args1' )
177
+
178
+ # then
179
+ with self .assertRaises (Exception ) as context :
180
+ await res1
181
+ self .assertEqual (context .exception .__class__ , CachedMethodFailedException )
182
+ self .assertEqual (str (context .exception .__cause__ ), str (ValueError ('stub0' )))
183
+
184
+ with self .assertRaises (Exception ) as context :
185
+ await res2
186
+ self .assertEqual (context .exception .__class__ , CachedMethodFailedException )
187
+ self .assertEqual (str (context .exception .__cause__ ), str (ValueError ('stub0' )))
188
+
189
+ with self .assertRaises (Exception ) as context :
190
+ await res3
191
+ self .assertEqual (context .exception .__class__ , CachedMethodFailedException )
192
+ self .assertEqual (str (context .exception .__cause__ ), str (ValueError ('stub0' )))
193
+
194
+ @gen_test
195
+ async def test_should_return_timeout_for_all_concurrent_callers (self ):
196
+ # given
197
+ value = 0
198
+
199
+ @memoize (configuration = DefaultInMemoryCacheConfiguration (method_timeout = timedelta (milliseconds = 1 )))
200
+ async def get_value (arg , kwarg = None ):
201
+ await _ensure_asyncio_background_tasks_finished ()
202
+ time .sleep (.200 )
203
+ await _ensure_asyncio_background_tasks_finished ()
204
+ return value
205
+
206
+ # when
207
+ res1 = get_value ('test' , kwarg = 'args1' )
208
+ res2 = get_value ('test' , kwarg = 'args1' )
209
+ res3 = get_value ('test' , kwarg = 'args1' )
210
+
211
+ # then
212
+ with self .assertRaises (Exception ) as context :
213
+ await res1
214
+ self .assertEqual (context .exception .__class__ , CachedMethodFailedException )
215
+ self .assertEqual (context .exception .__cause__ .__class__ , _timeout_error_type ())
216
+
217
+ with self .assertRaises (Exception ) as context :
218
+ await res2
219
+ self .assertEqual (context .exception .__class__ , CachedMethodFailedException )
220
+ self .assertEqual (context .exception .__cause__ .__class__ , _timeout_error_type ())
221
+
222
+ with self .assertRaises (Exception ) as context :
223
+ await res3
224
+ self .assertEqual (context .exception .__class__ , CachedMethodFailedException )
225
+ self .assertEqual (context .exception .__cause__ .__class__ , _timeout_error_type ())
226
+
163
227
@gen_test
164
228
async def test_should_return_same_value_on_constant_key_function (self ):
165
229
# given
@@ -253,8 +317,8 @@ async def get_value(arg, kwarg=None):
253
317
await get_value ('test1' , kwarg = 'args1' )
254
318
255
319
# then
256
- expected = CachedMethodFailedException ('Refresh failed to complete' , ValueError ( 'Get lost' , ))
257
- self .assertEqual (str (expected ), str (context . exception )) # ToDo: consider better comparision
320
+ self . assertEqual ( str ( context . exception ), str ( CachedMethodFailedException ('Refresh failed to complete' ) ))
321
+ self .assertEqual (str (context . exception . __cause__ ), str (ValueError ( "Get lost" )))
258
322
259
323
@gen_test
260
324
async def test_should_throw_exception_on_refresh_timeout (self ):
@@ -272,8 +336,8 @@ async def get_value(arg, kwarg=None):
272
336
await get_value ('test1' , kwarg = 'args1' )
273
337
274
338
# then
275
- expected = CachedMethodFailedException ( 'Refresh timed out' )
276
- self .assertEqual (str ( expected ), str ( context .exception )) # ToDo: consider better comparision
339
+ self . assertEqual ( context . exception . __class__ , CachedMethodFailedException )
340
+ self .assertEqual (context .exception . __cause__ . __class__ , _timeout_error_type ())
277
341
278
342
@staticmethod
279
343
async def _call_thrice (call ):
0 commit comments