1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-28 21:09:17 +02:00

Add concurrency test for Exporter to otlploghttp (#5183)

* Add concurrency test for Exporter to otlploghttp

* Update exporters/otlp/otlplog/otlploghttp/exporter_test.go

Co-authored-by: Robert Pająk <pellared@hotmail.com>

---------

Co-authored-by: Robert Pająk <pellared@hotmail.com>
This commit is contained in:
Tyler Yahn 2024-04-10 07:11:21 -07:00 committed by GitHub
parent 648b40eae1
commit b13ee4ae39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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()
}