@@ -396,29 +396,72 @@ fn soft_purge_does_not_make_mandatory_revalidation_eligible_for_stale_reuse() {
396396 }
397397}
398398
399+ #[ test]
400+ fn bin_soft_purge_checkpoints_once_per_group_even_with_colliding_stripes ( ) {
401+ for encrypted in [ false , true ] {
402+ let directory = tempfile:: tempdir ( ) . unwrap ( ) ;
403+ let mut config = storage_bin_config ( & directory. path ( ) . join ( "cache" ) ) ;
404+ if encrypted {
405+ super :: store_admission:: configure_encryption ( & mut config, directory. path ( ) ) ;
406+ }
407+ let cache = Arc :: new ( NativeDiskCache :: from_config ( & config) . unwrap ( ) ) ;
408+ let scope = format ! ( "group-soft-{}" , directory. path( ) . display( ) ) ;
409+ register_native_disk_cache_purge_handle ( Arc :: from ( scope. as_str ( ) ) , None , & cache) ;
410+ let mut keys = vec ! [ "first" . to_owned( ) ] ;
411+ let stripe = super :: super :: native_http1_cache_state:: native_disk_cache_mutation_lock_stripe;
412+ keys. extend (
413+ ( 0 ..20000 )
414+ . map ( |n| format ! ( "collision-{n}" ) )
415+ . filter ( |k| stripe ( k) == stripe ( "first" ) )
416+ . take ( 39 ) ,
417+ ) ;
418+ assert_eq ! ( keys. len( ) , 40 ) ;
419+ let mut item = disk_cache_entry ( b"body" ) ;
420+ item. stale_if_error_until = Some ( Instant :: now ( ) + Duration :: from_secs ( 120 ) ) ;
421+ for key in & keys {
422+ store ( & cache, & scope, key, & item) ;
423+ }
424+ let tag = fluxheim_cache:: cache_user_tag ( & scope, None ) ;
425+ for ( expected, truncated, writes) in [ ( 32 , true , 1 ) , ( 8 , false , 2 ) , ( 0 , true , 2 ) ] {
426+ let result = purge_native_disk_cache_user_tag ( & scope, None , & tag, 10000 , true ) ;
427+ assert_eq ! (
428+ ( result. purged, result. truncated, cache. soft_checkpoints( ) ) ,
429+ ( expected, truncated, writes)
430+ ) ;
431+ }
432+ drop ( cache) ;
433+ let reopened = NativeDiskCache :: from_config ( & config) . unwrap ( ) ;
434+ for key in keys {
435+ let object = reopened. get ( & key, |_| None ) . unwrap ( ) ;
436+ assert ! ( object. expires_at <= Instant :: now( ) ) ;
437+ assert_eq ! ( & * object. body, b"body" ) ;
438+ }
439+ }
440+ }
441+
399442#[ test]
400443fn bin_soft_purge_checkpoint_error_leaves_the_old_location_and_accounting ( ) {
401444 let ( directory, cache, scope) = fixture ( CacheDiskBackend :: StorageBin ) ;
402445 store ( & cache, & scope, "asset" , & disk_cache_entry ( b"old" ) ) ;
403- let old = cache. with_state ( |s| ( s. objects [ "asset" ] . clone ( ) , s. bytes ) ) ;
446+ store ( & cache, & scope, "second" , & disk_cache_entry ( b"other" ) ) ;
447+ let old = cache. with_state ( |s| ( s. objects . clone ( ) , s. bytes ) ) ;
448+ let free = cache. stats ( ) . free_size_bytes ;
404449 let path = fluxheim_cache:: storage_bin_index_path ( directory. path ( ) ) ;
405450 let saved = directory. path ( ) . join ( "saved-index" ) ;
406451 std:: fs:: rename ( & path, & saved) . unwrap ( ) ;
407452 std:: fs:: create_dir ( & path) . unwrap ( ) ;
408453 let tag = fluxheim_cache:: cache_user_tag ( & scope, None ) ;
409- let result = purge_native_disk_cache_user_tag ( & scope, None , & tag, 1 , true ) ;
454+ let result = purge_native_disk_cache_user_tag ( & scope, None , & tag, 32 , true ) ;
410455 assert_eq ! ( result. purged, 0 ) ;
411456 assert ! ( result. truncated) ;
412- assert_eq ! (
413- cache. with_state( |s| ( s. objects[ "asset" ] . clone( ) , s. bytes) ) ,
414- old
415- ) ;
457+ assert_eq ! ( cache. with_state( |s| ( s. objects. clone( ) , s. bytes) ) , old) ;
458+ assert_eq ! ( cache. stats( ) . free_size_bytes, free) ;
416459 assert_eq ! ( & * cache. get( "asset" , |_| None ) . unwrap( ) . body, b"old" ) ;
417460 std:: fs:: remove_dir ( & path) . unwrap ( ) ;
418461 std:: fs:: rename ( saved, path) . unwrap ( ) ;
419462 assert_eq ! (
420- purge_native_disk_cache_user_tag( & scope, None , & tag, 1 , true ) . purged,
421- 1
463+ purge_native_disk_cache_user_tag( & scope, None , & tag, 32 , true ) . purged,
464+ 2
422465 ) ;
423466}
424467
0 commit comments