Skip to content

Commit

Permalink
[chore][service/internal/proctelemetry] Enable goleak check (#9227)
Browse files Browse the repository at this point in the history
**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
Add `goleak` to detect leaks in tests. Leaking goroutines were detected
that were caused by a dependency that we can ignore
(`go.opencensus.io/stats/view.(*worker).start`), some `Shutdown` calls
were also added that were missing.

**Link to tracking Issue:** <Issue number if applicable>
#9165 

**Testing:** <Describe what testing was performed and which tests were
added.>
Added check is passing as well as existing tests.
  • Loading branch information
crobert-1 committed Jan 5, 2024
1 parent c64ac69 commit df1ff47
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 15 additions & 2 deletions service/internal/proctelemetry/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,15 @@ func TestMetricReader(t *testing.T) {
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
_, _, err := InitMetricReader(context.Background(), tt.reader, make(chan error))
reader, server, err := InitMetricReader(context.Background(), tt.reader, make(chan error))
defer func() {
if reader != nil {
assert.NoError(t, reader.Shutdown(context.Background()))
}
if server != nil {
assert.NoError(t, server.Shutdown(context.Background()))
}
}()
assert.Equal(t, tt.err, err)
})
}
Expand Down Expand Up @@ -691,7 +699,12 @@ func TestSpanProcessor(t *testing.T) {
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
_, err := InitSpanProcessor(context.Background(), tt.processor)
processor, err := InitSpanProcessor(context.Background(), tt.processor)
defer func() {
if processor != nil {
assert.NoError(t, processor.Shutdown(context.Background()))
}
}()
assert.Equal(t, tt.err, err)
})
}
Expand Down
17 changes: 17 additions & 0 deletions service/internal/proctelemetry/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package proctelemetry

import (
"testing"

"go.uber.org/goleak"
)

// The IgnoreTopFunction call prevents catching the leak generated by opencensus
// defaultWorker.Start which at this time is part of the package's init call.
// See https://github.com/open-telemetry/opentelemetry-collector/issues/9165#issuecomment-1874836336 for more context.
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"))
}

0 comments on commit df1ff47

Please sign in to comment.