@@ -332,6 +332,67 @@ public async Task ThreadSafeSnapshot()
332332 }
333333 }
334334
335+ [ Fact ]
336+ public async Task ReturnsCachedResultFromSnapshot ( )
337+ {
338+ IConfiguration config = new ConfigurationBuilder ( ) . AddJsonFile ( "appsettings.json" ) . Build ( ) ;
339+
340+ var services = new ServiceCollection ( ) ;
341+
342+ services
343+ . AddSingleton ( config )
344+ . AddFeatureManagement ( )
345+ . AddFeatureFilter < TestFilter > ( ) ;
346+
347+ ServiceProvider serviceProvider = services . BuildServiceProvider ( ) ;
348+
349+ IVariantFeatureManager featureManager = serviceProvider . GetRequiredService < IVariantFeatureManager > ( ) ;
350+
351+ IVariantFeatureManager featureManagerSnapshot = serviceProvider . GetRequiredService < IVariantFeatureManagerSnapshot > ( ) ;
352+
353+ IEnumerable < IFeatureFilterMetadata > featureFilters = serviceProvider . GetRequiredService < IEnumerable < IFeatureFilterMetadata > > ( ) ;
354+
355+ TestFilter testFeatureFilter = ( TestFilter ) featureFilters . First ( f => f is TestFilter ) ;
356+
357+ int callCount = 0 ;
358+ bool filterEnabled = true ;
359+
360+ testFeatureFilter . Callback = ( evaluationContext ) =>
361+ {
362+ callCount ++ ;
363+ return Task . FromResult ( filterEnabled ) ;
364+ } ;
365+
366+ // First evaluation - filter is enabled and should return true
367+ bool result1 = await featureManagerSnapshot . IsEnabledAsync ( Features . ConditionalFeature ) ;
368+ Assert . Equal ( 1 , callCount ) ;
369+ Assert . True ( result1 ) ;
370+
371+ Variant variant1 = await featureManagerSnapshot . GetVariantAsync ( Features . ConditionalFeature ) ;
372+ Assert . Equal ( 2 , callCount ) ;
373+ Assert . Equal ( "DefaultWhenEnabled" , variant1 . Name ) ;
374+
375+ // "Shut down" the feature filter
376+ filterEnabled = false ;
377+
378+ // Second evaluation - should use cached value despite filter being shut down
379+ bool result2 = await featureManagerSnapshot . IsEnabledAsync ( Features . ConditionalFeature ) ;
380+ Assert . Equal ( 2 , callCount ) ;
381+ Assert . True ( result2 ) ;
382+
383+ Variant variant2 = await featureManagerSnapshot . GetVariantAsync ( Features . ConditionalFeature ) ;
384+ Assert . Equal ( 2 , callCount ) ;
385+ Assert . Equal ( "DefaultWhenEnabled" , variant2 . Name ) ;
386+
387+ bool result3 = await featureManager . IsEnabledAsync ( Features . ConditionalFeature ) ;
388+ Assert . Equal ( 3 , callCount ) ;
389+ Assert . False ( result3 ) ;
390+
391+ Variant variant3 = await featureManager . GetVariantAsync ( Features . ConditionalFeature ) ;
392+ Assert . Equal ( 4 , callCount ) ;
393+ Assert . Equal ( "DefaultWhenDisabled" , variant3 . Name ) ;
394+ }
395+
335396 [ Fact ]
336397 public void AddsScopedFeatureManagement ( )
337398 {
0 commit comments