Debian Codegraph @

runutil/example_test.go

raw symbols SCIP
1 // Copyright (c) The EfficientGo Authors.
2 // Licensed under the Apache License 2.0.
3
4 // Initially copied from Thanos
5 //
6 // Copyright (c) The Thanos Authors.
7 // Licensed under the Apache License 2.0.
8
9 package runutil_test
10
11 import (
12 "context"
13 "fmt"
14 "log"
15 "time"
16
17 "github.com/efficientgo/core/errors"
18 "github.com/efficientgo/core/runutil"
19 )
20
21 func ExampleRepeat() {
22 // It will stop Repeat 10 seconds later.
23 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
24 defer cancel()
25
26 // It will print out "Repeat" every 5 seconds.
27 err := runutil.Repeat(5*time.Second, ctx.Done(), func() error {
28 fmt.Println("Repeat")
29 return nil
30 })
31 if err != nil {
32 log.Fatal(err)
33 }
34 }
35
36 func ExampleRetry() {
37 // It will stop Retry 10 seconds later.
38 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
39 defer cancel()
40
41 // It will print out "Retry" every 5 seconds.
42 err := runutil.Retry(5*time.Second, ctx.Done(), func() error {
43 fmt.Println("Retry")
44 return errors.New("Try to retry")
45 })
46 if err != nil {
47 log.Fatal(err)
48 }
49 }