forked from gorpher/gone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo_test.go
More file actions
59 lines (55 loc) · 1012 Bytes
/
Copy pathgo_test.go
File metadata and controls
59 lines (55 loc) · 1012 Bytes
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
package gone
import (
"context"
"fmt"
"testing"
"time"
)
func TestAfterStopFunc(t *testing.T) {
var lastIndex int
AfterStopFunc(10*time.Second, func(c <-chan struct{}) {
for i := 5; i >= 0; i-- {
select {
case <-c:
return
default:
doSomeThing(i)
if i == 4 {
lastIndex = i
return
}
}
}
})
if lastIndex != 4 {
t.Errorf("lastIndex error: want=%d,actual=%d", 4, lastIndex)
}
time.Sleep(time.Second)
fmt.Println("finished")
}
func TestAfterStopWithContext(t *testing.T) {
var lastIndex int
AfterStopWithContext(10*time.Second, func(c context.Context) {
for i := 5; i >= 0; i-- {
select {
case <-c.Done():
return
default:
doSomeThing(i)
if i == 4 {
lastIndex = i
return
}
}
}
})
if lastIndex != 4 {
t.Errorf("lastIndex error: want=%d,actual=%d", 4, lastIndex)
}
time.Sleep(time.Second)
fmt.Println("finished")
}
func doSomeThing(i int) {
time.Sleep(1 * time.Second)
fmt.Printf("do %d something...\n", i)
}