Skip to content

Commit 28eaaa9

Browse files
XSAMMrAliasAneurysm9
authored
Add a test to prove the Tracer is safe for concurrent calls (#1665)
* Add test to prove the Tracer is safe for concurrent calls * Fix NotToPanic of harness never call the input func * Fix tests Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
1 parent 8b1be11 commit 28eaaa9

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

internal/matchers/expectation.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,19 @@ func (e *Expectation) ToBeFalse() {
8484
}
8585

8686
func (e *Expectation) NotToPanic() {
87-
if actual := recover(); actual != nil {
88-
e.fail(fmt.Sprintf("Expected panic\n\t%v\nto have not been raised", actual))
87+
switch a := e.actual.(type) {
88+
case func():
89+
func() {
90+
defer func() {
91+
if recovered := recover(); recovered != nil {
92+
e.fail(fmt.Sprintf("Expected panic\n\t%v\nto have not been raised", recovered))
93+
}
94+
}()
95+
96+
a()
97+
}()
98+
default:
99+
e.fail(fmt.Sprintf("Cannot check if non-func value\n\t%v\nis truthy", a))
89100
}
90101
}
91102

internal/tools/tools.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package tools
1818

1919
import (
2020
_ "github.com/client9/misspell/cmd/misspell"
21+
_ "github.com/gogo/protobuf/protoc-gen-gogofast"
2122
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
2223
_ "github.com/itchyny/gojq"
2324
_ "golang.org/x/tools/cmd/stringer"
24-
_ "github.com/gogo/protobuf/protoc-gen-gogofast"
2525
)

oteltest/harness.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,44 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
217217
e.Expect(csc.TraceID()).NotToEqual(psc.TraceID())
218218
e.Expect(csc.SpanID()).NotToEqual(psc.SpanID())
219219
})
220+
221+
t.Run("all methods are safe to be called concurrently", func(t *testing.T) {
222+
t.Parallel()
223+
224+
e := matchers.NewExpecter(t)
225+
tracer := subjectFactory()
226+
227+
ctx, parent := tracer.Start(context.Background(), "span")
228+
229+
runner := func(tp trace.Tracer) <-chan struct{} {
230+
done := make(chan struct{})
231+
go func(tp trace.Tracer) {
232+
var wg sync.WaitGroup
233+
for i := 0; i < 20; i++ {
234+
wg.Add(1)
235+
go func(name string) {
236+
defer wg.Done()
237+
_, child := tp.Start(ctx, name)
238+
239+
psc := parent.SpanContext()
240+
csc := child.SpanContext()
241+
242+
e.Expect(csc.TraceID()).ToEqual(psc.TraceID())
243+
e.Expect(csc.SpanID()).NotToEqual(psc.SpanID())
244+
}(fmt.Sprintf("span %d", i))
245+
}
246+
wg.Wait()
247+
done <- struct{}{}
248+
}(tp)
249+
return done
250+
}
251+
252+
e.Expect(func() {
253+
done := runner(tracer)
254+
255+
<-done
256+
}).NotToPanic()
257+
})
220258
})
221259

222260
h.testSpan(subjectFactory)

0 commit comments

Comments
 (0)