forked from name5566/leaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
70 lines (57 loc) · 819 Bytes
/
example_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
package g_test
import (
"fmt"
"github.com/name5566/leaf/go"
"time"
)
func Example() {
d := g.New(10)
// go 1
var res int
d.Go(func() {
fmt.Println("1 + 1 = ?")
res = 1 + 1
}, func() {
fmt.Println(res)
})
d.Cb(<-d.ChanCb)
// go 2
d.Go(func() {
fmt.Print("My name is ")
}, func() {
fmt.Println("Leaf")
})
d.Close()
// Output:
// 1 + 1 = ?
// 2
// My name is Leaf
}
func ExampleLinearContext() {
d := g.New(10)
// parallel
d.Go(func() {
time.Sleep(time.Second / 2)
fmt.Println("1")
}, nil)
d.Go(func() {
fmt.Println("2")
}, nil)
d.Cb(<-d.ChanCb)
d.Cb(<-d.ChanCb)
// linear
c := d.NewLinearContext()
c.Go(func() {
time.Sleep(time.Second / 2)
fmt.Println("1")
}, nil)
c.Go(func() {
fmt.Println("2")
}, nil)
d.Close()
// Output:
// 2
// 1
// 1
// 2
}