Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use T.TempDir to create temporary test directory #381

Merged
merged 2 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package segmentedfile

import (
// standard libraries.
Juneezee marked this conversation as resolved.
Show resolved Hide resolved
"os"
"testing"

// third-party libraries.
Expand All @@ -27,8 +25,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 +76,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)
})
}