From 41cf7418bb5ad0cc278105a9c98d14be18f881d7 Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Wed, 3 Jan 2024 14:54:42 -0800 Subject: [PATCH 1/2] [chore][exporter/exporterhelper] Enable goleak test This enables goleak to run on tests within exporter/exporterhelper. A known leak needs to be ignored, and we need to explicitly call shutdown on the queue to make sure no routines are still running after the test is complete. --- exporter/exporterhelper/package_test.go | 17 +++++++++++++++++ exporter/exporterhelper/queue_sender_test.go | 1 + 2 files changed, 18 insertions(+) create mode 100644 exporter/exporterhelper/package_test.go diff --git a/exporter/exporterhelper/package_test.go b/exporter/exporterhelper/package_test.go new file mode 100644 index 00000000000..0980e46319f --- /dev/null +++ b/exporter/exporterhelper/package_test.go @@ -0,0 +1,17 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package exporterhelper + +import ( + "testing" + + "go.uber.org/goleak" +) + +// The IgnoreTopFunction call prevents catching the leak generated by opencensus +// defaultWorker.Start which at this time is part of the package's init call. +// See https://github.com/open-telemetry/opentelemetry-collector/issues/9165#issuecomment-1874836336 for more context. +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m, goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start")) +} diff --git a/exporter/exporterhelper/queue_sender_test.go b/exporter/exporterhelper/queue_sender_test.go index fe0633a605d..0af3c37b354 100644 --- a/exporter/exporterhelper/queue_sender_test.go +++ b/exporter/exporterhelper/queue_sender_test.go @@ -328,6 +328,7 @@ func TestQueuedRetryPersistentEnabled_NoDataLossOnShutdown(t *testing.T) { // wait for the item to be consumed from the queue replacedReq.checkNumRequests(t, 1) + require.NoError(t, be.Shutdown(context.Background())) } func TestQueueSenderNoStartShutdown(t *testing.T) { From aafc28a7a9769c55046a2d53a79efd78f22be66e Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Thu, 4 Jan 2024 08:48:40 -0800 Subject: [PATCH 2/2] Use t.Cleanup to make sure shutdown is called. --- exporter/exporterhelper/queue_sender_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/exporterhelper/queue_sender_test.go b/exporter/exporterhelper/queue_sender_test.go index 0af3c37b354..e6d4a1852d9 100644 --- a/exporter/exporterhelper/queue_sender_test.go +++ b/exporter/exporterhelper/queue_sender_test.go @@ -325,10 +325,10 @@ func TestQueuedRetryPersistentEnabled_NoDataLossOnShutdown(t *testing.T) { newNoopObsrepSender, WithRetry(rCfg), WithQueue(qCfg)) require.NoError(t, err) require.NoError(t, be.Start(context.Background(), host)) + t.Cleanup(func() { require.NoError(t, be.Shutdown(context.Background())) }) // wait for the item to be consumed from the queue replacedReq.checkNumRequests(t, 1) - require.NoError(t, be.Shutdown(context.Background())) } func TestQueueSenderNoStartShutdown(t *testing.T) {