diff --git a/auth/store_test.go b/auth/store_test.go index b447a2c2f8f0..9c1e49da4c54 100644 --- a/auth/store_test.go +++ b/auth/store_test.go @@ -608,11 +608,7 @@ func TestRecoverFromSnapshot(t *testing.T) { as.Close() -<<<<<<< HEAD - tp, err := NewTokenProvider(tokenTypeSimple, dummyIndexWaiter) -======= - tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter, simpleTokenTTLDefault) ->>>>>>> auth: Customize simpleTokenTTL settings. + tp, err := NewTokenProvider(tokenTypeSimple, dummyIndexWaiter, simpleTokenTTLDefault) if err != nil { t.Fatal(err) } diff --git a/wal/wal.go b/wal/wal.go index d4e48350e3d3..f1ffc4326b65 100644 --- a/wal/wal.go +++ b/wal/wal.go @@ -62,6 +62,7 @@ var ( ErrSnapshotNotFound = errors.New("wal: snapshot not found") ErrSliceOutOfRange = errors.New("wal: slice bounds out of range") ErrMaxWALEntrySizeLimitExceeded = errors.New("wal: max entry size limit exceeded") + ErrDecoderNotFound = errors.New("wal: decoder not found") crcTable = crc32.MakeTable(crc32.Castagnoli) ) @@ -92,7 +93,8 @@ type WAL struct { } // Create creates a WAL ready for appending records. The given metadata is -// recorded at the head of each WAL file, and can be retrieved with ReadAll. +// recorded at the head of each WAL file, and can be retrieved with ReadAll +// after the file is Open. func Create(dirpath string, metadata []byte) (*WAL, error) { if Exist(dirpath) { return nil, os.ErrExist @@ -323,6 +325,10 @@ func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb. defer w.mu.Unlock() rec := &walpb.Record{} + + if w.decoder == nil { + return nil, state, nil, ErrDecoderNotFound + } decoder := w.decoder var match bool diff --git a/wal/wal_test.go b/wal/wal_test.go index 277d4bb9c7cc..766625d2747f 100644 --- a/wal/wal_test.go +++ b/wal/wal_test.go @@ -18,6 +18,7 @@ import ( "bytes" "io" "io/ioutil" + "math" "os" "path" "path/filepath" @@ -583,7 +584,7 @@ func TestOpenWithMaxIndex(t *testing.T) { } defer os.RemoveAll(p) // create WAL - w, err := Create(zap.NewExample(), p, nil) + w, err := Create(p, nil) if err != nil { t.Fatal(err) } @@ -595,7 +596,7 @@ func TestOpenWithMaxIndex(t *testing.T) { } w.Close() - w, err = Open(zap.NewExample(), p, walpb.Snapshot{}) + w, err = Open(p, walpb.Snapshot{}) if err != nil { t.Fatal(err) } @@ -990,3 +991,24 @@ func TestValidSnapshotEntriesAfterPurgeWal(t *testing.T) { t.Fatal(err) } } + +// TestReadAllFail ensure ReadAll error if used without opening the WAL +func TestReadAllFail(t *testing.T) { + dir, err := ioutil.TempDir(os.TempDir(), "waltest") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + // create initial WAL + f, err := Create(dir, []byte("metadata")) + if err != nil { + t.Fatal(err) + } + f.Close() + // try to read without opening the WAL + _, _, _, err = f.ReadAll() + if err == nil || err != ErrDecoderNotFound { + t.Fatalf("err = %v, want ErrDecoderNotFound", err) + } +}