@@ -343,7 +343,7 @@ await cacheStack.GetOrSetAsync<int>(
343343 async _ =>
344344 {
345345 Interlocked . Increment ( ref getterCallCount ) ;
346- await Task . Delay ( 250 ) ;
346+ await Task . Delay ( 100 ) ;
347347 throw expectedException ;
348348 } ,
349349 new CacheSettings ( TimeSpan . FromDays ( 2 ) , TimeSpan . Zero )
@@ -358,8 +358,9 @@ await cacheStack.GetOrSetAsync<int>(
358358 {
359359 await Task . WhenAll ( tasks ) ;
360360 }
361- catch ( Exception )
361+ catch ( Exception e )
362362 {
363+ Assert . IsInstanceOfType ( e , expectedException . GetType ( ) ) ;
363364 }
364365
365366 Assert . AreEqual ( 1 , getterCallCount ) ;
@@ -368,6 +369,43 @@ await cacheStack.GetOrSetAsync<int>(
368369 Assert . AreSame ( expectedException , task . Exception . InnerException ) ;
369370 }
370371 }
372+ [ TestMethod ]
373+ [ DataRow ( null ) ]
374+ [ DataRow ( 42 ) ]
375+ public async Task GetOrSet_ConcurrentAccess_SameResultForAllTasks ( int ? expectedResult )
376+ {
377+ await using var cacheStack = new CacheStack ( new [ ] { new MemoryCacheLayer ( ) } , Array . Empty < ICacheExtension > ( ) ) ;
378+
379+ Internal . DateTimeProvider . UpdateTime ( ) ;
380+ var getterCallCount = 0 ;
381+
382+ async Task < int ? > act ( )
383+ {
384+ return await cacheStack . GetOrSetAsync < int ? > (
385+ "GetOrSet_ConcurrentAccess_SameResultForAllTasks" ,
386+ async _ =>
387+ {
388+ Interlocked . Increment ( ref getterCallCount ) ;
389+ await Task . Delay ( 100 ) ;
390+ return expectedResult ;
391+ } ,
392+ new CacheSettings ( TimeSpan . FromDays ( 2 ) , TimeSpan . Zero )
393+ ) ;
394+ }
395+
396+ var tasks = Enumerable . Range ( 1 , 4 )
397+ . Select ( i => act ( ) )
398+ . ToArray ( ) ;
399+
400+ var whenAll = Task . WhenAll ( tasks ) ;
401+ await Task . WhenAny ( whenAll , Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ) ;
402+
403+ Assert . AreEqual ( 1 , getterCallCount ) ;
404+ foreach ( var task in tasks )
405+ {
406+ Assert . AreEqual ( expectedResult , task . Result ) ;
407+ }
408+ }
371409 [ TestMethod , ExpectedException ( typeof ( ObjectDisposedException ) ) ]
372410 public async Task GetOrSet_ThrowsOnUseAfterDisposal ( )
373411 {
0 commit comments