Skip to content

Commit 8960925

Browse files
committed
time: deflake TestChan/asynctimerchan=1 tests
The overall time package tests increase from 3.85s to 4.85s on my laptop. But they should be less flaky, and the time is spent sleeping, so it won't slow down the overall machine running multiple package tests in parallel. For #66322. Change-Id: I66d6647c389c943b53045e8836ede4ba3d4670c7 Reviewed-on: https://go-review.googlesource.com/c/go/+/581315 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent db5f2b4 commit 8960925

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/time/tick_test.go

+19-14
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ func TestTicker(t *testing.T) {
5555
count, delta := test.count, test.delta
5656
ticker := NewTicker(delta)
5757
t0 := Now()
58-
for i := 0; i < count/2; i++ {
58+
for range count / 2 {
5959
<-ticker.C
6060
}
6161
ticker.Reset(delta * 2)
62-
for i := count / 2; i < count; i++ {
62+
for range count - count/2 {
6363
<-ticker.C
6464
}
6565
ticker.Stop()
@@ -114,7 +114,7 @@ func TestTeardown(t *testing.T) {
114114
if testing.Short() {
115115
Delta = 20 * Millisecond
116116
}
117-
for i := 0; i < 3; i++ {
117+
for range 3 {
118118
ticker := NewTicker(Delta)
119119
<-ticker.C
120120
ticker.Stop()
@@ -356,14 +356,19 @@ func testTimerChan(t *testing.T, tim timer, C <-chan Time, synctimerchan bool) {
356356
// Windows in particular has very coarse timers so we have to
357357
// wait 10ms just to make a timer go off.
358358
const (
359-
sched = 10 * Millisecond
360-
tries = 100
359+
sched = 10 * Millisecond
360+
tries = 100
361+
drainTries = 5
361362
)
362363

363364
drain := func() {
364-
select {
365-
case <-C:
366-
default:
365+
for range drainTries {
366+
select {
367+
case <-C:
368+
return
369+
default:
370+
}
371+
Sleep(sched)
367372
}
368373
}
369374
noTick := func() {
@@ -381,7 +386,7 @@ func testTimerChan(t *testing.T, tim timer, C <-chan Time, synctimerchan bool) {
381386
case <-C:
382387
return
383388
}
384-
for i := 0; i < tries; i++ {
389+
for range tries {
385390
Sleep(sched)
386391
select {
387392
default:
@@ -403,7 +408,7 @@ func testTimerChan(t *testing.T, tim timer, C <-chan Time, synctimerchan bool) {
403408
if n = len(C); n == 1 {
404409
return
405410
}
406-
for i := 0; i < tries; i++ {
411+
for range tries {
407412
Sleep(sched)
408413
if n = len(C); n == 1 {
409414
return
@@ -477,7 +482,7 @@ func testTimerChan(t *testing.T, tim timer, C <-chan Time, synctimerchan bool) {
477482

478483
waitDone := func(done chan bool) {
479484
t.Helper()
480-
for i := 0; i < tries; i++ {
485+
for range tries {
481486
Sleep(sched)
482487
select {
483488
case <-done:
@@ -580,7 +585,7 @@ func testTimerChan(t *testing.T, tim timer, C <-chan Time, synctimerchan bool) {
580585
// Test enqueueTimerChan when timer is stopped.
581586
stop = make(chan bool)
582587
done = make(chan bool, 2)
583-
for i := 0; i < 2; i++ {
588+
for range 2 {
584589
go func() {
585590
select {
586591
case <-C:
@@ -641,7 +646,7 @@ func TestAfterTimes(t *testing.T) {
641646
// Make sure it does.
642647
// To avoid flakes due to very long scheduling delays,
643648
// require 10 failures in a row before deciding something is wrong.
644-
for i := 0; i < 10; i++ {
649+
for range 10 {
645650
start := Now()
646651
c := After(10 * Millisecond)
647652
Sleep(500 * Millisecond)
@@ -657,7 +662,7 @@ func TestAfterTimes(t *testing.T) {
657662
func TestTickTimes(t *testing.T) {
658663
t.Parallel()
659664
// See comment in TestAfterTimes
660-
for i := 0; i < 10; i++ {
665+
for range 10 {
661666
start := Now()
662667
c := Tick(10 * Millisecond)
663668
Sleep(500 * Millisecond)

0 commit comments

Comments
 (0)