Debian Codegraph @

sdk/trace/tracetest/recorder_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package tracetest
5
6 import (
7 "context"
8 "sync"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12
13 sdktrace "go.opentelemetry.io/otel/sdk/trace"
14 )
15
16 type rwSpan struct {
17 sdktrace.ReadWriteSpan
18 }
19
20 func TestSpanRecorderOnStartAppends(t *testing.T) {
21 s0, s1 := new(rwSpan), new(rwSpan)
22 ctx := t.Context()
23 sr := new(SpanRecorder)
24
25 assert.Empty(t, sr.started)
26 sr.OnStart(ctx, s0)
27 assert.Len(t, sr.started, 1)
28 sr.OnStart(ctx, s1)
29 assert.Len(t, sr.started, 2)
30
31 // Ensure order correct.
32 started := sr.Started()
33 assert.Same(t, s0, started[0])
34 assert.Same(t, s1, started[1])
35 }
36
37 type roSpan struct {
38 sdktrace.ReadOnlySpan
39 }
40
41 func TestSpanRecorderOnEndAppends(t *testing.T) {
42 s0, s1 := new(roSpan), new(roSpan)
43 sr := new(SpanRecorder)
44
45 assert.Empty(t, sr.ended)
46 sr.OnEnd(s0)
47 assert.Len(t, sr.ended, 1)
48 sr.OnEnd(s1)
49 assert.Len(t, sr.ended, 2)
50
51 // Ensure order correct.
52 ended := sr.Ended()
53 assert.Same(t, s0, ended[0])
54 assert.Same(t, s1, ended[1])
55 }
56
57 func TestSpanRecorderShutdownNoError(t *testing.T) {
58 ctx := t.Context()
59 assert.NoError(t, new(SpanRecorder).Shutdown(ctx))
60
61 var c context.CancelFunc
62 ctx, c = context.WithCancel(ctx)
63 c()
64 assert.NoError(t, new(SpanRecorder).Shutdown(ctx))
65 }
66
67 func TestSpanRecorderForceFlushNoError(t *testing.T) {
68 ctx := t.Context()
69 assert.NoError(t, new(SpanRecorder).ForceFlush(ctx))
70
71 var c context.CancelFunc
72 ctx, c = context.WithCancel(ctx)
73 c()
74 assert.NoError(t, new(SpanRecorder).ForceFlush(ctx))
75 }
76
77 func runConcurrently(funcs ...func()) {
78 var wg sync.WaitGroup
79
80 for _, f := range funcs {
81 wg.Add(1)
82 go func(f func()) {
83 f()
84 wg.Done()
85 }(f)
86 }
87
88 wg.Wait()
89 }
90
91 func TestEndingConcurrentSafe(t *testing.T) {
92 sr := NewSpanRecorder()
93
94 runConcurrently(
95 func() { sr.OnEnd(new(roSpan)) },
96 func() { sr.OnEnd(new(roSpan)) },
97 func() { sr.Ended() },
98 )
99
100 assert.Len(t, sr.Ended(), 2)
101 }
102
103 func TestStartingConcurrentSafe(t *testing.T) {
104 sr := NewSpanRecorder()
105
106 ctx := t.Context()
107 runConcurrently(
108 func() { sr.OnStart(ctx, new(rwSpan)) },
109 func() { sr.OnStart(ctx, new(rwSpan)) },
110 func() { sr.Started() },
111 )
112
113 assert.Len(t, sr.Started(), 2)
114 }
115
116 func TestResetConcurrentSafe(t *testing.T) {
117 sr := NewSpanRecorder()
118 ctx := t.Context()
119
120 runConcurrently(
121 func() { sr.OnStart(ctx, new(rwSpan)) },
122 func() { sr.OnStart(ctx, new(rwSpan)) },
123 func() { sr.OnEnd(new(roSpan)) },
124 func() { sr.OnEnd(new(roSpan)) },
125 )
126
127 assert.Len(t, sr.Started(), 2)
128 assert.Len(t, sr.Ended(), 2)
129
130 runConcurrently(
131 func() { sr.Reset() },
132 func() { sr.Reset() },
133 func() { sr.Reset() },
134 )
135
136 assert.Empty(t, sr.Started())
137 assert.Empty(t, sr.Ended())
138 }