From 47b008fc2aeaeae3e026213686773030f54d6637 Mon Sep 17 00:00:00 2001 From: tangcong Date: Wed, 20 May 2020 20:43:48 +0800 Subject: [PATCH] wal: add TestValidSnapshotEntriesAfterPurgeWal testcase --- wal/wal_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/wal/wal_test.go b/wal/wal_test.go index 068b05334b2b..b4a105a47195 100644 --- a/wal/wal_test.go +++ b/wal/wal_test.go @@ -1052,3 +1052,56 @@ func TestValidSnapshotEntries(t *testing.T) { t.Errorf("expected walSnaps %+v, got %+v", expected, walSnaps) } } + +// TestValidSnapshotEntriesAfterPurgeWal ensure that there are many wal files, and after cleaning the first wal file, +// it can work well. +func TestValidSnapshotEntriesAfterPurgeWal(t *testing.T) { + SegmentSizeBytes = 64 + p, err := ioutil.TempDir(os.TempDir(), "waltest") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(p) + snap0 := walpb.Snapshot{Index: 0, Term: 0} + snap1 := walpb.Snapshot{Index: 1, Term: 1} + state1 := raftpb.HardState{Commit: 1, Term: 1} + snap2 := walpb.Snapshot{Index: 2, Term: 1} + snap3 := walpb.Snapshot{Index: 3, Term: 2} + state2 := raftpb.HardState{Commit: 3, Term: 2} + func() { + w, err := Create(zap.NewExample(), p, nil) + if err != nil { + t.Fatal(err) + } + defer w.Close() + + // snap0 is implicitly created at index 0, term 0 + if err = w.SaveSnapshot(snap1); err != nil { + t.Fatal(err) + } + if err = w.Save(state1, nil); err != nil { + t.Fatal(err) + } + if err = w.SaveSnapshot(snap2); err != nil { + t.Fatal(err) + } + if err = w.SaveSnapshot(snap3); err != nil { + t.Fatal(err) + } + for i := 0; i < 128; i++ { + if err = w.Save(state2, nil); err != nil { + t.Fatal(err) + } + } + + }() + files, _, err := selectWALFiles(nil, p, snap0) + if err != nil { + t.Fatal(err) + } + os.Remove(p + "/" + files[0]) + _, err = ValidSnapshotEntries(zap.NewExample(), p) + if err != nil { + t.Fatal(err) + } +}