Skip to content

Commit

Permalink
[chore][receiver/zookeeper] Enable goleak check (#32179)
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.-->
This change enables `goleak` to help ensure the ZooKeeper receiver does
not leak any goroutines. This is a test only change, the existing test
implementation needed a bit of modification to properly close the
network listener.

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

**Testing:** <Describe what testing was performed and which tests were
added.>
Existing tests and new `goleak` check are all passing.
  • Loading branch information
crobert-1 authored Apr 5, 2024
1 parent ef4402b commit c3e7965
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
14 changes: 14 additions & 0 deletions receiver/zookeeperreceiver/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package zookeeperreceiver

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
37 changes: 28 additions & 9 deletions receiver/zookeeperreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,16 @@ func TestZookeeperMetricsScraperScrape(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
localAddr := testutil.GetAvailableLocalAddress(t)
if !tt.mockZKConnectionErr {
ms := mockedServer{ready: make(chan bool, 1)}
go ms.mockZKServer(t, localAddr, tt.mockedZKCmdToOutputFilename)
listener, err := net.Listen("tcp", localAddr)
require.NoError(t, err)
ms := mockedServer{
listener: listener,
ready: make(chan bool, 1),
quit: make(chan struct{}),
}

defer ms.shutdown()
go ms.mockZKServer(t, tt.mockedZKCmdToOutputFilename)
<-ms.ready
}

Expand Down Expand Up @@ -337,19 +345,26 @@ func TestZookeeperShutdownBeforeScrape(t *testing.T) {
}

type mockedServer struct {
listener net.Listener

ready chan bool
quit chan struct{}
}

func (ms *mockedServer) mockZKServer(t *testing.T, endpoint string, cmdToFileMap map[string]string) {
func (ms *mockedServer) mockZKServer(t *testing.T, cmdToFileMap map[string]string) {
var cmd string
listener, err := net.Listen("tcp", endpoint)
require.NoError(t, err)
defer listener.Close()
ms.ready <- true

for {
conn, err := listener.Accept()
require.NoError(t, err)
conn, err := ms.listener.Accept()
if err != nil {
select {
case <-ms.quit:
return
default:
require.NoError(t, err)
}
}
reader := bufio.NewReader(conn)
scanner := bufio.NewScanner(reader)
scanner.Scan()
Expand All @@ -366,6 +381,10 @@ func (ms *mockedServer) mockZKServer(t *testing.T, endpoint string, cmdToFileMap
require.NoError(t, err)

conn.Close()

}
}

func (ms *mockedServer) shutdown() {
close(ms.quit)
ms.listener.Close()
}

0 comments on commit c3e7965

Please sign in to comment.