Skip to content

Commit 982a94e

Browse files
zirainjukie
andcommitted
fix test race (#8180)
* fix test race Signed-off-by: zirain <zirain2009@gmail.com> * use io.Discard Signed-off-by: zirain <zirain2009@gmail.com> * use sync.WaitGroup Signed-off-by: zirain <zirain2009@gmail.com> --------- Signed-off-by: zirain <zirain2009@gmail.com> Signed-off-by: Isaac Wilson <isaac.wilson514@gmail.com> Co-authored-by: Isaac Wilson <isaac.wilson514@gmail.com>
1 parent 4d13174 commit 982a94e

2 files changed

Lines changed: 24 additions & 18 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ require (
6464
go.uber.org/zap v1.27.1
6565
golang.org/x/exp v0.0.0-20250718183923-645b1fa84792
6666
golang.org/x/net v0.49.0
67+
golang.org/x/sync v0.19.0
6768
gomodules.xyz/jsonpatch/v2 v2.5.0
6869
gonum.org/v1/gonum v0.17.0
6970
google.golang.org/genproto/googleapis/api v0.0.0-20251202230838-ff82c1b0f217
@@ -293,7 +294,6 @@ require (
293294
golang.org/x/crypto/x509roots/fallback v0.0.0-20250406160420-959f8f3db0fb // indirect
294295
golang.org/x/mod v0.32.0 // indirect
295296
golang.org/x/oauth2 v0.34.0 // indirect
296-
golang.org/x/sync v0.19.0 // indirect
297297
golang.org/x/sys v0.40.0 // indirect
298298
golang.org/x/term v0.39.0 // indirect
299299
golang.org/x/text v0.33.0 // indirect

internal/cmd/server_test.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"testing"
1717

1818
"github.com/stretchr/testify/require"
19+
"golang.org/x/sync/errgroup"
1920

2021
"github.com/envoyproxy/gateway/internal/envoygateway/config"
2122
)
@@ -100,39 +101,44 @@ func testCustomProvider(t *testing.T, genCert bool) (string, string) {
100101

101102
func TestCustomProviderCancelWhenStarting(t *testing.T) {
102103
_, configPath := testCustomProvider(t, false)
103-
errCh := make(chan error)
104104
ctx, cancel := context.WithCancel(t.Context())
105-
go func() {
106-
errCh <- server(ctx, t.Output(), t.Output(), configPath, testHook, nil)
107-
}()
105+
var g errgroup.Group
106+
107+
// Use io.Discard to avoid data races (it's thread-safe unlike bytes.Buffer)
108+
g.Go(func() error {
109+
return server(ctx, io.Discard, io.Discard, configPath, testHook, nil)
110+
})
108111
go func() {
109112
cancel()
110113
}()
111114

112-
err := <-errCh
115+
err := g.Wait()
113116
require.NoError(t, err)
114117
}
115118

116119
func TestCustomProviderFailedToStart(t *testing.T) {
117120
_, configPath := testCustomProvider(t, false)
118121

119-
errCh := make(chan error)
120122
ctx, cancel := context.WithCancel(t.Context())
121-
go func() {
122-
errCh <- server(ctx, t.Output(), t.Output(), configPath, testHook, nil)
123-
}()
123+
defer cancel()
124+
var g errgroup.Group
125+
126+
// Use io.Discard to avoid data races (it's thread-safe unlike bytes.Buffer)
127+
g.Go(func() error {
128+
return server(ctx, io.Discard, io.Discard, configPath, testHook, nil)
129+
})
124130

125-
err := <-errCh
126-
cancel()
131+
err := g.Wait()
127132
require.Error(t, err, "failed to load TLS config")
128133
}
129134

130135
func TestCustomProviderCancelWhenConfigReload(t *testing.T) {
131136
configHome, configPath := testCustomProvider(t, true)
132137

133-
errCh := make(chan error)
134138
ctx, cancel := context.WithCancel(t.Context())
139+
defer cancel()
135140
count := atomic.Int32{}
141+
var g errgroup.Group
136142
hook := func(c context.Context, cfg *config.Server) error {
137143
if count.Add(1) >= 2 {
138144
t.Logf("Config reload triggered, cancelling context")
@@ -152,12 +158,12 @@ func TestCustomProviderCancelWhenConfigReload(t *testing.T) {
152158
}()
153159
}
154160

155-
go func() {
156-
errCh <- server(ctx, t.Output(), t.Output(), configPath, hook, startedCallback)
157-
}()
161+
// Use io.Discard to avoid data races (it's thread-safe unlike bytes.Buffer)
162+
g.Go(func() error {
163+
return server(ctx, io.Discard, io.Discard, configPath, hook, startedCallback)
164+
})
158165

159-
err := <-errCh
160-
cancel()
166+
err := g.Wait()
161167
require.NoError(t, err)
162168
}
163169

0 commit comments

Comments
 (0)