Skip to content

Commit 101b8b1

Browse files
authored
[extension/jaegarremotesamplingextension/internal] add goleak tests for jaegerremotesampling extension (#31661)
Related to #30438 Resolves #31157 Signed-off-by: Israel Blancas <[email protected]>
1 parent ac6474c commit 101b8b1

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: jaegerremotesamplingextension
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Fix leaking goroutine on shutdown"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [31157]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

extension/jaegerremotesampling/internal/grpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (s *SamplingGRPCServer) Shutdown(ctx context.Context) error {
9090
return errGRPCServerNotRunning
9191
}
9292

93-
ch := make(chan struct{})
93+
ch := make(chan struct{}, 1)
9494
go func() {
9595
s.grpcServer.GracefulStop()
9696
ch <- struct{}{}

extension/jaegerremotesampling/internal/grpc_test.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestSamplingGRPCServer_Shutdown(t *testing.T) {
5454
{
5555
name: "graceful stop is successful with delay",
5656
grpcServer: &grpcServerMock{
57-
timeToGracefulStop: 5 * time.Second,
57+
timeToGracefulStop: time.Millisecond,
5858
},
5959
timeout: time.Minute,
6060
},
@@ -63,29 +63,45 @@ func TestSamplingGRPCServer_Shutdown(t *testing.T) {
6363
grpcServer: &grpcServerMock{
6464
timeToGracefulStop: time.Minute,
6565
},
66-
timeout: 5 * time.Second,
67-
},
68-
{
69-
name: "grpc server not started",
70-
timeout: time.Minute,
71-
expect: errGRPCServerNotRunning,
66+
timeout: time.Millisecond,
7267
},
7368
}
7469

7570
for _, tc := range tt {
7671
t.Run(tc.name, func(t *testing.T) {
7772
srv := &SamplingGRPCServer{grpcServer: tc.grpcServer}
7873
ctx, cancel := context.WithTimeout(context.Background(), tc.timeout)
74+
assert.NoError(t, tc.grpcServer.Serve(nil))
7975
defer cancel()
8076
assert.Equal(t, tc.expect, srv.Shutdown(ctx))
8177
})
8278
}
8379
}
8480

81+
func TestSamplingGRPCServerNotStarted_Shutdown(t *testing.T) {
82+
srv := &SamplingGRPCServer{}
83+
assert.Equal(t, errGRPCServerNotRunning, srv.Shutdown(context.Background()))
84+
}
85+
8586
type grpcServerMock struct {
8687
timeToGracefulStop time.Duration
88+
timer *time.Timer
89+
quit chan bool
8790
}
8891

89-
func (g *grpcServerMock) Serve(_ net.Listener) error { return nil }
90-
func (g *grpcServerMock) Stop() {}
91-
func (g *grpcServerMock) GracefulStop() { time.Sleep(g.timeToGracefulStop) }
92+
func (g *grpcServerMock) Serve(_ net.Listener) error {
93+
g.timer = time.NewTimer(g.timeToGracefulStop)
94+
g.quit = make(chan bool)
95+
return nil
96+
}
97+
func (g *grpcServerMock) Stop() {
98+
g.quit <- true
99+
}
100+
func (g *grpcServerMock) GracefulStop() {
101+
select {
102+
case <-g.quit:
103+
return
104+
case <-g.timer.C:
105+
return
106+
}
107+
}
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 internal
5+
6+
import (
7+
"testing"
8+
9+
"go.uber.org/goleak"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
goleak.VerifyTestMain(m)
14+
}

0 commit comments

Comments
 (0)