Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions exporters/otlp/otlphttp/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
23 changes: 15 additions & 8 deletions exporters/otlp/otlphttp/mock_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"net/http"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -53,7 +52,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 @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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()
Expand Down