diff --git a/exporters/otlp/otlpmetric/internal/exporter_test.go b/exporters/otlp/otlpmetric/internal/exporter_test.go index 9f071032f..bdbe35138 100644 --- a/exporters/otlp/otlpmetric/internal/exporter_test.go +++ b/exporters/otlp/otlpmetric/internal/exporter_test.go @@ -64,16 +64,17 @@ func TestExporterClientConcurrentSafe(t *testing.T) { ctx := context.Background() done := make(chan struct{}) - first := make(chan struct{}, goroutines) - var wg sync.WaitGroup + var wg, someWork sync.WaitGroup for i := 0; i < goroutines; i++ { wg.Add(1) + someWork.Add(1) go func() { defer wg.Done() assert.NoError(t, exp.Export(ctx, rm)) assert.NoError(t, exp.ForceFlush(ctx)) + // Ensure some work is done before shutting down. - first <- struct{}{} + someWork.Done() for { _ = exp.Export(ctx, rm) @@ -88,10 +89,7 @@ func TestExporterClientConcurrentSafe(t *testing.T) { }() } - for i := 0; i < goroutines; i++ { - <-first - } - close(first) + someWork.Wait() assert.NoError(t, exp.Shutdown(ctx)) assert.ErrorIs(t, exp.Shutdown(ctx), errShutdown) diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/exporter_test.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/exporter_test.go index fb3e9fe02..6bb91b6ca 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/exporter_test.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/exporter_test.go @@ -47,16 +47,17 @@ func TestExporterClientConcurrentSafe(t *testing.T) { rm := new(metricdata.ResourceMetrics) done := make(chan struct{}) - first := make(chan struct{}, goroutines) - var wg sync.WaitGroup + var wg, someWork sync.WaitGroup for i := 0; i < goroutines; i++ { wg.Add(1) + someWork.Add(1) go func() { defer wg.Done() assert.NoError(t, exp.Export(ctx, rm)) assert.NoError(t, exp.ForceFlush(ctx)) + // Ensure some work is done before shutting down. - first <- struct{}{} + someWork.Done() for { _ = exp.Export(ctx, rm) @@ -71,10 +72,7 @@ func TestExporterClientConcurrentSafe(t *testing.T) { }() } - for i := 0; i < goroutines; i++ { - <-first - } - close(first) + someWork.Wait() assert.NoError(t, exp.Shutdown(ctx)) assert.ErrorIs(t, exp.Shutdown(ctx), errShutdown) diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/exporter_test.go b/exporters/otlp/otlpmetric/otlpmetrichttp/exporter_test.go index 9154d6276..4af2ab433 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/exporter_test.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/exporter_test.go @@ -47,16 +47,17 @@ func TestExporterClientConcurrentSafe(t *testing.T) { rm := new(metricdata.ResourceMetrics) done := make(chan struct{}) - first := make(chan struct{}, goroutines) - var wg sync.WaitGroup + var wg, someWork sync.WaitGroup for i := 0; i < goroutines; i++ { wg.Add(1) + someWork.Add(1) go func() { defer wg.Done() assert.NoError(t, exp.Export(ctx, rm)) assert.NoError(t, exp.ForceFlush(ctx)) + // Ensure some work is done before shutting down. - first <- struct{}{} + someWork.Done() for { _ = exp.Export(ctx, rm) @@ -71,10 +72,7 @@ func TestExporterClientConcurrentSafe(t *testing.T) { }() } - for i := 0; i < goroutines; i++ { - <-first - } - close(first) + someWork.Wait() assert.NoError(t, exp.Shutdown(ctx)) assert.ErrorIs(t, exp.Shutdown(ctx), errShutdown) diff --git a/sdk/metric/periodic_reader_test.go b/sdk/metric/periodic_reader_test.go index 6ac527a37..0a34b7e99 100644 --- a/sdk/metric/periodic_reader_test.go +++ b/sdk/metric/periodic_reader_test.go @@ -345,7 +345,7 @@ func TestPeriodicReaderFlushesPending(t *testing.T) { return nil }}) r.RegisterProducer(testExternalProducer{}) - assert.Equal(t, context.DeadlineExceeded, r.ForceFlush(context.Background()), "timeout error not returned") + assert.ErrorIs(t, r.ForceFlush(context.Background()), context.DeadlineExceeded) assert.False(t, *called, "exporter Export method called when it should have failed before export") // Ensure Reader is allowed clean up attempt. @@ -368,7 +368,7 @@ func TestPeriodicReaderFlushesPending(t *testing.T) { return []metricdata.ScopeMetrics{testScopeMetricsA}, nil }, }) - assert.Equal(t, context.DeadlineExceeded, r.ForceFlush(context.Background()), "timeout error not returned") + assert.ErrorIs(t, r.ForceFlush(context.Background()), context.DeadlineExceeded) assert.False(t, *called, "exporter Export method called when it should have failed before export") // Ensure Reader is allowed clean up attempt. @@ -400,7 +400,7 @@ func TestPeriodicReaderFlushesPending(t *testing.T) { return nil }}) r.RegisterProducer(testExternalProducer{}) - assert.Equal(t, context.DeadlineExceeded, r.Shutdown(context.Background()), "timeout error not returned") + assert.ErrorIs(t, r.Shutdown(context.Background()), context.DeadlineExceeded) assert.False(t, *called, "exporter Export method called when it should have failed before export") }) @@ -420,7 +420,7 @@ func TestPeriodicReaderFlushesPending(t *testing.T) { return []metricdata.ScopeMetrics{testScopeMetricsA}, nil }, }) - assert.Equal(t, context.DeadlineExceeded, r.Shutdown(context.Background()), "timeout error not returned") + assert.ErrorIs(t, r.Shutdown(context.Background()), context.DeadlineExceeded) assert.False(t, *called, "exporter Export method called when it should have failed before export") }) }