Skip to content

Commit ccb8b49

Browse files
authored
testing(bigquery/storage/managedwriter): fix data race in router test (#7664)
Fixes: https://togithub.com/googleapis/google-cloud-go/issues/7662
1 parent ef61e47 commit ccb8b49

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

bigquery/storage/managedwriter/routers_test.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -193,37 +193,31 @@ func TestSharedRouter_Multiplex(t *testing.T) {
193193
t.Errorf("expected conn to be loaded, was not")
194194
}
195195
// wait for a watchdog interval
196-
time.Sleep(2100 * time.Millisecond)
196+
time.Sleep(watchDogInterval * 2)
197197

198198
wantConnCount = 2
199+
// grab read lock so we can assert internal state of the router
200+
router.multiMu.RLock()
201+
defer router.multiMu.RUnlock()
199202
if got := len(router.multiConns); wantConnCount != got {
200203
t.Fatalf("wanted %d conns, got %d", wantConnCount, got)
201204
}
202-
203205
gotLoad0 := router.multiConns[0].curLoad()
204206
gotLoad1 := router.multiConns[1].curLoad()
205207
if gotLoad0 > gotLoad1 {
206208
t.Errorf("expected connections to be ordered by load, got %f, %f", gotLoad0, gotLoad1)
207209
}
208-
209-
// grab the router multiplex mutex so we can assert rebalancing happened.
210-
router.mu.RLock()
211-
freqs := make(map[string]int)
212-
for _, conn := range router.multiMap {
213-
id := conn.id
214-
count, ok := freqs[id]
215-
if ok {
216-
freqs[id] = count + 1
217-
} else {
218-
freqs[id] = 1
210+
// verify that rebalance occurred
211+
connsWithWriters := 0
212+
for _, v := range router.invertedMultiMap {
213+
if len(v) > 0 {
214+
connsWithWriters++
219215
}
220216
}
221-
router.mu.RUnlock()
222-
for connID, ct := range freqs {
223-
if ct == 0 {
224-
t.Errorf("conn %q had zero writers", connID)
225-
}
217+
if connsWithWriters < wantConnCount {
218+
t.Errorf("wanted at least %d connections to have writers attached, got %d", wantConnCount, connsWithWriters)
226219
}
220+
227221
}
228222

229223
func BenchmarkRoutingParallel(b *testing.B) {

0 commit comments

Comments
 (0)