From 0347b251dfe7e783abc77e4be36abe6b0068a012 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Mon, 6 Jun 2022 10:40:45 -0700 Subject: [PATCH] Use t.TempDir instead of ioutil + defer remove (#5470) Signed-off-by: Bogdan Drutu --- .../internal/persistent_queue_test.go | 10 +++----- .../internal/persistent_storage_batch_test.go | 4 +-- .../internal/persistent_storage_test.go | 25 ++++--------------- 3 files changed, 9 insertions(+), 30 deletions(-) diff --git a/exporter/exporterhelper/internal/persistent_queue_test.go b/exporter/exporterhelper/internal/persistent_queue_test.go index 6189c0375cd..417d0fa1ef9 100644 --- a/exporter/exporterhelper/internal/persistent_queue_test.go +++ b/exporter/exporterhelper/internal/persistent_queue_test.go @@ -20,7 +20,6 @@ package internal import ( "context" "fmt" - "os" "testing" "time" @@ -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) @@ -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())) }) @@ -138,7 +135,7 @@ 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) @@ -146,7 +143,6 @@ func TestPersistentQueue_ConsumersProducers(t *testing.T) { ext := createStorageExtension(path) tq := createTestQueue(ext, 5000) - defer os.RemoveAll(path) defer tq.Stop() t.Cleanup(func() { require.NoError(t, ext.Shutdown(context.Background())) }) diff --git a/exporter/exporterhelper/internal/persistent_storage_batch_test.go b/exporter/exporterhelper/internal/persistent_storage_batch_test.go index 74dfdb32065..9a7aef221b8 100644 --- a/exporter/exporterhelper/internal/persistent_storage_batch_test.go +++ b/exporter/exporterhelper/internal/persistent_storage_batch_test.go @@ -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) diff --git a/exporter/exporterhelper/internal/persistent_storage_test.go b/exporter/exporterhelper/internal/persistent_storage_test.go index 81a41aac7ef..a08231fda65 100644 --- a/exporter/exporterhelper/internal/persistent_storage_test.go +++ b/exporter/exporterhelper/internal/persistent_storage_test.go @@ -21,8 +21,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" - "os" "reflect" "sync" "testing" @@ -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() @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)