@@ -21,6 +21,7 @@ package grpc
21
21
import (
22
22
"context"
23
23
"fmt"
24
+ "sync"
24
25
"sync/atomic"
25
26
"testing"
26
27
"time"
@@ -80,19 +81,24 @@ func (s) TestBlockingPick(t *testing.T) {
80
81
var finishedCount uint64
81
82
ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
82
83
defer cancel ()
84
+ wg := sync.WaitGroup {}
85
+ wg .Add (goroutineCount )
83
86
for i := goroutineCount ; i > 0 ; i -- {
84
87
go func () {
85
88
if tr , _ , err := bp .pick (ctx , true , balancer.PickInfo {}); err != nil || tr != testT {
86
89
t .Errorf ("bp.pick returned non-nil error: %v" , err )
87
90
}
88
91
atomic .AddUint64 (& finishedCount , 1 )
92
+ wg .Done ()
89
93
}()
90
94
}
91
95
time .Sleep (50 * time .Millisecond )
92
96
if c := atomic .LoadUint64 (& finishedCount ); c != 0 {
93
97
t .Errorf ("finished goroutines count: %v, want 0" , c )
94
98
}
95
99
bp .updatePicker (& testingPicker {sc : testSC , maxCalled : goroutineCount })
100
+ // Wait for all pickers to finish before the context is cancelled.
101
+ wg .Wait ()
96
102
}
97
103
98
104
func (s ) TestBlockingPickNoSubAvailable (t * testing.T ) {
@@ -102,19 +108,24 @@ func (s) TestBlockingPickNoSubAvailable(t *testing.T) {
102
108
ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
103
109
defer cancel ()
104
110
// All goroutines should block because picker returns no subConn available.
111
+ wg := sync.WaitGroup {}
112
+ wg .Add (goroutineCount )
105
113
for i := goroutineCount ; i > 0 ; i -- {
106
114
go func () {
107
115
if tr , _ , err := bp .pick (ctx , true , balancer.PickInfo {}); err != nil || tr != testT {
108
116
t .Errorf ("bp.pick returned non-nil error: %v" , err )
109
117
}
110
118
atomic .AddUint64 (& finishedCount , 1 )
119
+ wg .Done ()
111
120
}()
112
121
}
113
122
time .Sleep (50 * time .Millisecond )
114
123
if c := atomic .LoadUint64 (& finishedCount ); c != 0 {
115
124
t .Errorf ("finished goroutines count: %v, want 0" , c )
116
125
}
117
126
bp .updatePicker (& testingPicker {sc : testSC , maxCalled : goroutineCount })
127
+ // Wait for all pickers to finish before the context is cancelled.
128
+ wg .Wait ()
118
129
}
119
130
120
131
func (s ) TestBlockingPickTransientWaitforready (t * testing.T ) {
@@ -125,19 +136,24 @@ func (s) TestBlockingPickTransientWaitforready(t *testing.T) {
125
136
defer cancel ()
126
137
// All goroutines should block because picker returns transientFailure and
127
138
// picks are not failfast.
139
+ wg := sync.WaitGroup {}
140
+ wg .Add (goroutineCount )
128
141
for i := goroutineCount ; i > 0 ; i -- {
129
142
go func () {
130
143
if tr , _ , err := bp .pick (ctx , false , balancer.PickInfo {}); err != nil || tr != testT {
131
144
t .Errorf ("bp.pick returned non-nil error: %v" , err )
132
145
}
133
146
atomic .AddUint64 (& finishedCount , 1 )
147
+ wg .Done ()
134
148
}()
135
149
}
136
150
time .Sleep (time .Millisecond )
137
151
if c := atomic .LoadUint64 (& finishedCount ); c != 0 {
138
152
t .Errorf ("finished goroutines count: %v, want 0" , c )
139
153
}
140
154
bp .updatePicker (& testingPicker {sc : testSC , maxCalled : goroutineCount })
155
+ // Wait for all pickers to finish before the context is cancelled.
156
+ wg .Wait ()
141
157
}
142
158
143
159
func (s ) TestBlockingPickSCNotReady (t * testing.T ) {
@@ -147,17 +163,22 @@ func (s) TestBlockingPickSCNotReady(t *testing.T) {
147
163
ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
148
164
defer cancel ()
149
165
// All goroutines should block because subConn is not ready.
166
+ wg := sync.WaitGroup {}
167
+ wg .Add (goroutineCount )
150
168
for i := goroutineCount ; i > 0 ; i -- {
151
169
go func () {
152
170
if tr , _ , err := bp .pick (ctx , true , balancer.PickInfo {}); err != nil || tr != testT {
153
171
t .Errorf ("bp.pick returned non-nil error: %v" , err )
154
172
}
155
173
atomic .AddUint64 (& finishedCount , 1 )
174
+ wg .Done ()
156
175
}()
157
176
}
158
177
time .Sleep (time .Millisecond )
159
178
if c := atomic .LoadUint64 (& finishedCount ); c != 0 {
160
179
t .Errorf ("finished goroutines count: %v, want 0" , c )
161
180
}
162
181
bp .updatePicker (& testingPicker {sc : testSC , maxCalled : goroutineCount })
182
+ // Wait for all pickers to finish before the context is cancelled.
183
+ wg .Wait ()
163
184
}
0 commit comments