aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2024-04-23 17:34:38 -0400
committerRuss Cox <rsc@golang.org>2024-04-25 11:28:32 +0000
commit8960925ad8dd1ef234731d94ebbea263e35a3e42 (patch)
tree11051b8fcbcc2d97e3cd4689a72f86e62a2847a1
parentdb5f2b415399da9b653e68aa03f23ce661cc5339 (diff)
downloadgo-8960925ad8dd1ef234731d94ebbea263e35a3e42.tar.gz
go-8960925ad8dd1ef234731d94ebbea263e35a3e42.zip
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
-rw-r--r--src/time/tick_test.go33
1 files changed, 19 insertions, 14 deletions
diff --git a/src/time/tick_test.go b/src/time/tick_test.go
index 6cb9c0ae14..42ef6d3217 100644
--- a/src/time/tick_test.go
+++ b/src/time/tick_test.go
@@ -55,11 +55,11 @@ func TestTicker(t *testing.T) {
count, delta := test.count, test.delta
ticker := NewTicker(delta)
t0 := Now()
- for i := 0; i < count/2; i++ {
+ for range count / 2 {
<-ticker.C
}
ticker.Reset(delta * 2)
- for i := count / 2; i < count; i++ {
+ for range count - count/2 {
<-ticker.C
}
ticker.Stop()
@@ -114,7 +114,7 @@ func TestTeardown(t *testing.T) {
if testing.Short() {
Delta = 20 * Millisecond
}
- for i := 0; i < 3; i++ {
+ for range 3 {
ticker := NewTicker(Delta)
<-ticker.C
ticker.Stop()
@@ -356,14 +356,19 @@ func testTimerChan(t *testing.T, tim timer, C <-chan Time, synctimerchan bool) {
// Windows in particular has very coarse timers so we have to
// wait 10ms just to make a timer go off.
const (
- sched = 10 * Millisecond
- tries = 100
+ sched = 10 * Millisecond
+ tries = 100
+ drainTries = 5
)
drain := func() {
- select {
- case <-C:
- default:
+ for range drainTries {
+ select {
+ case <-C:
+ return
+ default:
+ }
+ Sleep(sched)
}
}
noTick := func() {
@@ -381,7 +386,7 @@ func testTimerChan(t *testing.T, tim timer, C <-chan Time, synctimerchan bool) {
case <-C:
return
}
- for i := 0; i < tries; i++ {
+ for range tries {
Sleep(sched)
select {
default:
@@ -403,7 +408,7 @@ func testTimerChan(t *testing.T, tim timer, C <-chan Time, synctimerchan bool) {
if n = len(C); n == 1 {
return
}
- for i := 0; i < tries; i++ {
+ for range tries {
Sleep(sched)
if n = len(C); n == 1 {
return
@@ -477,7 +482,7 @@ func testTimerChan(t *testing.T, tim timer, C <-chan Time, synctimerchan bool) {
waitDone := func(done chan bool) {
t.Helper()
- for i := 0; i < tries; i++ {
+ for range tries {
Sleep(sched)
select {
case <-done:
@@ -580,7 +585,7 @@ func testTimerChan(t *testing.T, tim timer, C <-chan Time, synctimerchan bool) {
// Test enqueueTimerChan when timer is stopped.
stop = make(chan bool)
done = make(chan bool, 2)
- for i := 0; i < 2; i++ {
+ for range 2 {
go func() {
select {
case <-C:
@@ -641,7 +646,7 @@ func TestAfterTimes(t *testing.T) {
// Make sure it does.
// To avoid flakes due to very long scheduling delays,
// require 10 failures in a row before deciding something is wrong.
- for i := 0; i < 10; i++ {
+ for range 10 {
start := Now()
c := After(10 * Millisecond)
Sleep(500 * Millisecond)
@@ -657,7 +662,7 @@ func TestAfterTimes(t *testing.T) {
func TestTickTimes(t *testing.T) {
t.Parallel()
// See comment in TestAfterTimes
- for i := 0; i < 10; i++ {
+ for range 10 {
start := Now()
c := Tick(10 * Millisecond)
Sleep(500 * Millisecond)