aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata/timers.go
blob: c179e47445423c381e3b44359cd388444ef0becd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main

import "time"

var timer = time.NewTimer(time.Millisecond)

func main() {
	// Test ticker.
	ticker := time.NewTicker(time.Millisecond * 500)
	println("waiting on ticker")
	go func() {
		time.Sleep(time.Millisecond * 150)
		println(" - after 150ms")
		time.Sleep(time.Millisecond * 200)
		println(" - after 200ms")
		time.Sleep(time.Millisecond * 300)
		println(" - after 300ms")
	}()
	<-ticker.C
	println("waited on ticker at 500ms")
	<-ticker.C
	println("waited on ticker at 1000ms")
	ticker.Stop()
	time.Sleep(time.Millisecond * 750)
	select {
	case <-ticker.C:
		println("fail: ticker should have stopped!")
	default:
		println("ticker was stopped (didn't send anything after 750ms)")
	}

	timer := time.NewTimer(time.Millisecond * 750)
	println("waiting on timer")
	go func() {
		time.Sleep(time.Millisecond * 200)
		println(" - after 200ms")
		time.Sleep(time.Millisecond * 400)
		println(" - after 400ms")
	}()
	<-timer.C
	println("waited on timer at 750ms")
	time.Sleep(time.Millisecond * 500)

	reset := timer.Reset(time.Millisecond * 750)
	println("timer reset:", reset)
	println("waiting on timer")
	go func() {
		time.Sleep(time.Millisecond * 200)
		println(" - after 200ms")
		time.Sleep(time.Millisecond * 400)
		println(" - after 400ms")
	}()
	<-timer.C
	println("waited on timer at 750ms")
	time.Sleep(time.Millisecond * 500)
}