Skip to content

Commit

Permalink
Avoid errors when closing while compacting
Browse files Browse the repository at this point in the history
  • Loading branch information
aalda committed Nov 25, 2019
1 parent e3b0600 commit b716686
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 36 deletions.
48 changes: 32 additions & 16 deletions badger_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ func TestNewBadgerStore(t *testing.T) {

func TestBadgerStore_FirstIndex(t *testing.T) {
store, path := testBadgerStore(t)
defer store.Close()
defer os.RemoveAll(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

// Should get 0 index on empty log
idx, err := store.FirstIndex()
Expand Down Expand Up @@ -163,8 +165,10 @@ func TestBadgerStore_FirstIndex(t *testing.T) {

func TestBadgerStore_LastIndex(t *testing.T) {
store, path := testBadgerStore(t)
defer store.Close()
defer os.RemoveAll(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

// Should get 0 index on empty log
idx, err := store.LastIndex()
Expand Down Expand Up @@ -197,8 +201,10 @@ func TestBadgerStore_LastIndex(t *testing.T) {

func TestBadgerStore_GetLog(t *testing.T) {
store, path := testBadgerStore(t)
defer store.Close()
defer os.RemoveAll(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

log := new(raft.Log)

Expand Down Expand Up @@ -228,8 +234,10 @@ func TestBadgerStore_GetLog(t *testing.T) {

func TestBadgerStore_SetLog(t *testing.T) {
store, path := testBadgerStore(t)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

// Create the log
log := &raft.Log{
Expand All @@ -256,8 +264,10 @@ func TestBadgerStore_SetLog(t *testing.T) {

func TestBadgerStore_SetLogs(t *testing.T) {
store, path := testBadgerStore(t)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

// Create a set of logs
logs := []*raft.Log{
Expand Down Expand Up @@ -288,8 +298,10 @@ func TestBadgerStore_SetLogs(t *testing.T) {

func TestBadgerStore_DeleteRange(t *testing.T) {
store, path := testBadgerStore(t)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

// Create a set of logs
log1 := testRaftLog(1, "log1")
Expand Down Expand Up @@ -318,8 +330,10 @@ func TestBadgerStore_DeleteRange(t *testing.T) {

func TestBadgerStore_Set_Get(t *testing.T) {
store, path := testBadgerStore(t)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

// Returns error on non-existent key
if _, err := store.Get([]byte("bad")); err != ErrKeyNotFound {
Expand All @@ -345,8 +359,10 @@ func TestBadgerStore_Set_Get(t *testing.T) {

func TestBadgerStore_SetUint64_GetUint64(t *testing.T) {
store, path := testBadgerStore(t)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

// Returns error on non-existent key
if _, err := store.GetUint64([]byte("bad")); err != ErrKeyNotFound {
Expand Down
60 changes: 40 additions & 20 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,80 +25,100 @@ import (

func BenchmarkBadgerStore_FirstIndex(b *testing.B) {
store, path := testBadgerStore(b)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

raftbench.FirstIndex(b, store)
}

func BenchmarkBadgerStore_LastIndex(b *testing.B) {
store, path := testBadgerStore(b)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

raftbench.LastIndex(b, store)
}

func BenchmarkBadgerStore_GetLog(b *testing.B) {
store, path := testBadgerStore(b)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

raftbench.GetLog(b, store)
}

func BenchmarkBadgerStore_StoreLog(b *testing.B) {
store, path := testBadgerStore(b)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

raftbench.StoreLog(b, store)
}

func BenchmarkBadgerStore_StoreLogs(b *testing.B) {
store, path := testBadgerStore(b)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

raftbench.StoreLogs(b, store)
}

func BenchmarkBadgerStore_DeleteRange(b *testing.B) {
store, path := testBadgerStore(b)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

raftbench.DeleteRange(b, store)
}

func BenchmarkBadgerStore_Set(b *testing.B) {
store, path := testBadgerStore(b)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

raftbench.Set(b, store)
}

func BenchmarkBadgerStore_Get(b *testing.B) {
store, path := testBadgerStore(b)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

raftbench.Get(b, store)
}

func BenchmarkBadgerStore_SetUint64(b *testing.B) {
store, path := testBadgerStore(b)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

raftbench.SetUint64(b, store)
}

func BenchmarkBadgerStore_GetUint64(b *testing.B) {
store, path := testBadgerStore(b)
defer store.Close()
defer os.Remove(path)
defer func() {
store.Close()
os.RemoveAll(path)
}()

raftbench.GetUint64(b, store)
}

0 comments on commit b716686

Please sign in to comment.