@@ -13,11 +13,18 @@ namespace BitFaster.Caching.UnitTests.Atomic
1313 public class AtomicFactoryCacheTests
1414 {
1515 private const int capacity = 6 ;
16- private readonly AtomicFactoryCache < int , int > cache = new ( new ConcurrentLru < int , AtomicFactory < int , int > > ( capacity ) ) ;
16+ private readonly ConcurrentLru < int , AtomicFactory < int , int > > innerCache ;
17+ private readonly AtomicFactoryCache < int , int > cache ;
1718
1819 private List < ItemRemovedEventArgs < int , int > > removedItems = new ( ) ;
1920 private List < ItemUpdatedEventArgs < int , int > > updatedItems = new ( ) ;
2021
22+ public AtomicFactoryCacheTests ( )
23+ {
24+ innerCache = new ConcurrentLru < int , AtomicFactory < int , int > > ( capacity ) ;
25+ cache = new ( innerCache ) ;
26+ }
27+
2128 [ Fact ]
2229 public void WhenInnerCacheIsNullCtorThrows ( )
2330 {
@@ -70,10 +77,52 @@ public void WhenRemovedEventHandlerIsRegisteredItIsFired()
7077 this . cache . TryRemove ( 1 ) ;
7178
7279 this . removedItems . First ( ) . Key . Should ( ) . Be ( 1 ) ;
80+ }
81+
82+ // backcompat: remove conditional compile
83+ #if NETCOREAPP3_0_OR_GREATER
84+ [ Fact ]
85+ public void WhenRemovedValueIsReturned ( )
86+ {
87+ this . cache . AddOrUpdate ( 1 , 1 ) ;
88+ this . cache . TryRemove ( 1 , out var value ) ;
89+
90+ value . Should ( ) . Be ( 1 ) ;
91+ }
92+
93+ [ Fact ]
94+ public void WhenNotRemovedValueIsDefault ( )
95+ {
96+ this . cache . AddOrUpdate ( 1 , 1 ) ;
97+ this . cache . TryRemove ( 2 , out var value ) ;
98+
99+ value . Should ( ) . Be ( 0 ) ;
100+ }
101+
102+ [ Fact ]
103+ public void WhenRemoveKeyValueAndValueDoesntMatchDontRemove ( )
104+ {
105+ this . cache . AddOrUpdate ( 1 , 1 ) ;
106+ this . cache . TryRemove ( new KeyValuePair < int , int > ( 1 , 2 ) ) . Should ( ) . BeFalse ( ) ;
107+ }
108+
109+ [ Fact ]
110+ public void WhenRemoveKeyValueAndValueDoesMatchThenRemove ( )
111+ {
112+ this . cache . AddOrUpdate ( 1 , 1 ) ;
113+ this . cache . TryRemove ( new KeyValuePair < int , int > ( 1 , 1 ) ) . Should ( ) . BeTrue ( ) ;
114+ }
115+
116+ [ Fact ]
117+ public void WhenRemoveKeyValueAndValueIsNotCreatedDoesNotRemove ( )
118+ {
119+ // seed the inner cache with an not yet created value
120+ this . innerCache . AddOrUpdate ( 1 , new AtomicFactory < int , int > ( ) ) ;
121+
122+ // try to remove with the default value (0)
123+ this . cache . TryRemove ( new KeyValuePair < int , int > ( 1 , 0 ) ) . Should ( ) . BeFalse ( ) ;
73124 }
74125
75- // backcompat: remove conditional compile
76- #if NETCOREAPP3_0_OR_GREATER
77126 [ Fact ]
78127 public void WhenUpdatedEventHandlerIsRegisteredItIsFired ( )
79128 {
0 commit comments