-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
121 lines (104 loc) · 2.32 KB
/
Copy pathexample_test.go
File metadata and controls
121 lines (104 loc) · 2.32 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package workerpool
import (
"context"
"fmt"
"sync/atomic"
"time"
)
func ExampleWorkerPool() {
p := New(Options{
Capacity: 10,
IdleTimeout: 5 * time.Minute,
CreateIfNoWorkersAvailable: true,
})
count := uint32(0)
for i := 0; i < 100; i++ {
n := uint32(i + 1)
_ = p.Submit(context.TODO(), func(context.Context) {
atomic.AddUint32(&count, n)
})
}
_ = p.WaitDone(context.TODO())
fmt.Println(count)
// Output:
// 5050
}
func ExampleWorkerPool_locklessOperation() {
p := New(Options{
Capacity: 8,
WaitIfNoWorkersAvailable: true,
CreateWorkerID: true,
})
values := make([]uint32, 8, 8)
for i := 0; i < 100; i++ {
n := uint32(i + 1)
_ = p.Submit(context.TODO(), func(ctx context.Context) {
id, ok := WorkerID(ctx)
if !ok {
panic("not possible")
}
// The worker id starts with 1.
values[id-1] += n
time.Sleep(10 * time.Millisecond) // Too fast, sleep for a while..
})
}
_ = p.WaitDone(context.TODO())
sum := uint32(0)
count := 0
for _, v := range values {
if v > 0 {
count++
}
sum += v
}
fmt.Println(count)
fmt.Println(sum)
// Output:
// 8
// 5050
}
func ExamplePipeline() {
pool := New(Options{
Capacity: 4,
WaitIfNoWorkersAvailable: true,
})
pipeline := NewPipelineWith[int, int](PipelineOptions{
FeederAsyncExecutor: GoSpawn,
WorkerAsyncExecutor: pool.Submit,
})
inputs := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
_ = pipeline.StartFeeder(context.Background(), inputs)
_ = pipeline.StartWorkerN(context.Background(), 4, func(_ context.Context, i int) int {
return i * 2
})
sum := 0
for v := range pipeline.Join() {
sum += v
}
fmt.Println("zero canceled:", len(inputs) == pipeline.ProcessedCount())
fmt.Println("sum:", sum)
_ = pool.WaitDone(context.TODO()) // Clean up.
// Output:
// zero canceled: true
// sum: 272
}
func ExampleWrap() {
pool := New(Options{
Capacity: 8,
WaitIfNoWorkersAvailable: true,
})
increase := func(a int) int {
return a + 1
}
wrappedIncrease := Wrap(pool, func(_ context.Context, i int) (int, error) {
return increase(i), nil
})
count := 0
for i := 0; i < 100; i++ {
count, _ = wrappedIncrease(context.TODO(), count)
}
_ = pool.WaitDone(context.TODO())
fmt.Println(count)
// Output:
// 100
}