Skip to content

Commit

Permalink
wal: fix panic when decoder not set
Browse files Browse the repository at this point in the history
Handle the related panic and clarify doc.
  • Loading branch information
spzala committed Jun 27, 2020
1 parent 3076b61 commit fcab17d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
6 changes: 1 addition & 5 deletions auth/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 7 additions & 1 deletion wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 24 additions & 2 deletions wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"io"
"io/ioutil"
"math"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
}

0 comments on commit fcab17d

Please sign in to comment.