| 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_testscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/runutil_test`/ |
| 10 | |
| 11 | import ( |
| 12 | "contextscip-go gomod github.com/golang/go/src go1.17 context/" |
| 13 | "fmtscip-go gomod github.com/golang/go/src go1.17 fmt/" |
| 14 | "logscip-go gomod github.com/golang/go/src go1.17 log/" |
| 15 | "timescip-go gomod github.com/golang/go/src go1.17 time/" |
| 16 | |
| 17 | "github.com/efficientgo/core/errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/" |
| 18 | "github.com/efficientgo/core/runutilscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/runutil`/" |
| 19 | ) |
| 20 | |
| 21 | func ExampleRepeatscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/runutil_test`/ExampleRepeat().() { |
| 22 | // It will stop Repeat 10 seconds later. |
| 23 | ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.17 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.17 context/WithTimeout().(contextscip-go gomod github.com/golang/go/src go1.17 context/.Backgroundscip-go gomod github.com/golang/go/src go1.17 context/Background().(), 10*timescip-go gomod github.com/golang/go/src go1.17 time/.Secondscip-go gomod github.com/golang/go/src go1.17 time/Second.) |
| 24 | defer cancel() |
| 25 | |
| 26 | // It will print out "Repeat" every 5 seconds. |
| 27 | err := runutilscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/runutil`/.Repeatscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/runutil`/Repeat().(5*timescip-go gomod github.com/golang/go/src go1.17 time/.Secondscip-go gomod github.com/golang/go/src go1.17 time/Second., ctx.Donescip-go gomod github.com/golang/go/src go1.17 context/Context#Done().(), func() error { |
| 28 | fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.17 fmt/Println().("Repeat") |
| 29 | return nil |
| 30 | }) |
| 31 | if err != nil { |
| 32 | logscip-go gomod github.com/golang/go/src go1.17 log/.Fatalscip-go gomod github.com/golang/go/src go1.17 log/Fatal().(err) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func ExampleRetryscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/runutil_test`/ExampleRetry().() { |
| 37 | // It will stop Retry 10 seconds later. |
| 38 | ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.17 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.17 context/WithTimeout().(contextscip-go gomod github.com/golang/go/src go1.17 context/.Backgroundscip-go gomod github.com/golang/go/src go1.17 context/Background().(), 10*timescip-go gomod github.com/golang/go/src go1.17 time/.Secondscip-go gomod github.com/golang/go/src go1.17 time/Second.) |
| 39 | defer cancel() |
| 40 | |
| 41 | // It will print out "Retry" every 5 seconds. |
| 42 | err := runutilscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/runutil`/.Retryscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/runutil`/Retry().(5*timescip-go gomod github.com/golang/go/src go1.17 time/.Secondscip-go gomod github.com/golang/go/src go1.17 time/Second., ctx.Donescip-go gomod github.com/golang/go/src go1.17 context/Context#Done().(), func() error { |
| 43 | fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.17 fmt/Println().("Retry") |
| 44 | return errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/.Newscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/New().("Try to retry") |
| 45 | }) |
| 46 | if err != nil { |
| 47 | logscip-go gomod github.com/golang/go/src go1.17 log/.Fatalscip-go gomod github.com/golang/go/src go1.17 log/Fatal().(err) |
| 48 | } |
| 49 | } |