-
Notifications
You must be signed in to change notification settings - Fork 1
/
exampleErrorTriggersCancellation_test.go
89 lines (77 loc) · 2.6 KB
/
exampleErrorTriggersCancellation_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
package sup_test
import (
"context"
"fmt"
"sync"
"time"
"github.com/warpfork/go-sup"
)
// This eaxmple demonstrates a fan-out of goroutines in which one of the
// tasks errors... and go-sup automatically cancels any remaining tasks,
// while also ensuring only the errors from the first task are returned.
//
// This is the same basic content as ExampleSuperviseForkJoin, but now
// exercises all those extra safety features you just wouldn't get if you
// wrote this in plain Go yourself without some nice library assistance.
//
func ExampleSuperviseForkJoin_errorsTriggerSiblingCancellationg() {
var foobarIn = map[string]int{
"a": 1, "b": 2, "c": 3, "d": 4,
}
// We'll use this to force order in our child goroutines.
// Without it, the example would run so fast we'd have no way to
// expect which of the child tasks would successfully complete
// versus be slow enough to get cancelled!
var lockstepper int = 1
var mu sync.Mutex
// Our second task is a bomb: it'll return an error.
// This will cause the later tasks to be cancelled!
err := sup.SuperviseRoot(context.Background(),
sup.SuperviseForkJoin("main",
sup.TasksFromMap(foobarIn, func(ctx context.Context, k_, v_ interface{}) error {
k, v := k_.(string), v_.(int)
for {
// Busy wait. (But this is just a test; we won't be here for long.)
mu.Lock()
if lockstepper == v {
defer func() {
lockstepper++
mu.Unlock()
}()
break
}
mu.Unlock()
time.Sleep(1)
}
// ..... We can't really get this to wait until the supervisor
// is certain to have finished acknowledging the error, and also
// then to be certain the context quit channel close has propagated,
// unless we produce so much additional mutexing that it would
// actually make the scheduler behavior itself non-load-bearing.
// So there's a hacky sleep here, I guess.
// This gets this test to pass the "majority" of the time.
// And I believe if we had race-detector'able bugs, it would still
// trigger, since a sleep *is not* a memory fence; so that's good.
time.Sleep(100)
if ctx.Err() != nil {
fmt.Printf("Oh no! My context is %v!\n", ctx.Err())
return ctx.Err()
}
if k == "b" {
fmt.Printf("This task errors!\n")
return fmt.Errorf("Boom!")
}
fmt.Printf("The task for %q completed :)\n", k)
return nil
}),
),
)
fmt.Printf("final error: %v\n", err)
// Output:
//
// The task for "a" completed :)
// This task errors!
// Oh no! My context is context canceled!
// Oh no! My context is context canceled!
// final error: Boom!
}