Skip to content

Commit 71c3e01

Browse files
committed
fix data race in test
1 parent 5b7c6d9 commit 71c3e01

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

Diff for: clientconn_test.go

+29-9
Original file line numberDiff line numberDiff line change
@@ -359,24 +359,43 @@ func TestPreventReaddAddrConnAfterDeletingByBalancer(t *testing.T) {
359359
time.Sleep(10 * time.Millisecond)
360360
}
361361
}
362-
if len(cc.conns) != 2 {
363-
t.Fatalf("Number of addConns, want %d, got %d", 2, len(cc.conns))
362+
363+
getConnCount := func() int {
364+
cc.mu.RLock()
365+
defer cc.mu.RUnlock()
366+
return len(cc.conns)
367+
}
368+
369+
getAddrCount := func() int {
370+
cc.mu.RLock()
371+
defer cc.mu.RUnlock()
372+
return len(cc.addrs)
373+
}
374+
375+
connCount := getConnCount()
376+
if connCount != 2 {
377+
t.Fatalf("Number of addConns, want %d, got %d", 2, connCount)
364378
}
365-
if len(cc.addrs) != 2 {
366-
t.Fatalf("Number of addrs, want %d, got %d", 2, len(cc.addrs))
379+
380+
addrCount := getAddrCount()
381+
if addrCount != 2 {
382+
t.Fatalf("Number of addrs, want %d, got %d", 2, addrCount)
367383
}
384+
368385
// Delete servers[1] by balancer
369386
u := &naming.Update{
370387
Op: naming.Delete,
371388
Addr: "localhost:" + servers[1].port,
372389
}
373390
r.w.inject([]*naming.Update{u})
374391
// Wait until the addrConn of services[1] is deleted
375-
for len(cc.conns) != 1 {
392+
for getConnCount() != 1 {
376393
time.Sleep(10 * time.Millisecond)
377394
}
378-
if len(cc.addrs) != 1 {
379-
t.Fatalf("Number of addrs, want %d, got %d", 1, len(cc.addrs))
395+
396+
addrCount = getAddrCount()
397+
if addrCount != 1 {
398+
t.Fatalf("Number of addrs, want %d, got %d", 1, addrCount)
380399
}
381400

382401
// Call resetAddrConn (ex: goaway)
@@ -385,7 +404,8 @@ func TestPreventReaddAddrConnAfterDeletingByBalancer(t *testing.T) {
385404
t.Fatalf("Failed to reset addrConn: %v", err)
386405
}
387406

388-
if len(cc.conns) != 1 {
389-
t.Fatalf("Number of addConns, want %d, got %d", 1, len(cc.conns))
407+
connCount = getConnCount()
408+
if connCount != 1 {
409+
t.Fatalf("Number of addConns, want %d, got %d", 1, connCount)
390410
}
391411
}

0 commit comments

Comments
 (0)