-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathworker_test.go
97 lines (84 loc) · 1.9 KB
/
worker_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
88
89
90
91
92
93
94
95
96
97
package DxCommonLib
import (
"fmt"
"testing"
"time"
)
func worktest(data ...interface{}) {
if len(data) < 3 {
Sleep(time.Second)
}
fmt.Println(data...)
}
func TestWorker(t *testing.T) {
work := NewWorkers(100, 0)
for i := 0; i <= 10; i++ {
work.PostFunc(worktest, 3, 4)
work.PostFunc(worktest, 3, 4, 6, 786, 867)
work.PostFunc(worktest, 345, 34, "$56456")
}
<-After(time.Second * 12)
fmt.Println("准备关闭")
work.Stop()
}
func TestTimeWheelWorker_After(t *testing.T) {
mm := NewTimeWheelWorker(time.Millisecond*2, 6000) //目前只能精确到2毫秒,低于2毫秒,就不行了
start := time.Now()
mm.AfterFunc(time.Second, func(data ...interface{}) {
fmt.Println("一秒之后执行,", time.Now().Sub(start).String())
}, start)
c1 := mm.After(time.Second * 8)
c4 := mm.After(time.Second * 2)
//mm.Sleep(time.Millisecond * 980)
c2 := mm.After(time.Second * 1)
c3 := mm.After(time.Second * 18)
c11 := mm.After(time.Millisecond * 500)
c12 := mm.After(time.Millisecond * 500)
mm.AfterFunc(time.Millisecond*500, func(data ...interface{}) {
fmt.Println("500毫秒之后执行,", time.Now().Sub(start).String())
}, start)
go func() {
select {
case <-c11:
fmt.Println("C11触发:", time.Now().Sub(start))
}
}()
go func() {
select {
case <-c12:
fmt.Println("C12触发:", time.Now().Sub(start))
}
}()
c5 := mm.After(time.Second * 2)
go func() {
select {
case <-c2:
fmt.Println("C2 1秒触发:", time.Now().Sub(start))
}
}()
go func() {
select {
case <-c4:
fmt.Println("C4 2s后触发:", time.Now().Sub(start))
}
}()
go func() {
select {
case <-c5:
fmt.Println("C5触发:", time.Now().Sub(start))
}
}()
go func() {
select {
case <-c3:
fmt.Println("C3触发:", time.Now().Sub(start))
}
}()
go func() {
select {
case <-c1:
fmt.Println("C1触发:", time.Now().Sub(start))
}
}()
time.Sleep(time.Second * 10)
}