Skip to content

Commit

Permalink
Add a test to prove the Tracer is safe for concurrent calls (#1665)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Anthony Mirabella <[email protected]>
  • Loading branch information
3 people committed Mar 16, 2021
1 parent 8b1be11 commit 28eaaa9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
15 changes: 13 additions & 2 deletions internal/matchers/expectation.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,19 @@ func (e *Expectation) ToBeFalse() {
}

func (e *Expectation) NotToPanic() {
if actual := recover(); actual != nil {
e.fail(fmt.Sprintf("Expected panic\n\t%v\nto have not been raised", actual))
switch a := e.actual.(type) {
case func():
func() {
defer func() {
if recovered := recover(); recovered != nil {
e.fail(fmt.Sprintf("Expected panic\n\t%v\nto have not been raised", recovered))
}
}()

a()
}()
default:
e.fail(fmt.Sprintf("Cannot check if non-func value\n\t%v\nis truthy", a))
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package tools

import (
_ "github.com/client9/misspell/cmd/misspell"
_ "github.com/gogo/protobuf/protoc-gen-gogofast"
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
_ "github.com/itchyny/gojq"
_ "golang.org/x/tools/cmd/stringer"
_ "github.com/gogo/protobuf/protoc-gen-gogofast"
)
38 changes: 38 additions & 0 deletions oteltest/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,44 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
e.Expect(csc.TraceID()).NotToEqual(psc.TraceID())
e.Expect(csc.SpanID()).NotToEqual(psc.SpanID())
})

t.Run("all methods are safe to be called concurrently", func(t *testing.T) {
t.Parallel()

e := matchers.NewExpecter(t)
tracer := subjectFactory()

ctx, parent := tracer.Start(context.Background(), "span")

runner := func(tp trace.Tracer) <-chan struct{} {
done := make(chan struct{})
go func(tp trace.Tracer) {
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(name string) {
defer wg.Done()
_, child := tp.Start(ctx, name)

psc := parent.SpanContext()
csc := child.SpanContext()

e.Expect(csc.TraceID()).ToEqual(psc.TraceID())
e.Expect(csc.SpanID()).NotToEqual(psc.SpanID())
}(fmt.Sprintf("span %d", i))
}
wg.Wait()
done <- struct{}{}
}(tp)
return done
}

e.Expect(func() {
done := runner(tracer)

<-done
}).NotToPanic()
})
})

h.testSpan(subjectFactory)
Expand Down

0 comments on commit 28eaaa9

Please sign in to comment.