Skip to content

Commit c3e7965

Browse files
authored
[chore][receiver/zookeeper] Enable goleak check (#32179)
**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.
1 parent ef4402b commit c3e7965

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package zookeeperreceiver
5+
6+
import (
7+
"testing"
8+
9+
"go.uber.org/goleak"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
goleak.VerifyTestMain(m)
14+
}

receiver/zookeeperreceiver/scraper_test.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,16 @@ func TestZookeeperMetricsScraperScrape(t *testing.T) {
270270
t.Run(tt.name, func(t *testing.T) {
271271
localAddr := testutil.GetAvailableLocalAddress(t)
272272
if !tt.mockZKConnectionErr {
273-
ms := mockedServer{ready: make(chan bool, 1)}
274-
go ms.mockZKServer(t, localAddr, tt.mockedZKCmdToOutputFilename)
273+
listener, err := net.Listen("tcp", localAddr)
274+
require.NoError(t, err)
275+
ms := mockedServer{
276+
listener: listener,
277+
ready: make(chan bool, 1),
278+
quit: make(chan struct{}),
279+
}
280+
281+
defer ms.shutdown()
282+
go ms.mockZKServer(t, tt.mockedZKCmdToOutputFilename)
275283
<-ms.ready
276284
}
277285

@@ -337,19 +345,26 @@ func TestZookeeperShutdownBeforeScrape(t *testing.T) {
337345
}
338346

339347
type mockedServer struct {
348+
listener net.Listener
349+
340350
ready chan bool
351+
quit chan struct{}
341352
}
342353

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

350358
for {
351-
conn, err := listener.Accept()
352-
require.NoError(t, err)
359+
conn, err := ms.listener.Accept()
360+
if err != nil {
361+
select {
362+
case <-ms.quit:
363+
return
364+
default:
365+
require.NoError(t, err)
366+
}
367+
}
353368
reader := bufio.NewReader(conn)
354369
scanner := bufio.NewScanner(reader)
355370
scanner.Scan()
@@ -366,6 +381,10 @@ func (ms *mockedServer) mockZKServer(t *testing.T, endpoint string, cmdToFileMap
366381
require.NoError(t, err)
367382

368383
conn.Close()
369-
370384
}
371385
}
386+
387+
func (ms *mockedServer) shutdown() {
388+
close(ms.quit)
389+
ms.listener.Close()
390+
}

0 commit comments

Comments
 (0)