Skip to content

Commit

Permalink
Use t.TempDir instead of ioutil + defer remove (#5470)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Jun 6, 2022
1 parent 1878e41 commit 0347b25
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 30 deletions.
10 changes: 3 additions & 7 deletions exporter/exporterhelper/internal/persistent_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package internal
import (
"context"
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -48,8 +47,7 @@ func createTestQueue(extension storage.Extension, capacity int) *persistentQueue
}

func TestPersistentQueue_Capacity(t *testing.T) {
path := createTemporaryDirectory()
defer os.RemoveAll(path)
path := t.TempDir()

for i := 0; i < 100; i++ {
ext := createStorageExtension(path)
Expand Down Expand Up @@ -82,8 +80,7 @@ func TestPersistentQueue_Capacity(t *testing.T) {
}

func TestPersistentQueue_Close(t *testing.T) {
path := createTemporaryDirectory()
defer os.RemoveAll(path)
path := t.TempDir()

ext := createStorageExtension(path)
t.Cleanup(func() { require.NoError(t, ext.Shutdown(context.Background())) })
Expand Down Expand Up @@ -138,15 +135,14 @@ func TestPersistentQueue_ConsumersProducers(t *testing.T) {

for _, c := range cases {
t.Run(fmt.Sprintf("#messages: %d #consumers: %d", c.numMessagesProduced, c.numConsumers), func(t *testing.T) {
path := createTemporaryDirectory()
path := t.TempDir()

traces := newTraces(1, 10)
req := newFakeTracesRequest(traces)

ext := createStorageExtension(path)
tq := createTestQueue(ext, 5000)

defer os.RemoveAll(path)
defer tq.Stop()
t.Cleanup(func() { require.NoError(t, ext.Shutdown(context.Background())) })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ package internal

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestPersistentStorageBatch_Operations(t *testing.T) {
path := createTemporaryDirectory()
defer os.RemoveAll(path)
path := t.TempDir()

ext := createStorageExtension(path)
client := createTestClient(ext)
Expand Down
25 changes: 5 additions & 20 deletions exporter/exporterhelper/internal/persistent_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"reflect"
"sync"
"testing"
Expand Down Expand Up @@ -59,14 +57,6 @@ func createTestPersistentStorage(client storage.Client) *persistentContiguousSto
return createTestPersistentStorageWithLoggingAndCapacity(client, logger, 1000)
}

func createTemporaryDirectory() string {
directory, err := ioutil.TempDir("", "persistent-test")
if err != nil {
panic(err)
}
return directory
}

type fakeTracesRequest struct {
td ptrace.Traces
processingFinishedCallback func()
Expand Down Expand Up @@ -104,8 +94,7 @@ func newFakeTracesRequestUnmarshalerFunc() RequestUnmarshaler {
}

func TestPersistentStorage_CorruptedData(t *testing.T) {
path := createTemporaryDirectory()
defer os.RemoveAll(path)
path := t.TempDir()

traces := newTraces(5, 10)
req := newFakeTracesRequest(traces)
Expand Down Expand Up @@ -221,8 +210,7 @@ func TestPersistentStorage_CorruptedData(t *testing.T) {
}

func TestPersistentStorage_CurrentlyProcessedItems(t *testing.T) {
path := createTemporaryDirectory()
defer os.RemoveAll(path)
path := t.TempDir()

traces := newTraces(5, 10)
req := newFakeTracesRequest(traces)
Expand Down Expand Up @@ -286,8 +274,7 @@ func TestPersistentStorage_CurrentlyProcessedItems(t *testing.T) {
}

func TestPersistentStorage_RepeatPutCloseReadClose(t *testing.T) {
path := createTemporaryDirectory()
defer os.RemoveAll(path)
path := t.TempDir()

traces := newTraces(5, 10)
req := newFakeTracesRequest(traces)
Expand Down Expand Up @@ -336,8 +323,7 @@ func TestPersistentStorage_RepeatPutCloseReadClose(t *testing.T) {
}

func TestPersistentStorage_EmptyRequest(t *testing.T) {
path := createTemporaryDirectory()
defer os.RemoveAll(path)
path := t.TempDir()

ext := createStorageExtension(path)
client := createTestClient(ext)
Expand Down Expand Up @@ -375,8 +361,7 @@ func BenchmarkPersistentStorage_TraceSpans(b *testing.B) {

for _, c := range cases {
b.Run(fmt.Sprintf("#traces: %d #spansPerTrace: %d", c.numTraces, c.numSpansPerTrace), func(bb *testing.B) {
path := createTemporaryDirectory()
defer os.RemoveAll(path)
path := bb.TempDir()
ext := createStorageExtension(path)
client := createTestClient(ext)
ps := createTestPersistentStorageWithLoggingAndCapacity(client, zap.NewNop(), 10000000)
Expand Down

0 comments on commit 0347b25

Please sign in to comment.