-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratecounter_test.go
87 lines (75 loc) · 1.49 KB
/
ratecounter_test.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package ratecounter
import (
"fmt"
"log"
"time"
"testing"
)
func TestMilisecond(t *testing.T) { // count miliseconds in a second
counter := NewRateCounter(time.Second)
counter.Start()
label := "miliseconds"
stop_tick := make(chan bool)
go func(stop chan bool) {
ticker := time.NewTicker(time.Millisecond)
for {
select {
case <- ticker.C:
counter.Inc(label)
case <-stop:
return
}
}
}(stop_tick)
stop_print := make(chan bool)
go func(stop chan bool) {
ticker := time.NewTicker(time.Second)
for {
select {
case <- ticker.C:
fmt.Printf("%s: %d \n", label, counter.Count(label))
case <-stop:
return
}
}
}(stop_print)
<-time.After(10*time.Second)
stop_tick <- true
stop_print <- true
counter.Stop()
log.Println("Exiting.")
}
func TestNanosecond(t *testing.T) { // count nanoseconds in a second
counter := NewRateCounter(time.Second)
counter.Start()
label := "nanoseconds"
stop_tick := make(chan bool)
go func(stop chan bool) {
ticker := time.NewTicker(time.Nanosecond)
for {
select {
case <- ticker.C:
counter.Inc(label)
case <-stop:
return
}
}
}(stop_tick)
stop_print := make(chan bool)
go func(stop chan bool) {
ticker := time.NewTicker(time.Second)
for {
select {
case <- ticker.C:
fmt.Printf("%s: %d \n", label, counter.Count(label))
case <-stop:
return
}
}
}(stop_print)
<-time.After(10*time.Second)
stop_tick <- true
stop_print <- true
counter.Stop()
log.Println("Exiting.")
}