1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-12-01 23:12:29 +02:00

Fix sporadic test failure in otlp exporter http driver (#1906)

* Fix sporadic test failure in otlp exporter http driver

* Shorten driver timeout
This commit is contained in:
Tyler Yahn
2021-05-13 16:10:03 +00:00
committed by GitHub
parent a3df00f49c
commit d8ac212c02
2 changed files with 20 additions and 13 deletions

View File

@@ -171,15 +171,15 @@ func TestRetry(t *testing.T) {
}
func TestTimeout(t *testing.T) {
mcCfg := mockCollectorConfig{
InjectDelay: 100 * time.Millisecond,
}
delay := make(chan struct{})
mcCfg := mockCollectorConfig{Delay: delay}
mc := runMockCollector(t, mcCfg)
defer mc.MustStop(t)
defer func() { close(delay) }()
driver := otlphttp.NewDriver(
otlphttp.WithEndpoint(mc.Endpoint()),
otlphttp.WithInsecure(),
otlphttp.WithTimeout(50*time.Millisecond),
otlphttp.WithTimeout(time.Nanosecond),
)
ctx := context.Background()
exporter, err := otlp.NewExporter(ctx, driver)
@@ -188,7 +188,7 @@ func TestTimeout(t *testing.T) {
assert.NoError(t, exporter.Shutdown(ctx))
}()
err = exporter.ExportSpans(ctx, otlptest.SingleReadOnlySpan())
assert.Equal(t, true, os.IsTimeout(err))
assert.Equalf(t, true, os.IsTimeout(err), "expected timeout error, got: %v", err)
}
func TestRetryFailed(t *testing.T) {

View File

@@ -26,7 +26,6 @@ import (
"net/http"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -53,7 +52,7 @@ type mockCollector struct {
injectHTTPStatus []int
injectContentType string
injectDelay time.Duration
delay <-chan struct{}
clientTLSConfig *tls.Config
expectedHeaders map[string]string
@@ -94,8 +93,12 @@ func (c *mockCollector) ClientTLSConfig() *tls.Config {
}
func (c *mockCollector) serveMetrics(w http.ResponseWriter, r *http.Request) {
if c.injectDelay != 0 {
time.Sleep(c.injectDelay)
if c.delay != nil {
select {
case <-c.delay:
case <-r.Context().Done():
return
}
}
if !c.checkHeaders(r) {
@@ -139,8 +142,12 @@ func unmarshalMetricsRequest(rawRequest []byte, contentType string) (*collectorm
}
func (c *mockCollector) serveTraces(w http.ResponseWriter, r *http.Request) {
if c.injectDelay != 0 {
time.Sleep(c.injectDelay)
if c.delay != nil {
select {
case <-c.delay:
case <-r.Context().Done():
return
}
}
if !c.checkHeaders(r) {
@@ -247,7 +254,7 @@ type mockCollectorConfig struct {
Port int
InjectHTTPStatus []int
InjectContentType string
InjectDelay time.Duration
Delay <-chan struct{}
WithTLS bool
ExpectedHeaders map[string]string
}
@@ -273,7 +280,7 @@ func runMockCollector(t *testing.T, cfg mockCollectorConfig) *mockCollector {
metricsStorage: otlptest.NewMetricsStorage(),
injectHTTPStatus: cfg.InjectHTTPStatus,
injectContentType: cfg.InjectContentType,
injectDelay: cfg.InjectDelay,
delay: cfg.Delay,
expectedHeaders: cfg.ExpectedHeaders,
}
mux := http.NewServeMux()