diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go b/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go index e15d8b35d6d..5e614da2640 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go @@ -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) @@ -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) { diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/mock_collector_test.go b/exporters/otlp/otlpmetric/otlpmetrichttp/mock_collector_test.go index 042866c2eef..876f8608c3d 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/mock_collector_test.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/mock_collector_test.go @@ -26,7 +26,6 @@ import ( "net/http" "sync" "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -47,7 +46,7 @@ type mockCollector struct { injectHTTPStatus []int injectContentType string - injectDelay time.Duration + delay <-chan struct{} clientTLSConfig *tls.Config expectedHeaders map[string]string @@ -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) { @@ -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 } @@ -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() diff --git a/exporters/otlp/otlptrace/otlptracehttp/client_test.go b/exporters/otlp/otlptrace/otlptracehttp/client_test.go index 445717e08d5..d14a6ce806f 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/client_test.go +++ b/exporters/otlp/otlptrace/otlptracehttp/client_test.go @@ -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) @@ -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) { diff --git a/exporters/otlp/otlptrace/otlptracehttp/mock_collector_test.go b/exporters/otlp/otlptrace/otlptracehttp/mock_collector_test.go index 01d79daf8c4..468506466c4 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/mock_collector_test.go +++ b/exporters/otlp/otlptrace/otlptracehttp/mock_collector_test.go @@ -26,7 +26,6 @@ import ( "net/http" "sync" "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -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 @@ -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) { @@ -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 } @@ -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()