Skip to content

Commit

Permalink
use time.After for timing out the OTel tests, instead of a test-scope…
Browse files Browse the repository at this point in the history
…d context
  • Loading branch information
katiehockman committed Jan 18, 2024
1 parent 7f1897c commit 0a8fb1e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 47 deletions.
67 changes: 23 additions & 44 deletions ddtrace/opentelemetry/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,18 @@ func mockTracerProvider(t *testing.T, opts ...tracer.StartOption) (tp *TracerPro
}
}

func waitForPayload(ctx context.Context, payloads chan traces) (traces, error) {
func waitForPayload(payloads chan traces) (traces, error) {
select {
case <-ctx.Done():
return nil, fmt.Errorf("Timed out waiting for traces")
case p := <-payloads:
return p, nil
case <-time.After(10 * time.Second):
return nil, fmt.Errorf("Timed out waiting for traces")
}
}

func TestSpanResourceNameDefault(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ctx := context.Background()

_, payloads, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
Expand All @@ -102,7 +101,7 @@ func TestSpanResourceNameDefault(t *testing.T) {
sp.End()

tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -113,19 +112,19 @@ func TestSpanResourceNameDefault(t *testing.T) {

func TestSpanSetName(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, sp := tr.Start(ctx, "OldName")
sp.SetName("NewName")
sp.End()

tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -135,8 +134,6 @@ func TestSpanSetName(t *testing.T) {

func TestSpanEnd(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, payloads, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()
Expand Down Expand Up @@ -166,7 +163,7 @@ func TestSpanEnd(t *testing.T) {
}

tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand Down Expand Up @@ -220,14 +217,11 @@ func TestSpanSetStatus(t *testing.T) {

for _, test := range testData {
t.Run(fmt.Sprintf("Setting Code: %d", test.code), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

var sp oteltrace.Span
testStatus := func() {
sp.End()
tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand Down Expand Up @@ -258,8 +252,6 @@ func TestSpanSetStatus(t *testing.T) {

func TestSpanContextWithStartOptions(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, payloads, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()
Expand Down Expand Up @@ -291,7 +283,7 @@ func TestSpanContextWithStartOptions(t *testing.T) {
sp.End()

tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -312,8 +304,7 @@ func TestSpanContextWithStartOptions(t *testing.T) {

func TestSpanContextWithStartOptionsPriorityOrder(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()
Expand All @@ -330,7 +321,7 @@ func TestSpanContextWithStartOptionsPriorityOrder(t *testing.T) {
sp.End()

tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -344,8 +335,7 @@ func TestSpanContextWithStartOptionsPriorityOrder(t *testing.T) {

func TestSpanEndOptionsPriorityOrder(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()
Expand All @@ -370,7 +360,7 @@ func TestSpanEndOptionsPriorityOrder(t *testing.T) {
sp.End()

tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -380,8 +370,7 @@ func TestSpanEndOptionsPriorityOrder(t *testing.T) {

func TestSpanEndOptions(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
defer cleanup()
Expand All @@ -400,7 +389,7 @@ func TestSpanEndOptions(t *testing.T) {
tracer.WithError(errors.New("persisted_option")))
sp.End()
tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -417,8 +406,6 @@ func TestSpanEndOptions(t *testing.T) {

func TestSpanSetAttributes(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t)
tr := otel.Tracer("")
Expand Down Expand Up @@ -450,7 +437,7 @@ func TestSpanSetAttributes(t *testing.T) {

sp.End()
tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand Down Expand Up @@ -491,7 +478,7 @@ func TestSpanSetAttributesWithRemapping(t *testing.T) {
sp.End()

tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -501,8 +488,6 @@ func TestSpanSetAttributesWithRemapping(t *testing.T) {

func TestTracerStartOptions(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t, tracer.WithEnv("test_env"), tracer.WithService("test_serv"))
tr := otel.Tracer("")
Expand All @@ -511,7 +496,7 @@ func TestTracerStartOptions(t *testing.T) {
_, sp := tr.Start(context.Background(), "test")
sp.End()
tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -534,7 +519,7 @@ func TestOperationNameRemapping(t *testing.T) {
sp.End()

tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand Down Expand Up @@ -650,10 +635,6 @@ func TestRemapName(t *testing.T) {
out: "internal",
},
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t, tracer.WithEnv("test_env"), tracer.WithService("test_serv"))
tr := otel.Tracer("")
defer cleanup()
Expand All @@ -665,7 +646,7 @@ func TestRemapName(t *testing.T) {
sp.End()

tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -677,8 +658,6 @@ func TestRemapName(t *testing.T) {

func TestRemapWithMultipleSetAttributes(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, payloads, cleanup := mockTracerProvider(t, tracer.WithEnv("test_env"), tracer.WithService("test_serv"))
tr := otel.Tracer("")
Expand All @@ -696,7 +675,7 @@ func TestRemapWithMultipleSetAttributes(t *testing.T) {
sp.End()

tracer.Flush()
traces, err := waitForPayload(ctx, payloads)
traces, err := waitForPayload(payloads)
if err != nil {
t.Fatalf(err.Error())
}
Expand Down
4 changes: 1 addition & 3 deletions ddtrace/opentelemetry/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ func TestForceFlush(t *testing.T) {
}
for _, tc := range testData {
t.Run(fmt.Sprintf("Flush success: %t", tc.flushed), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
tp, payloads, cleanup := mockTracerProvider(t)
defer cleanup()

Expand All @@ -156,7 +154,7 @@ func TestForceFlush(t *testing.T) {
_, sp := tr.Start(context.Background(), "test_span")
sp.End()
tp.forceFlush(tc.timeOut, setFlushStatus, tc.flushFunc)
p, err := waitForPayload(ctx, payloads)
p, err := waitForPayload(payloads)
if tc.flushed {
assert.NoError(err)
assert.Equal("test_span", p[0][0]["resource"])
Expand Down

0 comments on commit 0a8fb1e

Please sign in to comment.