Skip to content

Commit

Permalink
test(spanner): fix race when accessing trace spans (googleapis#10186)
Browse files Browse the repository at this point in the history
* test(spanner): fix race when accessing trace spans

* fix go vet

* fix build

* return span data itself

* fix go vet

* fix vet

* revert changes

* revert other module changes, they will be done after cloud.google.com/go release

---------

Co-authored-by: rahul2393 <[email protected]>
Co-authored-by: rahul yadav <[email protected]>
  • Loading branch information
3 people authored May 23, 2024
1 parent b19dd34 commit 5fa60ba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions internal/testutil/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// TestExporter is a test utility exporter. It should be created with NewtestExporter.
type TestExporter struct {
mu sync.Mutex
Spans []*trace.SpanData
spans []*trace.SpanData

Stats chan *view.Data
Views []*view.View
Expand Down Expand Up @@ -56,7 +56,16 @@ func NewTestExporter(views ...*view.View) *TestExporter {
func (te *TestExporter) ExportSpan(s *trace.SpanData) {
te.mu.Lock()
defer te.mu.Unlock()
te.Spans = append(te.Spans, s)
te.spans = append(te.spans, s)
}

// Spans returns the exported spans.
func (te *TestExporter) Spans() []*trace.SpanData {
te.mu.Lock()
defer te.mu.Unlock()
spans := make([]*trace.SpanData, len(te.spans))
copy(spans, te.spans)
return spans
}

// ExportView exports a view.
Expand Down
2 changes: 1 addition & 1 deletion internal/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestStartSpan_OpenCensus(t *testing.T) {
if IsOpenTelemetryTracingEnabled() {
t.Errorf("got true, want false")
}
spans := te.Spans
spans := te.Spans()
if len(spans) != 1 {
t.Fatalf("got %d, want 1", len(spans))
}
Expand Down

0 comments on commit 5fa60ba

Please sign in to comment.