@@ -417,7 +417,7 @@ public void SetNullCallback_NotAllowed_ArgumentException()
417417
418418 var notNullCallback = new PostEvictionCallbackRegistration ( )
419419 {
420- EvictionCallback = ( _ , _ , _ , _ ) => { }
420+ EvictionCallback = ( _ , _ , _ , _ ) => { }
421421 } ;
422422
423423 options . PostEvictionCallbacks . Add ( notNullCallback ) ;
@@ -570,53 +570,61 @@ public void SetGetAndRemoveWorksWithObjectKeysWhenDifferentReferences()
570570
571571 [ Fact ]
572572 [ ActiveIssue ( "https://github.com/dotnet/runtime/issues/72879" ) ] // issue in cache
573+ [ ActiveIssue ( "https://github.com/dotnet/runtime/issues/72890" ) ] // issue in test
573574 public void GetAndSet_AreThreadSafe_AndUpdatesNeverLeavesNullValues ( )
574575 {
575576 var cache = CreateCache ( ) ;
576577 string key = "myKey" ;
577- bool readValueIsNull = false ;
578+ var cts = new CancellationTokenSource ( ) ;
579+ var readValueIsNull = false ;
578580
579581 cache . Set ( key , new Guid ( ) ) ;
580582
581- const int WriterCount = 2 ;
582- const int Iterations = 20_000 ;
583- using var barrier = new Barrier ( WriterCount + 1 ) ;
583+ var task0 = Task . Run ( ( ) =>
584+ {
585+ while ( ! cts . IsCancellationRequested )
586+ {
587+ cache . Set ( key , Guid . NewGuid ( ) ) ;
588+ }
589+ } ) ;
584590
585- var writers = new Task [ WriterCount ] ;
586- for ( int i = 0 ; i < WriterCount ; i ++ )
591+ var task1 = Task . Run ( ( ) =>
587592 {
588- writers [ i ] = Task . Run ( ( ) =>
593+ while ( ! cts . IsCancellationRequested )
589594 {
590- barrier . SignalAndWait ( ) ;
591- for ( int j = 0 ; j < Iterations ; j ++ )
592- {
593- cache . Set ( key , Guid . NewGuid ( ) ) ;
594- }
595- } ) ;
596- }
595+ cache . Set ( key , Guid . NewGuid ( ) ) ;
596+ }
597+ } ) ;
597598
598- var reader = Task . Run ( ( ) =>
599+ var task2 = Task . Run ( ( ) =>
599600 {
600- barrier . SignalAndWait ( ) ;
601- for ( int j = 0 ; j < Iterations ; j ++ )
601+ while ( ! cts . IsCancellationRequested )
602602 {
603603 if ( cache . Get ( key ) == null )
604604 {
605+ // Stop this task and update flag for assertion
605606 readValueIsNull = true ;
606607 break ;
607608 }
608609 }
609610 } ) ;
610611
611- var all = new Task [ WriterCount + 1 ] ;
612- Array . Copy ( writers , all , WriterCount ) ;
613- all [ WriterCount ] = reader ;
614- Task . WaitAll ( all ) ;
612+ var task3 = Task . Delay ( TimeSpan . FromSeconds ( 7 ) ) ;
613+
614+ Task . WaitAny ( task0 , task1 , task2 , task3 ) ;
615615
616616 Assert . False ( readValueIsNull ) ;
617+ Assert . Equal ( TaskStatus . Running , task0 . Status ) ;
618+ Assert . Equal ( TaskStatus . Running , task1 . Status ) ;
619+ Assert . Equal ( TaskStatus . Running , task2 . Status ) ;
620+ Assert . Equal ( TaskStatus . RanToCompletion , task3 . Status ) ;
621+
622+ cts . Cancel ( ) ;
623+ Task . WaitAll ( task0 , task1 , task2 , task3 ) ;
617624 }
618625
619626 [ Fact ]
627+ [ ActiveIssue ( "https://github.com/dotnet/runtime/issues/72890" ) ]
620628 public void OvercapacityPurge_AreThreadSafe ( )
621629 {
622630 var cache = new MemoryCache ( new MemoryCacheOptions
@@ -625,38 +633,64 @@ public void OvercapacityPurge_AreThreadSafe()
625633 SizeLimit = 10 ,
626634 CompactionPercentage = 0.5
627635 } ) ;
636+ var cts = new CancellationTokenSource ( ) ;
637+ var limitExceeded = false ;
628638
629- const int NumberOfThreads = 3 ;
630- const int IterationsPerThread = 10_000 ;
631- using var barrier = new Barrier ( NumberOfThreads ) ;
632- bool limitExceeded = false ;
639+ var task0 = Task . Run ( ( ) =>
640+ {
641+ while ( ! cts . IsCancellationRequested )
642+ {
643+ if ( cache . Size > 10 )
644+ {
645+ limitExceeded = true ;
646+ break ;
647+ }
648+ cache . Set ( Guid . NewGuid ( ) , Guid . NewGuid ( ) , new MemoryCacheEntryOptions { Size = 1 } ) ;
649+ }
650+ } , cts . Token ) ;
633651
634- var tasks = new Task [ NumberOfThreads ] ;
635- for ( int i = 0 ; i < NumberOfThreads ; i ++ )
652+ var task1 = Task . Run ( ( ) =>
636653 {
637- tasks [ i ] = Task . Run ( ( ) =>
654+ while ( ! cts . IsCancellationRequested )
638655 {
639- barrier . SignalAndWait ( ) ;
640- for ( int j = 0 ; j < IterationsPerThread ; j ++ )
656+ if ( cache . Size > 10 )
641657 {
642- if ( cache . Size > 10 )
643- {
644- limitExceeded = true ;
645- break ;
646- }
647- cache . Set ( Guid . NewGuid ( ) , Guid . NewGuid ( ) , new MemoryCacheEntryOptions { Size = 1 } ) ;
658+ limitExceeded = true ;
659+ break ;
648660 }
649- } ) ;
650- }
661+ cache . Set ( Guid . NewGuid ( ) , Guid . NewGuid ( ) , new MemoryCacheEntryOptions { Size = 1 } ) ;
662+ }
663+ } , cts . Token ) ;
651664
652- Task . WaitAll ( tasks ) ;
665+ var task2 = Task . Run ( ( ) =>
666+ {
667+ while ( ! cts . IsCancellationRequested )
668+ {
669+ if ( cache . Size > 10 )
670+ {
671+ limitExceeded = true ;
672+ break ;
673+ }
674+ cache . Set ( Guid . NewGuid ( ) , Guid . NewGuid ( ) , new MemoryCacheEntryOptions { Size = 1 } ) ;
675+ }
676+ } , cts . Token ) ;
653677
678+ cts . CancelAfter ( TimeSpan . FromSeconds ( 5 ) ) ;
679+ var task3 = Task . Delay ( TimeSpan . FromSeconds ( 7 ) ) ;
680+
681+ Task . WaitAll ( task0 , task1 , task2 , task3 ) ;
682+
683+ Assert . Equal ( TaskStatus . RanToCompletion , task0 . Status ) ;
684+ Assert . Equal ( TaskStatus . RanToCompletion , task1 . Status ) ;
685+ Assert . Equal ( TaskStatus . RanToCompletion , task2 . Status ) ;
686+ Assert . Equal ( TaskStatus . RanToCompletion , task3 . Status ) ;
654687 CapacityTests . AssertCacheSize ( cache . Count , cache ) ;
655688 Assert . InRange ( cache . Count , 0 , 10 ) ;
656689 Assert . False ( limitExceeded ) ;
657690 }
658691
659692 [ Fact ]
693+ [ ActiveIssue ( "https://github.com/dotnet/runtime/issues/72890" ) ]
660694 public void AddAndReplaceEntries_AreThreadSafe ( )
661695 {
662696 var cache = new MemoryCache ( new MemoryCacheOptions
@@ -665,28 +699,46 @@ public void AddAndReplaceEntries_AreThreadSafe()
665699 SizeLimit = 20 ,
666700 CompactionPercentage = 0.5
667701 } ) ;
702+ var cts = new CancellationTokenSource ( ) ;
668703
669- const int NumberOfThreads = 3 ;
670- const int IterationsPerThread = 10_000 ;
671- using var barrier = new Barrier ( NumberOfThreads ) ;
704+ var random = new Random ( ) ;
672705
673- var tasks = new Task [ NumberOfThreads ] ;
674- for ( int i = 0 ; i < NumberOfThreads ; i ++ )
706+ var task0 = Task . Run ( ( ) =>
675707 {
676- // Each thread gets its own Random; Random is not thread-safe.
677- var random = new Random ( i ) ;
678- tasks [ i ] = Task . Run ( ( ) =>
708+ while ( ! cts . IsCancellationRequested )
679709 {
680- barrier . SignalAndWait ( ) ;
681- for ( int j = 0 ; j < IterationsPerThread ; j ++ )
682- {
683- var entrySize = random . Next ( 0 , 5 ) ;
684- cache . Set ( random . Next ( 0 , 10 ) , entrySize , new MemoryCacheEntryOptions { Size = entrySize } ) ;
685- }
686- } ) ;
687- }
710+ var entrySize = random . Next ( 0 , 5 ) ;
711+ cache . Set ( random . Next ( 0 , 10 ) , entrySize , new MemoryCacheEntryOptions { Size = entrySize } ) ;
712+ }
713+ } ) ;
714+
715+ var task1 = Task . Run ( ( ) =>
716+ {
717+ while ( ! cts . IsCancellationRequested )
718+ {
719+ var entrySize = random . Next ( 0 , 5 ) ;
720+ cache . Set ( random . Next ( 0 , 10 ) , entrySize , new MemoryCacheEntryOptions { Size = entrySize } ) ;
721+ }
722+ } ) ;
723+
724+ var task2 = Task . Run ( ( ) =>
725+ {
726+ while ( ! cts . IsCancellationRequested )
727+ {
728+ var entrySize = random . Next ( 0 , 5 ) ;
729+ cache . Set ( random . Next ( 0 , 10 ) , entrySize , new MemoryCacheEntryOptions { Size = entrySize } ) ;
730+ }
731+ } ) ;
688732
689- Task . WaitAll ( tasks ) ;
733+ cts . CancelAfter ( TimeSpan . FromSeconds ( 5 ) ) ;
734+ var task3 = Task . Delay ( TimeSpan . FromSeconds ( 7 ) ) ;
735+
736+ Task . WaitAll ( task0 , task1 , task2 , task3 ) ;
737+
738+ Assert . Equal ( TaskStatus . RanToCompletion , task0 . Status ) ;
739+ Assert . Equal ( TaskStatus . RanToCompletion , task1 . Status ) ;
740+ Assert . Equal ( TaskStatus . RanToCompletion , task2 . Status ) ;
741+ Assert . Equal ( TaskStatus . RanToCompletion , task3 . Status ) ;
690742
691743 var cacheSize = 0 ;
692744 for ( var i = 0 ; i < 10 ; i ++ )
@@ -725,16 +777,15 @@ public void SetDataToCacheWithNullKeyAndChangeTokenThrows()
725777 public void TryGetValueFromCacheWithNullKeyThrows ( )
726778 {
727779 var cache = CreateCache ( ) ;
728- Assert . Throws < ArgumentNullException > ( ( ) => cache . TryGetValue ( null , out long result ) ) ;
780+ Assert . Throws < ArgumentNullException > ( ( ) => cache . TryGetValue ( null , out long result ) ) ;
729781 }
730782
731783 [ Fact ]
732784 public void GetOrCreateFromCacheWithNullKeyThrows ( )
733785 {
734786 var cache = CreateCache ( ) ;
735787 Assert . Throws < ArgumentNullException > ( ( ) => cache . GetOrCreate < object > ( null , null ) )
736- ;
737- }
788+ ; }
738789
739790 [ Fact ]
740791 public async Task GetOrCreateAsyncFromCacheWithNullKeyThrows ( )
0 commit comments