github.com/jimmyx0x/go-ethereum@v1.10.28/core/rawdb/freezer_resettable_test.go (about) 1 // Copyright 2022 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rawdb 18 19 import ( 20 "bytes" 21 "os" 22 "testing" 23 24 "github.com/ethereum/go-ethereum/ethdb" 25 ) 26 27 func TestResetFreezer(t *testing.T) { 28 items := []struct { 29 id uint64 30 blob []byte 31 }{ 32 {0, bytes.Repeat([]byte{0}, 2048)}, 33 {1, bytes.Repeat([]byte{1}, 2048)}, 34 {2, bytes.Repeat([]byte{2}, 2048)}, 35 } 36 f, _ := NewResettableFreezer(t.TempDir(), "", false, 2048, freezerTestTableDef) 37 defer f.Close() 38 39 f.ModifyAncients(func(op ethdb.AncientWriteOp) error { 40 for _, item := range items { 41 op.AppendRaw("test", item.id, item.blob) 42 } 43 return nil 44 }) 45 for _, item := range items { 46 blob, _ := f.Ancient("test", item.id) 47 if !bytes.Equal(blob, item.blob) { 48 t.Fatal("Unexpected blob") 49 } 50 } 51 52 // Reset freezer 53 f.Reset() 54 count, _ := f.Ancients() 55 if count != 0 { 56 t.Fatal("Failed to reset freezer") 57 } 58 for _, item := range items { 59 blob, _ := f.Ancient("test", item.id) 60 if len(blob) != 0 { 61 t.Fatal("Unexpected blob") 62 } 63 } 64 65 // Fill the freezer 66 f.ModifyAncients(func(op ethdb.AncientWriteOp) error { 67 for _, item := range items { 68 op.AppendRaw("test", item.id, item.blob) 69 } 70 return nil 71 }) 72 for _, item := range items { 73 blob, _ := f.Ancient("test", item.id) 74 if !bytes.Equal(blob, item.blob) { 75 t.Fatal("Unexpected blob") 76 } 77 } 78 } 79 80 func TestFreezerCleanup(t *testing.T) { 81 items := []struct { 82 id uint64 83 blob []byte 84 }{ 85 {0, bytes.Repeat([]byte{0}, 2048)}, 86 {1, bytes.Repeat([]byte{1}, 2048)}, 87 {2, bytes.Repeat([]byte{2}, 2048)}, 88 } 89 datadir := t.TempDir() 90 f, _ := NewResettableFreezer(datadir, "", false, 2048, freezerTestTableDef) 91 f.ModifyAncients(func(op ethdb.AncientWriteOp) error { 92 for _, item := range items { 93 op.AppendRaw("test", item.id, item.blob) 94 } 95 return nil 96 }) 97 f.Close() 98 os.Rename(datadir, tmpName(datadir)) 99 100 // Open the freezer again, trigger cleanup operation 101 f, _ = NewResettableFreezer(datadir, "", false, 2048, freezerTestTableDef) 102 f.Close() 103 104 if _, err := os.Lstat(tmpName(datadir)); !os.IsNotExist(err) { 105 t.Fatal("Failed to cleanup leftover directory") 106 } 107 }