-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_time_test.go
More file actions
60 lines (52 loc) · 1.7 KB
/
run_time_test.go
File metadata and controls
60 lines (52 loc) · 1.7 KB
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package task
import (
"context"
"errors"
"fmt"
"time"
)
func ExampleTask_Timed() {
ctx := context.Background()
begin := time.Now()
quickTask := Task(func(_ context.Context) error {
// simulates a quick task like computing 1+1
fmt.Printf("quick done at +%d socond\n", time.Since(begin)/time.Second)
return nil
}).Timed(time.Second)
quickTask.Run(ctx)
fmt.Printf("quick returns at +%d second\n", time.Since(begin)/time.Second)
begin = time.Now()
slowTask := Task(func(_ context.Context) error {
// simulates a slow task like calling web api
time.Sleep(2 * time.Second)
fmt.Printf("slow done at +%d socond\n", time.Since(begin)/time.Second)
return nil
}).Timed(time.Second)
slowTask.Run(ctx)
fmt.Printf("slow returns at +%d second\n", time.Since(begin)/time.Second)
// output: quick done at +0 socond
// quick returns at +1 second
// slow done at +2 socond
// slow returns at +2 second
}
func ExampleTask_TimedDone() {
ctx := context.Background()
begin := time.Now()
doneTask := Task(func(_ context.Context) error {
// a task which always success
return nil
}).TimedDone(time.Second)
doneTask.Run(ctx)
fmt.Printf("done returns at +%d second\n", time.Since(begin)/time.Second)
begin = time.Now()
failTask := Task(func(_ context.Context) error {
return errors.New("a task which always fail")
}).TimedDone(time.Second)
failTask.Run(ctx)
fmt.Printf("fail returns at +%d second\n", time.Since(begin)/time.Second)
// output: done returns at +1 second
// fail returns at +0 second
}