From 5fa60baf5636414127453b18f1a914811e24ef26 Mon Sep 17 00:00:00 2001 From: Diego Becciolini Date: Thu, 23 May 2024 05:39:52 +0100 Subject: [PATCH] test(spanner): fix race when accessing trace spans (#10186) * 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 Co-authored-by: rahul yadav --- internal/testutil/trace.go | 13 +++++++++++-- internal/trace/trace_test.go | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/testutil/trace.go b/internal/testutil/trace.go index 5cf28f894146..62df6fd6d01b 100644 --- a/internal/testutil/trace.go +++ b/internal/testutil/trace.go @@ -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 @@ -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. diff --git a/internal/trace/trace_test.go b/internal/trace/trace_test.go index 2e03d2a89df0..c5ba3a88fd35 100644 --- a/internal/trace/trace_test.go +++ b/internal/trace/trace_test.go @@ -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)) }