-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_service_test.go
46 lines (39 loc) · 1.02 KB
/
example_service_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
package scanner
import (
"context"
"fmt"
"time"
)
// Example of a service which status can be checked.
type Example struct {
// lets suppose a check takes some time
duration time.Duration
// eventually the check returns this status (unless it is cancelled)
status ExampleStatus
}
// Check is an example check function that supports cancellation.
func (e Example) Check(ctx context.Context) Status {
select {
case <-ctx.Done():
return ExampleStatus{err: fmt.Errorf("check cancelled: %w", ctx.Err())}
case <-time.After(e.duration):
return e.status
}
}
// ExampleStatus is a simple example with a string value.
//
// The value could have any type, and scanner doesn't modify it in any way.
type ExampleStatus struct {
value string
err error
}
// Value implements the Status interface.
func (s ExampleStatus) Value() interface{} {
return s.value
}
// Err implements the Status interface.
func (s ExampleStatus) Err() error {
return s.err
}
// Ensures the example is included in the documentation.
func ExampleService() {}