Skip to content

Commit

Permalink
wal: add a test for wal cleanup, improve comments
Browse files Browse the repository at this point in the history
To add test coverage of wal cleanup.
  • Loading branch information
joshcc3 committed May 10, 2019
1 parent f7f7e9c commit 30e1b17
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ func (w *WAL) cleanupWAL(lg *zap.Logger) {
var err error
if err = w.Close(); err != nil {
if lg != nil {
lg.Panic("failed to closeup WAL during cleanup", zap.Error(err))
lg.Panic("failed to close WAL during cleanup", zap.Error(err))
} else {
plog.Panicf("failed to closeup WAL during cleanup: %v", err)
plog.Panicf("failed to close WAL during cleanup: %v", err)
}
}
brokenDirName := fmt.Sprintf("%s.broken.%v", w.dir, time.Now().Format("20060102.150405.999999"))
Expand Down
33 changes: 33 additions & 0 deletions wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ package wal

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"testing"

"go.etcd.io/etcd/v3/pkg/fileutil"
Expand Down Expand Up @@ -101,6 +103,37 @@ func TestCreateFailFromPollutedDir(t *testing.T) {
}
}

func TestWalCleanup(t *testing.T) {
testRoot, err := ioutil.TempDir(os.TempDir(), "waltestroot")
if err != nil {
t.Fatal(err)
}
p, err := ioutil.TempDir(testRoot, "waltest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(testRoot)

logger := zap.NewExample()
w, err := Create(logger, p, []byte(""))
if err != nil {
t.Fatalf("err = %v, want nil", err)
}
w.cleanupWAL(logger)
fnames, err := fileutil.ReadDir(testRoot)
if err != nil {
t.Fatalf("err = %v, want nil", err)
}
if len(fnames) != 1 {
t.Fatalf("expected 1 file under %v, got %v", testRoot, len(fnames))
}
pattern := fmt.Sprintf(`%s.broken\.[\d]{8}\.[\d]{6}\.[\d]{6}`, filepath.Base(p))
match, _ := regexp.MatchString(pattern, fnames[0])
if !match {
t.Fatalf("match = false, expected true for %v with pattern %v", fnames[0], pattern)
}
}

func TestCreateFailFromNoSpaceLeft(t *testing.T) {
p, err := ioutil.TempDir(os.TempDir(), "waltest")
if err != nil {
Expand Down

0 comments on commit 30e1b17

Please sign in to comment.