From b7166863e0ecbfbc1ca235f203b1055c5b64ff9d Mon Sep 17 00:00:00 2001 From: Alvaro Alda Date: Mon, 25 Nov 2019 19:56:41 +0100 Subject: [PATCH] Avoid errors when closing while compacting --- badger_store_test.go | 48 +++++++++++++++++++++++------------ bench_test.go | 60 +++++++++++++++++++++++++++++--------------- 2 files changed, 72 insertions(+), 36 deletions(-) diff --git a/badger_store_test.go b/badger_store_test.go index db1ca48..7d63c6b 100644 --- a/badger_store_test.go +++ b/badger_store_test.go @@ -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() @@ -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() @@ -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) @@ -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{ @@ -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{ @@ -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") @@ -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 { @@ -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 { diff --git a/bench_test.go b/bench_test.go index 8f30808..09adc07 100644 --- a/bench_test.go +++ b/bench_test.go @@ -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) }