@@ -902,3 +902,102 @@ func TestFilePersist_Cleanup_ContextCancellation(t *testing.T) {
902902 t .Errorf ("expected context.Canceled error, got: %v" , err )
903903 }
904904}
905+
906+ func TestFilePersist_Flush (t * testing.T ) {
907+ dir := t .TempDir ()
908+ fp , err := New [string , int ]("test" , dir )
909+ if err != nil {
910+ t .Fatalf ("New: %v" , err )
911+ }
912+ defer func () {
913+ if err := fp .Close (); err != nil {
914+ t .Logf ("Close error: %v" , err )
915+ }
916+ }()
917+
918+ ctx := context .Background ()
919+
920+ // Store multiple entries
921+ for i := range 10 {
922+ if err := fp .Store (ctx , fmt .Sprintf ("key-%d" , i ), i * 100 , time.Time {}); err != nil {
923+ t .Fatalf ("Store: %v" , err )
924+ }
925+ }
926+
927+ // Verify files exist
928+ for i := range 10 {
929+ if _ , _ , found , err := fp .Load (ctx , fmt .Sprintf ("key-%d" , i )); err != nil || ! found {
930+ t .Fatalf ("key-%d should exist before flush" , i )
931+ }
932+ }
933+
934+ // Flush
935+ deleted , err := fp .Flush (ctx )
936+ if err != nil {
937+ t .Fatalf ("Flush: %v" , err )
938+ }
939+ if deleted != 10 {
940+ t .Errorf ("Flush deleted %d entries; want 10" , deleted )
941+ }
942+
943+ // All entries should be gone
944+ for i := range 10 {
945+ if _ , _ , found , err := fp .Load (ctx , fmt .Sprintf ("key-%d" , i )); err != nil {
946+ t .Fatalf ("Load: %v" , err )
947+ } else if found {
948+ t .Errorf ("key-%d should not exist after flush" , i )
949+ }
950+ }
951+ }
952+
953+ func TestFilePersist_Flush_Empty (t * testing.T ) {
954+ dir := t .TempDir ()
955+ fp , err := New [string , int ]("test" , dir )
956+ if err != nil {
957+ t .Fatalf ("New: %v" , err )
958+ }
959+ defer func () {
960+ if err := fp .Close (); err != nil {
961+ t .Logf ("Close error: %v" , err )
962+ }
963+ }()
964+
965+ // Flush empty cache
966+ deleted , err := fp .Flush (context .Background ())
967+ if err != nil {
968+ t .Fatalf ("Flush: %v" , err )
969+ }
970+ if deleted != 0 {
971+ t .Errorf ("Flush deleted %d entries; want 0" , deleted )
972+ }
973+ }
974+
975+ func TestFilePersist_Flush_ContextCancellation (t * testing.T ) {
976+ dir := t .TempDir ()
977+ fp , err := New [string , int ]("test" , dir )
978+ if err != nil {
979+ t .Fatalf ("New: %v" , err )
980+ }
981+ defer func () {
982+ if err := fp .Close (); err != nil {
983+ t .Logf ("Close error: %v" , err )
984+ }
985+ }()
986+
987+ // Store many entries
988+ for i := range 100 {
989+ if err := fp .Store (context .Background (), fmt .Sprintf ("key-%d" , i ), i , time.Time {}); err != nil {
990+ t .Fatalf ("Store: %v" , err )
991+ }
992+ }
993+
994+ // Create context that we'll cancel
995+ ctx , cancel := context .WithCancel (context .Background ())
996+ cancel ()
997+
998+ // Try to flush
999+ _ , err = fp .Flush (ctx )
1000+ if err == nil || ! errors .Is (err , context .Canceled ) {
1001+ t .Errorf ("expected context.Canceled error, got: %v" , err )
1002+ }
1003+ }
0 commit comments