From b13ee4ae393c95f737393167343c090c559630de Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 10 Apr 2024 07:11:21 -0700 Subject: [PATCH] Add concurrency test for Exporter to otlploghttp (#5183) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add concurrency test for Exporter to otlploghttp * Update exporters/otlp/otlplog/otlploghttp/exporter_test.go Co-authored-by: Robert Pająk --------- Co-authored-by: Robert Pająk --- .../otlp/otlplog/otlploghttp/exporter_test.go | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/exporters/otlp/otlplog/otlploghttp/exporter_test.go b/exporters/otlp/otlplog/otlploghttp/exporter_test.go index 2474836c84a..d818739a070 100644 --- a/exporters/otlp/otlplog/otlploghttp/exporter_test.go +++ b/exporters/otlp/otlplog/otlploghttp/exporter_test.go @@ -5,6 +5,9 @@ package otlploghttp import ( "context" + "runtime" + "sync" + "sync/atomic" "testing" "github.com/stretchr/testify/assert" @@ -34,3 +37,41 @@ func TestExporterForceFlush(t *testing.T) { assert.NoError(t, e.ForceFlush(ctx), "ForceFlush") } + +func TestExporterConcurrentSafe(t *testing.T) { + ctx := context.Background() + e, err := New(ctx) + require.NoError(t, err, "newExporter") + + const goroutines = 10 + + var wg sync.WaitGroup + ctx, cancel := context.WithCancel(context.Background()) + runs := new(uint64) + for i := 0; i < goroutines; i++ { + wg.Add(1) + go func() { + defer wg.Done() + + r := make([]log.Record, 1) + for { + select { + case <-ctx.Done(): + return + default: + _ = e.Export(ctx, r) + _ = e.ForceFlush(ctx) + atomic.AddUint64(runs, 1) + } + } + }() + } + + for atomic.LoadUint64(runs) == 0 { + runtime.Gosched() + } + + _ = e.Shutdown(ctx) + cancel() + wg.Wait() +}