test: fix possible goroutine leaks in unit tests (#4570)

This commit is contained in:
lzhfromustc 2021-07-21 13:40:04 -04:00 committed by GitHub
parent 0300770df1
commit 8332d5b997
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 5 deletions

View File

@ -48,6 +48,8 @@ func (s) TestSafeConfigSelector(t *testing.T) {
retChan1 := make(chan *RPCConfig)
retChan2 := make(chan *RPCConfig)
defer close(retChan1)
defer close(retChan2)
one := 1
two := 2
@ -55,8 +57,8 @@ func (s) TestSafeConfigSelector(t *testing.T) {
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) {

View File

@ -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 {
@ -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() {

View File

@ -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
}
@ -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")