Skip to content

Commit

Permalink
Fix sporadic test failure in otlp exporter http client tests (#2496)
Browse files Browse the repository at this point in the history
* Fix flaky TestTimeout in otlpmetrichttp

* Fix flaky TestTimeout in otlptracehttp
  • Loading branch information
MrAlias committed Jan 6, 2022
1 parent 1c1e846 commit 11ce2dd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
10 changes: 5 additions & 5 deletions exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go
Expand Up @@ -144,15 +144,15 @@ func TestExporterShutdown(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) }()
client := otlpmetrichttp.NewClient(
otlpmetrichttp.WithEndpoint(mc.Endpoint()),
otlpmetrichttp.WithInsecure(),
otlpmetrichttp.WithTimeout(50*time.Millisecond),
otlpmetrichttp.WithTimeout(time.Nanosecond),
)
ctx := context.Background()
exporter, err := otlpmetric.New(ctx, client)
Expand All @@ -161,7 +161,7 @@ func TestTimeout(t *testing.T) {
assert.NoError(t, exporter.Shutdown(ctx))
}()
err = exporter.Export(ctx, testResource, oneRecord)
assert.Equal(t, true, os.IsTimeout(err))
assert.Equalf(t, true, os.IsTimeout(err), "expected timeout error, got: %v", err)
}

func TestEmptyData(t *testing.T) {
Expand Down
15 changes: 9 additions & 6 deletions exporters/otlp/otlpmetric/otlpmetrichttp/mock_collector_test.go
Expand Up @@ -26,7 +26,6 @@ import (
"net/http"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -47,7 +46,7 @@ type mockCollector struct {

injectHTTPStatus []int
injectContentType string
injectDelay time.Duration
delay <-chan struct{}

clientTLSConfig *tls.Config
expectedHeaders map[string]string
Expand Down Expand Up @@ -76,8 +75,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) {
Expand Down Expand Up @@ -182,7 +185,7 @@ type mockCollectorConfig struct {
Port int
InjectHTTPStatus []int
InjectContentType string
InjectDelay time.Duration
Delay <-chan struct{}
WithTLS bool
ExpectedHeaders map[string]string
}
Expand All @@ -204,7 +207,7 @@ func runMockCollector(t *testing.T, cfg mockCollectorConfig) *mockCollector {
metricsStorage: otlpmetrictest.NewMetricsStorage(),
injectHTTPStatus: cfg.InjectHTTPStatus,
injectContentType: cfg.InjectContentType,
injectDelay: cfg.InjectDelay,
delay: cfg.Delay,
expectedHeaders: cfg.ExpectedHeaders,
}
mux := http.NewServeMux()
Expand Down
10 changes: 5 additions & 5 deletions exporters/otlp/otlptrace/otlptracehttp/client_test.go
Expand Up @@ -187,15 +187,15 @@ func TestExporterShutdown(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) }()
client := otlptracehttp.NewClient(
otlptracehttp.WithEndpoint(mc.Endpoint()),
otlptracehttp.WithInsecure(),
otlptracehttp.WithTimeout(50*time.Millisecond),
otlptracehttp.WithTimeout(time.Nanosecond),
)
ctx := context.Background()
exporter, err := otlptrace.New(ctx, client)
Expand All @@ -204,7 +204,7 @@ func TestTimeout(t *testing.T) {
assert.NoError(t, exporter.Shutdown(ctx))
}()
err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan())
assert.Equal(t, true, os.IsTimeout(err))
assert.Equalf(t, true, os.IsTimeout(err), "expected timeout error, got: %v", err)
}

func TestNoRetry(t *testing.T) {
Expand Down
15 changes: 9 additions & 6 deletions exporters/otlp/otlptrace/otlptracehttp/mock_collector_test.go
Expand Up @@ -26,7 +26,6 @@ import (
"net/http"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -48,7 +47,7 @@ type mockCollector struct {
injectHTTPStatus []int
injectResponseHeader []map[string]string
injectContentType string
injectDelay time.Duration
delay <-chan struct{}

clientTLSConfig *tls.Config
expectedHeaders map[string]string
Expand Down Expand Up @@ -83,8 +82,12 @@ func (c *mockCollector) ClientTLSConfig() *tls.Config {
}

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) {
Expand Down Expand Up @@ -205,7 +208,7 @@ type mockCollectorConfig struct {
InjectHTTPStatus []int
InjectContentType string
InjectResponseHeader []map[string]string
InjectDelay time.Duration
Delay <-chan struct{}
WithTLS bool
ExpectedHeaders map[string]string
}
Expand All @@ -228,7 +231,7 @@ func runMockCollector(t *testing.T, cfg mockCollectorConfig) *mockCollector {
injectHTTPStatus: cfg.InjectHTTPStatus,
injectResponseHeader: cfg.InjectResponseHeader,
injectContentType: cfg.InjectContentType,
injectDelay: cfg.InjectDelay,
delay: cfg.Delay,
expectedHeaders: cfg.ExpectedHeaders,
}
mux := http.NewServeMux()
Expand Down

0 comments on commit 11ce2dd

Please sign in to comment.