forked from euphoria-io/scope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
85 lines (70 loc) · 1.7 KB
/
examples_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
package scope_test
import (
"fmt"
"time"
"euphoria.io/scope"
)
func ExampleBreakpointer() {
root := scope.New()
// A function that returns an error, which we want to simulate.
output := func(arg string) error {
_, err := fmt.Println(arg)
return err
}
// A function that we want to test the error handling of.
verifyOutput := func(ctx scope.Context, arg string) error {
if err := ctx.Check("output()", arg); err != nil {
return err
}
return output(arg)
}
// Set a breakpoint on a particular invocation of output.
ctrl := root.Breakpoint("output()", "fail")
// Other invocations should proceed as normal.
err := verifyOutput(root, "normal behavior")
fmt.Println("verifyOutput returned", err)
// Our breakpoint should allow us to inject an error. To control it
// we must spin off a goroutine.
go func() {
<-ctrl // synchronize at beginning of verifyOutput
ctrl <- fmt.Errorf("test error")
}()
err = verifyOutput(root, "fail")
fmt.Println("verifyOutput returned", err)
// We can also inject an error by terminating the context.
go func() {
<-ctrl
root.Cancel()
}()
err = verifyOutput(root, "fail")
fmt.Println("verifyOutput returned", err)
// Output:
// normal behavior
// verifyOutput returned <nil>
// verifyOutput returned test error
// verifyOutput returned context cancelled
}
func ExampleContext_cancellation() {
ctx := scope.New()
go func() {
time.Sleep(50 * time.Millisecond)
ctx.Cancel()
}()
loop:
for {
t := time.After(10 * time.Millisecond)
select {
case <-ctx.Done():
break loop
case <-t:
fmt.Println("tick")
}
}
fmt.Println("finished with", ctx.Err())
// Output:
// tick
// tick
// tick
// tick
// finished with context cancelled
}