Skip to content

Commit 8332d5b

Browse files
authored
test: fix possible goroutine leaks in unit tests (#4570)
1 parent 0300770 commit 8332d5b

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

internal/resolver/config_selector_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@ func (s) TestSafeConfigSelector(t *testing.T) {
4848

4949
retChan1 := make(chan *RPCConfig)
5050
retChan2 := make(chan *RPCConfig)
51+
defer close(retChan1)
52+
defer close(retChan2)
5153

5254
one := 1
5355
two := 2
5456

5557
resp1 := &RPCConfig{MethodConfig: serviceconfig.MethodConfig{MaxReqSize: &one}}
5658
resp2 := &RPCConfig{MethodConfig: serviceconfig.MethodConfig{MaxReqSize: &two}}
5759

58-
cs1Called := make(chan struct{})
59-
cs2Called := make(chan struct{})
60+
cs1Called := make(chan struct{}, 1)
61+
cs2Called := make(chan struct{}, 1)
6062

6163
cs1 := &fakeConfigSelector{
6264
selectConfig: func(r RPCInfo) (*RPCConfig, error) {

xds/internal/httpfilter/fault/fault_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ func (s) TestFaultInjection_MaxActiveFaults(t *testing.T) {
608608
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
609609
defer cancel()
610610

611-
streams := make(chan testpb.TestService_FullDuplexCallClient)
611+
streams := make(chan testpb.TestService_FullDuplexCallClient, 5) // startStream() is called 5 times
612612
startStream := func() {
613613
str, err := client.FullDuplexCall(ctx)
614614
if err != nil {
@@ -620,7 +620,7 @@ func (s) TestFaultInjection_MaxActiveFaults(t *testing.T) {
620620
str := <-streams
621621
str.CloseSend()
622622
if _, err := str.Recv(); err != io.EOF {
623-
t.Fatal("stream error:", err)
623+
t.Error("stream error:", err)
624624
}
625625
}
626626
releaseStream := func() {

xds/internal/server/listener_wrapper_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ type fakeListener struct {
117117
}
118118

119119
func (fl *fakeListener) Accept() (net.Conn, error) {
120-
cne := <-fl.acceptCh
120+
cne, ok := <-fl.acceptCh
121+
if !ok {
122+
return nil, errors.New("a non-temporary error")
123+
}
121124
return cne.conn, cne.err
122125
}
123126

@@ -262,6 +265,7 @@ func (s) TestListenerWrapper_Accept(t *testing.T) {
262265
}}, nil)
263266
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
264267
defer cancel()
268+
defer close(lis.acceptCh)
265269
select {
266270
case <-ctx.Done():
267271
t.Fatalf("timeout waiting for the ready channel to be written to after receipt of a good Listener update")

0 commit comments

Comments
 (0)