-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_scanandwait_test.go
69 lines (59 loc) · 1.36 KB
/
example_scanandwait_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
package scanner
import (
"context"
"fmt"
"sort"
"time"
)
func ExampleScanner_ScanAndWait() {
// Example and ExampleStatus are defined in the Service type example.
services := []Service{
Example{
status: ExampleStatus{
value: "fast service: ok",
},
},
Example{
duration: 200 * time.Millisecond,
status: ExampleStatus{
value: "not so fast service: running slow",
},
},
Example{
duration: 800 * time.Second,
status: ExampleStatus{
value: "slow service: too slow",
},
},
Example{
duration: 400 * time.Millisecond,
status: ExampleStatus{
err: fmt.Errorf("unavailable service: responded with HTTP 503 Unavailable"),
},
},
}
s := New()
ctx, cancel := context.WithTimeout(context.Background(), 600*time.Millisecond)
defer cancel()
responses := s.ScanAndWait(ctx, services...)
// For testing purposes, do some sorting to ensure a stable output:
var line string
lines := []string{}
for _, status := range responses {
if err := status.Err(); err != nil {
line = err.Error()
} else {
line = status.Value().(string)
}
lines = append(lines, line)
}
sort.Strings(lines)
for _, line := range lines {
fmt.Println(line)
}
// Output:
// check cancelled: context deadline exceeded
// fast service: ok
// not so fast service: running slow
// unavailable service: responded with HTTP 503 Unavailable
}