@@ -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
101102func 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
116119func 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
130135func 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