Skip to content

Commit

Permalink
accounts: add test to check appveyor/windows-timeout capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Sep 22, 2022
1 parent 1bbf005 commit d593ec0
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions accounts/ardvark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package accounts

import (
"fmt"
"testing"
"time"
)

func TestTimeOuts(t *testing.T) {
t.Log(testTimeouts(1 * time.Nanosecond))
t.Log(testTimeouts(1 * time.Microsecond))
t.Log(testTimeouts(100 * time.Microsecond))
t.Log(testTimeouts(1 * time.Millisecond))
t.Log(testTimeouts(10 * time.Millisecond))
t.Error("Nope")
}

func testTimeouts(want time.Duration) string {
var (
t0 time.Time
N = 100
)
t0 = time.Now()
for i := 0; i < N; i++ {
time.Sleep(want)
}
duration0 := time.Since(t0) / time.Duration(N)

t0 = time.Now()
for i := 0; i < N; i++ {
<-time.After(want)
}
duration1 := time.Since(t0) / time.Duration(N)

t0 = time.Now()
timer := time.NewTimer(want)
for i := 0; i < N; i++ {
<-timer.C
timer.Reset(want)
}
duration2 := time.Since(t0) / time.Duration(N)

t0 = time.Now()
ticker := time.NewTicker(want)
for i := 0; i < N; i++ {
<-ticker.C
}
duration3 := time.Since(t0) / time.Duration(N)

result := fmt.Sprintf("Attempting: %v\ntime.Sleep: %v\ntime.After: %v\ntimer: %v\nticker: %v\n",
want, duration0, duration1, duration2, duration3)
return result
}

0 comments on commit d593ec0

Please sign in to comment.