Skip to content

Commit

Permalink
test: Fix possible goroutine leaks in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lzhfromustc committed Jun 29, 2021
1 parent 83f9def commit 9ddd10d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 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
2 changes: 1 addition & 1 deletion 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 Down
7 changes: 6 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,11 @@ type fakeListener struct {
}

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

Expand Down Expand Up @@ -262,6 +266,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 9ddd10d

Please sign in to comment.