Skip to content

Commit

Permalink
fix: improving test logic for TestStreamSendsErrorCode to avoid flaki…
Browse files Browse the repository at this point in the history
…ness
  • Loading branch information
darccio committed Dec 18, 2024
1 parent d15e61a commit 7a2db86
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions contrib/google.golang.org/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,10 @@ func TestPreservesMetadata(t *testing.T) {
func TestStreamSendsErrorCode(t *testing.T) {
wantCode := codes.InvalidArgument.String()

// This mutes the logger output regarding service name validation in
// gRPC instrumentation.
globalconfig.SetServiceName("grpc")

mt := mocktracer.Start()
defer mt.Stop()

Expand All @@ -520,20 +524,20 @@ func TestStreamSendsErrorCode(t *testing.T) {
// to flush the spans
_, _ = stream.Recv()

containsErrorCode := false
spans := mt.FinishedSpans()

// check if at least one span has error code
// check if at least one span with spank.kind=server has error code
var span mocktracer.Span
for _, s := range spans {
if s.Tag(tagCode) == wantCode {
containsErrorCode = true
if s.Tag(tagCode) != wantCode {
continue
}
if s.Tag(ext.SpanKind) != ext.SpanKindServer {
continue
}
span = s
}
assert.True(t, containsErrorCode, "at least one span should contain error code, the spans were:\n%v", spans)

// ensure that last span contains error code also
gotLastSpanCode := spans[len(spans)-1].Tag(tagCode)
assert.Equal(t, wantCode, gotLastSpanCode, "last span should contain error code")
assert.NotNilf(t, span, "at least one span should contain error code, the spans were:\n%v", spans)
}

// fixtureServer a dummy implementation of our grpc fixtureServer.
Expand Down

0 comments on commit 7a2db86

Please sign in to comment.