Skip to content

Commit

Permalink
test: use T.TempDir to create temporary test directory (#381)
Browse files Browse the repository at this point in the history
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored Jan 6, 2023
1 parent 8e3373f commit 786b70f
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 132 deletions.
21 changes: 3 additions & 18 deletions internal/raft/log/compaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
// standard libraries.
"encoding/binary"
"fmt"
"os"
"testing"

// third-party libraries.
Expand All @@ -37,23 +36,9 @@ func TestLog_Compact(t *testing.T) {
data := make([]byte, blockSize)
copy(data, []byte("hello world!"))

metaDir, err := os.MkdirTemp("", "meta-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(metaDir)

offsetDir, err := os.MkdirTemp("", "offset-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(offsetDir)

walDir, err := os.MkdirTemp("", "wal-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(walDir)
metaDir := t.TempDir()
offsetDir := t.TempDir()
walDir := t.TempDir()

Convey("raft log compaction", t, func() {
metaStore, err := meta.RecoverSyncStore(stdCtx.Background(), metaCfg, metaDir)
Expand Down
21 changes: 3 additions & 18 deletions internal/raft/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
// standard libraries.
"context"
"math"
"os"
"testing"

// third-party libraries.
Expand Down Expand Up @@ -61,23 +60,9 @@ var (
func TestLog(t *testing.T) {
ctx := context.Background()

metaDir, err := os.MkdirTemp("", "meta-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(metaDir)

offsetDir, err := os.MkdirTemp("", "offset-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(offsetDir)

walDir, err := os.MkdirTemp("", "wal-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(walDir)
metaDir := t.TempDir()
offsetDir := t.TempDir()
walDir := t.TempDir()

cc1 := raftpb.ConfChange{
Type: raftpb.ConfChangeAddNode, NodeID: nodeID1.Uint64(),
Expand Down
21 changes: 3 additions & 18 deletions internal/raft/log/snapshot_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package log
import (
// standard libraries.
"context"
"os"
"testing"

// third-party libraries.
Expand All @@ -35,23 +34,9 @@ import (
func TestLog_SnapshotStorage(t *testing.T) {
ctx := context.Background()

metaDir, err := os.MkdirTemp("", "meta-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(metaDir)

offsetDir, err := os.MkdirTemp("", "offset-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(offsetDir)

walDir, err := os.MkdirTemp("", "wal-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(walDir)
metaDir := t.TempDir()
offsetDir := t.TempDir()
walDir := t.TempDir()

cc1 := raftpb.ConfChange{
Type: raftpb.ConfChangeAddNode, NodeID: nodeID1.Uint64(),
Expand Down
7 changes: 1 addition & 6 deletions internal/store/io/zone/segmentedfile/segmented_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package segmentedfile

import (
// standard libraries.
"os"
"testing"

// third-party libraries.
Expand All @@ -27,8 +26,7 @@ const fileSize = 32 * 1024

func TestSegmentedFile_SelectSegment(t *testing.T) {
Convey("segmented file", t, func() {
dir, err := os.MkdirTemp("", "sf-*")
So(err, ShouldBeNil)
dir := t.TempDir()

sf, err := Open(dir, WithSegmentSize(fileSize))
So(err, ShouldBeNil)
Expand Down Expand Up @@ -79,9 +77,6 @@ func TestSegmentedFile_SelectSegment(t *testing.T) {

Reset(func() {
sf.Close()

err = os.RemoveAll(dir)
So(err, ShouldBeNil)
})
})
}
9 changes: 1 addition & 8 deletions internal/store/meta/async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package meta
import (
// standard libraries.
"context"
"os"
"testing"

// third-party libraries.
Expand All @@ -31,8 +30,7 @@ func TestAsyncStore(t *testing.T) {
ctx := context.Background()

Convey("AsyncStore", t, func() {
walDir, err := os.MkdirTemp("", "async-*")
So(err, ShouldBeNil)
walDir := t.TempDir()

Convey("new empty AsyncStore by recovery", func() {
ss, err := RecoverAsyncStore(ctx, config.AsyncStore{}, walDir)
Expand Down Expand Up @@ -97,10 +95,5 @@ func TestAsyncStore(t *testing.T) {
})
})
})

Reset(func() {
err := os.RemoveAll(walDir)
So(err, ShouldBeNil)
})
})
}
9 changes: 1 addition & 8 deletions internal/store/meta/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package meta
import (
// standard libraries.
"context"
"os"
"testing"

// third-party libraries.
Expand All @@ -35,8 +34,7 @@ var (

func TestSyncStore(t *testing.T) {
Convey("SyncStore", t, func() {
walDir, err := os.MkdirTemp("", "sync-*")
So(err, ShouldBeNil)
walDir := t.TempDir()

Convey("new empty SyncStore by recovery", func() {
ss, err := RecoverSyncStore(context.Background(), config.SyncStore{}, walDir)
Expand Down Expand Up @@ -101,10 +99,5 @@ func TestSyncStore(t *testing.T) {
})
})
})

Reset(func() {
err := os.RemoveAll(walDir)
So(err, ShouldBeNil)
})
})
}
9 changes: 2 additions & 7 deletions internal/store/segment/recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package segment
import (
// standard libraries.
"context"
"os"
"testing"

// third-party libraries.
Expand All @@ -33,11 +32,7 @@ func TestServer_recover(t *testing.T) {
defer ctrl.Finish()

Convey("recover", t, func() {
dir, err := os.MkdirTemp("", "volume-*")
So(err, ShouldBeNil)
defer func() {
So(os.RemoveAll(dir), ShouldBeNil)
}()
dir := t.TempDir()

srv := &server{
cfg: store.Config{
Expand All @@ -46,7 +41,7 @@ func TestServer_recover(t *testing.T) {
},
},
}
err = srv.loadVSBEngine(context.Background(), srv.cfg.VSB)
err := srv.loadVSBEngine(context.Background(), srv.cfg.VSB)
So(err, ShouldBeNil)

err = srv.recover(context.Background())
Expand Down
9 changes: 1 addition & 8 deletions internal/store/wal/recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package wal
import (
// standard libraries.
"context"
"os"
"testing"

// third-party libraries.
Expand All @@ -28,8 +27,7 @@ func TestOpen(t *testing.T) {
ctx := context.Background()

Convey("open wal in recover mode", t, func() {
walDir, err := os.MkdirTemp("", "wal-*")
So(err, ShouldBeNil)
walDir := t.TempDir()

Convey("create empty wal", func() {
var entryNum int
Expand Down Expand Up @@ -107,10 +105,5 @@ func TestOpen(t *testing.T) {
wal.Close()
wal.Wait()
})

Reset(func() {
err := os.RemoveAll(walDir)
So(err, ShouldBeNil)
})
})
}
19 changes: 3 additions & 16 deletions internal/store/wal/wal_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"fmt"
"log"
"os"
"sync"
"testing"

Expand All @@ -30,11 +29,7 @@ import (
const walTempDir = ""

func BenchmarkWAL_AppendOneWithBatching(b *testing.B) {
walDir, err := os.MkdirTemp(walTempDir, "wal-*")
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(walDir)
walDir := b.TempDir()

wal, err := Open(context.Background(), walDir)
if err != nil {
Expand Down Expand Up @@ -63,11 +58,7 @@ func BenchmarkWAL_AppendOneWithBatching(b *testing.B) {
}

func BenchmarkWAL_AppendOneWithoutBatching(b *testing.B) {
walDir, err := os.MkdirTemp(walTempDir, "wal-*")
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(walDir)
walDir := b.TempDir()

wal, err := Open(context.Background(), walDir)
if err != nil {
Expand Down Expand Up @@ -97,11 +88,7 @@ func BenchmarkWAL_AppendOneWithoutBatching(b *testing.B) {

func BenchmarkWAL_AppendOneWithCallback(b *testing.B) {
// walDir := filepath.Join(walTempDir, "wal-test")
walDir, err := os.MkdirTemp(walTempDir, "wal-*")
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(walDir)
walDir := b.TempDir()

wal, err := Open(context.Background(), walDir)
if err != nil {
Expand Down
30 changes: 5 additions & 25 deletions internal/store/wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func TestWAL_AppendOne(t *testing.T) {
ctx := context.Background()

Convey("wal append one", t, func() {
walDir, err := os.MkdirTemp("", "wal-*")
So(err, ShouldBeNil)
walDir := t.TempDir()

wal, err := Open(ctx, walDir, WithFileSize(fileSize))
So(err, ShouldBeNil)
Expand Down Expand Up @@ -86,15 +85,11 @@ func TestWAL_AppendOne(t *testing.T) {
Reset(func() {
wal.Close()
wal.Wait()

err := os.RemoveAll(walDir)
So(err, ShouldBeNil)
})
})

Convey("flush wal when timeout", t, func() {
walDir, err := os.MkdirTemp("", "wal-*")
So(err, ShouldBeNil)
walDir := t.TempDir()

flushTimeout := time.Second
wal, err := Open(ctx, walDir, WithFileSize(fileSize), WithFlushTimeout(flushTimeout))
Expand All @@ -120,14 +115,10 @@ func TestWAL_AppendOne(t *testing.T) {

wal.Close()
wal.Wait()

err = os.RemoveAll(walDir)
So(err, ShouldBeNil)
})

Convey("wal append one after close", t, func() {
walDir, err := os.MkdirTemp("", "wal-*")
So(err, ShouldBeNil)
walDir := t.TempDir()

flushTimeout := 100 * time.Millisecond
wal, err := Open(ctx, walDir, WithFileSize(fileSize), WithFlushTimeout(flushTimeout))
Expand All @@ -153,18 +144,14 @@ func TestWAL_AppendOne(t *testing.T) {
// NOTE: There is no guarantee that data0 will be successfully written.
// So(wal.wb.Size(), ShouldEqual, 10)
// So(wal.wb.Committed(), ShouldEqual, 10)

err = os.RemoveAll(walDir)
So(err, ShouldBeNil)
})
}

func TestWAL_Append(t *testing.T) {
ctx := context.Background()

Convey("wal append", t, func() {
walDir, err := os.MkdirTemp("", "wal-*")
So(err, ShouldBeNil)
walDir := t.TempDir()

wal, err := Open(ctx, walDir, WithFileSize(fileSize))
So(err, ShouldBeNil)
Expand Down Expand Up @@ -194,9 +181,6 @@ func TestWAL_Append(t *testing.T) {
Reset(func() {
wal.Close()
wal.Wait()

err := os.RemoveAll(walDir)
So(err, ShouldBeNil)
})
})
}
Expand All @@ -207,8 +191,7 @@ func TestWAL_Compact(t *testing.T) {
copy(data, []byte("hello world!"))

Convey("wal compaction", t, func() {
walDir, err := os.MkdirTemp("", "wal-*")
So(err, ShouldBeNil)
walDir := t.TempDir()

wal, err := Open(ctx, walDir, WithFileSize(fileSize))
So(err, ShouldBeNil)
Expand Down Expand Up @@ -244,8 +227,5 @@ func TestWAL_Compact(t *testing.T) {

wal.Close()
wal.Wait()

err = os.RemoveAll(walDir)
So(err, ShouldBeNil)
})
}

0 comments on commit 786b70f

Please sign in to comment.