Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore][receiver/zookeeper] Enable goleak check #32179

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
Loading