Repository navigation
Expand file tree
/
Copy pathtimeout_test.go
More file actions
84 lines (68 loc) · 2.66 KB
/
Copy pathtimeout_test.go
File metadata and controls
84 lines (68 loc) · 2.66 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package runnable
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestTimeout(t *testing.T) {
// blocking returns a runnable that blocks until cancelled, then returns err.
blocking := func(err error) Runnable {
return Func(func(ctx context.Context) error {
<-ctx.Done()
return err
})
}
noop := Func(func(context.Context) error { return nil })
returnsCtxErr := Func(func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
})
t.Run("returns the result when it finishes in time", func(t *testing.T) {
logs := captureLogs(t)
failing := Func(func(context.Context) error { return errors.New("failed") })
require.NoError(t, Timeout(time.Second, noop).Run(context.Background()))
require.EqualError(t, Timeout(time.Second, failing).Run(context.Background()), "failed")
require.Empty(t, logs.String())
})
t.Run("times out", func(t *testing.T) {
logs := captureLogs(t)
err := Named("job", Timeout(10*time.Millisecond, returnsCtxErr)).Run(context.Background())
require.EqualError(t, err, "timed out after 10ms: context deadline exceeded")
require.ErrorIs(t, err, context.DeadlineExceeded)
require.Equal(t, `level=INFO msg="job: timed out" timeout=10ms`+"\n", logs.String())
})
t.Run("succeeds when the runnable stops cleanly on the timeout", func(t *testing.T) {
logs := captureLogs(t)
require.NoError(t, Named("job", Timeout(10*time.Millisecond, blocking(nil))).Run(context.Background()))
require.Equal(t, `level=INFO msg="job: timed out" timeout=10ms`+"\n", logs.String())
})
t.Run("returns another error of a timed out runnable as is", func(t *testing.T) {
r := Func(func(ctx context.Context) error {
<-ctx.Done()
return fmt.Errorf("query: %w", ctx.Err())
})
err := Timeout(10*time.Millisecond, r).Run(context.Background())
require.EqualError(t, err, "query: context deadline exceeded")
})
t.Run("parent cancellation is not a timeout", func(t *testing.T) {
logs := captureLogs(t)
err := Timeout(time.Minute, returnsCtxErr).Run(cancelledContext())
require.ErrorIs(t, err, context.Canceled)
require.NoError(t, Timeout(time.Minute, blocking(nil)).Run(cancelledContext()))
require.Empty(t, logs.String())
})
t.Run("parent deadline is not a timeout", func(t *testing.T) {
logs := captureLogs(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
t.Cleanup(cancel)
err := Timeout(time.Minute, returnsCtxErr).Run(ctx)
require.Equal(t, context.DeadlineExceeded, err)
require.Empty(t, logs.String())
})
t.Run("name", func(t *testing.T) {
require.Equal(t, "timeout/dummyRunnable", runnableName(Timeout(time.Second, newDummyRunnable())))
})
}