History

bugfixes + test case

Dane Strandboge committed 2 years ago

Commit bc23b4e1 parent c3025142 files · +52 −2Browse files

Unified · Split

2 files
  1. wal.go +11 −2
  2. wal_test.go +41 −0
wal.go+11−2
@@ -1011,14 +1011,23 @@ func (l *Log) IsEmpty() (bool, error) { · 1010 unmodified lines
10111011 // Clear empties the log, removing all entries and resetting
10121012 // the index back to its default value
10131013 func (l *Log) Clear() error {
1014+ l.mu.Lock()
1015+ defer l.mu.Unlock()
1016+
1017+ // Clean up segment files
10141018 for _, segment := range l.segments {
10151019 if err := os.Remove(segment.path); err != nil {
10161020 return err
10171021 }
10181022 }
1023+
1024+ // Clear most state
1025+ l.segments = []*segment{}
10191026 l.sfile = nil
1020− l.firstIndex = 0
1027+ l.firstIndex = 1
10211028 l.lastIndex = 0
10221029 l.clearCache()
1023− return nil
1030+
1031+ // Load the log as if it were new again
1032+ return l.load()
10241033 }
wal_test.go+41−0
@@ -924,6 +924,47 @@ func TestSimpleTruncateBack(t *testing.T) { · 923 unmodified lines
924924
925925 }
926926
927+func TestClear(t *testing.T) {
928+ os.RemoveAll("testlog")
929+
930+ l, err := Open("testlog", &Options{
931+ NoSync: true,
932+ })
933+ if err != nil {
934+ t.Fatal(err)
935+ }
936+ defer l.Close()
937+
938+ // Write some entries
939+ for i := 1; i < 50; i++ {
940+ err := l.Write(uint64(i), []byte(dataStr(uint64(i))))
941+ if err != nil {
942+ t.Fatal(err)
943+ }
944+ }
945+
946+ // Clear the log
947+ if err := l.Clear(); err != nil {
948+ t.Fatal(err)
949+ }
950+
951+ // Validate some log information
952+ if i, err := l.FirstIndex(); err != nil || i != 0 {
953+ t.Fatal(fmt.Errorf("expected first index %d but got %d (error: %w)", 0, i, err))
954+ }
955+ if i, err := l.LastIndex(); err != nil || i != 0 {
956+ t.Fatal(fmt.Errorf("expected last index %d but got %d (error: %w)", 0, i, err))
957+ }
958+
959+ // Ensure we can continue to write after a clear
960+ for i := 1; i < 50; i++ {
961+ err := l.Write(uint64(i), []byte(dataStr(uint64(i))))
962+ if err != nil {
963+ t.Fatal(err)
964+ }
965+ }
966+}
967+
927968 func TestConcurrency(t *testing.T) {
928969 os.RemoveAll("testlog")
929970

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t