Skip to content

Commit

Permalink
test: fix possible goroutine leaks in unit tests (#4570)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzhfromustc authored Jul 21, 2021
1 parent 0300770 commit 8332d5b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions internal/resolver/config_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ func (s) TestSafeConfigSelector(t *testing.T) {

retChan1 := make(chan *RPCConfig)
retChan2 := make(chan *RPCConfig)
defer close(retChan1)
defer close(retChan2)

one := 1
two := 2

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

cs1Called := make(chan struct{})
cs2Called := make(chan struct{})
cs1Called := make(chan struct{}, 1)
cs2Called := make(chan struct{}, 1)

cs1 := &fakeConfigSelector{
selectConfig: func(r RPCInfo) (*RPCConfig, error) {
Expand Down
4 changes: 2 additions & 2 deletions xds/internal/httpfilter/fault/fault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func (s) TestFaultInjection_MaxActiveFaults(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

streams := make(chan testpb.TestService_FullDuplexCallClient)
streams := make(chan testpb.TestService_FullDuplexCallClient, 5) // startStream() is called 5 times
startStream := func() {
str, err := client.FullDuplexCall(ctx)
if err != nil {
Expand All @@ -620,7 +620,7 @@ func (s) TestFaultInjection_MaxActiveFaults(t *testing.T) {
str := <-streams
str.CloseSend()
if _, err := str.Recv(); err != io.EOF {
t.Fatal("stream error:", err)
t.Error("stream error:", err)
}
}
releaseStream := func() {
Expand Down
6 changes: 5 additions & 1 deletion xds/internal/server/listener_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ type fakeListener struct {
}

func (fl *fakeListener) Accept() (net.Conn, error) {
cne := <-fl.acceptCh
cne, ok := <-fl.acceptCh
if !ok {
return nil, errors.New("a non-temporary error")
}
return cne.conn, cne.err
}

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

0 comments on commit 8332d5b

Please sign in to comment.