-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_tract_worker_test.go
More file actions
96 lines (80 loc) · 1.75 KB
/
example_tract_worker_test.go
File metadata and controls
96 lines (80 loc) · 1.75 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
package tract_test
import (
"context"
"fmt"
"sync"
"git.dev.kochava.com/ccurrin/tract"
)
func ExampleWorkerFactory_tractWorkerFactorySync() {
squareRootTract := tract.NewWorkerTract("square root", 4, tract.NewFactoryFromWorker(SquareRootWorker{}))
factory := tract.NewTractWorkerFactory(squareRootTract)
worker, err := factory.MakeWorker()
if err != nil {
// Handle error
return
}
args := []float64{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}
for _, arg := range args {
resultReq, success := worker.Work(context.WithValue(context.Background(), SquareRootWorkerArg{}, arg))
if !success {
fmt.Println("not successful")
}
result, _ := resultReq.Value(SquareRootWorkerResult{}).(float64)
fmt.Println(result)
}
worker.Close()
factory.Close()
// Output:
// 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
}
func ExampleWorkerFactory_tractWorkerFactoryAsync() {
squareRootTract := tract.NewWorkerTract("square root", 4, tract.NewFactoryFromWorker(SquareRootWorker{}))
factory := tract.NewTractWorkerFactory(squareRootTract)
worker, err := factory.MakeWorker()
if err != nil {
// Handle error
return
}
args := []float64{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}
results := make([]float64, len(args))
wg := sync.WaitGroup{}
for i := range args {
wg.Add(1)
go func(j int) {
defer wg.Done()
resultReq, success := worker.Work(context.WithValue(context.Background(), SquareRootWorkerArg{}, args[j]))
if !success {
fmt.Println("not successful")
}
results[j], _ = resultReq.Value(SquareRootWorkerResult{}).(float64)
}(i)
}
wg.Wait()
for _, result := range results {
fmt.Println(result)
}
worker.Close()
factory.Close()
// Output:
// 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
}