1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-10-31 00:07:40 +02:00

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>
This commit is contained in:
Sam Xie
2021-03-16 23:55:14 +08:00
committed by GitHub
parent 8b1be11a54
commit 28eaaa9a91
3 changed files with 52 additions and 3 deletions

View File

@@ -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))
}
}

View File

@@ -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"
)

View File

@@ -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)