Created
August 6, 2019 02:55
-
-
Save jblebrun/9c5206d3f0a7c7a19df0c61e3da88c80 to your computer and use it in GitHub Desktop.
Showing varying behavior of sleep & afterfunc across system suspends depending on presence of Ticker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import( | |
"time" | |
"log" | |
"sync" | |
) | |
func main() { | |
var wg sync.WaitGroup | |
wg.Add(2) | |
start := time.Now() | |
start0 := start.Round(0) | |
log.Println("START") | |
log.Printf("%#v", start) | |
_ = time.NewTicker(time.Second) | |
/* | |
go func() { | |
ticks := 0 | |
for { | |
<-t.C | |
ticks++ | |
log.Println("TICKS",ticks) | |
} | |
}() | |
*/ | |
go func() { | |
time.Sleep(30 * time.Second) | |
gstop := time.Now() | |
log.Println("GO SLEEP", gstop.Sub(start), gstop.Round(0).Sub(start0)) | |
log.Printf("%#v %+v", time.Now(), time.Now().Round(0)) | |
wg.Done() | |
}() | |
time.AfterFunc(30 * time.Second, func() { | |
gstop := time.Now() | |
log.Println("AFTERFUNC", gstop.Sub(start), gstop.Round(0).Sub(start0)) | |
log.Printf("%#v %+v", time.Now(), time.Now().Round(0)) | |
wg.Done() | |
}) | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment