-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcloser_test.go
More file actions
112 lines (87 loc) · 2.6 KB
/
closer_test.go
File metadata and controls
112 lines (87 loc) · 2.6 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package runnable
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type dummyCloser struct {
err error
called int
}
func (c *dummyCloser) Close() error {
c.called++
return c.err
}
type dummyCloserCtx struct {
called int
ctxErr error // captures ctx.Err() at call time
}
func (c *dummyCloserCtx) Close(ctx context.Context) {
c.called++
c.ctxErr = ctx.Err()
}
type dummyCloserCtxErr struct {
err error
called int
ctxErr error // captures ctx.Err() at call time
}
func (c *dummyCloserCtxErr) Close(ctx context.Context) error {
c.called++
c.ctxErr = ctx.Err()
return c.err
}
func Test_Closer_Cancellation(t *testing.T) {
AssertRunnableRespectCancellation(t, CloserErr(&dummyCloser{}), time.Second)
AssertRunnableRespectPreCancelledContext(t, CloserErr(&dummyCloser{}))
}
func Test_Closer_Close(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
closer := &dummyCloser{}
err := CloserErr(closer).Run(ctx)
require.NoError(t, err)
require.Equal(t, 1, closer.called)
}
func Test_Closer_Close_Error(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
testErr := &dummyError{"dummy error"}
closer := &dummyCloser{err: testErr}
err := CloserErr(closer).Run(ctx)
require.EqualError(t, err, "closer: Close() returned an error: dummy error")
require.ErrorAs(t, err, new(*RunnableError))
require.ErrorIs(t, err, testErr)
require.Equal(t, 1, closer.called)
}
func Test_CloserCtx(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
closer := &dummyCloserCtx{}
err := CloserCtx(closer).Run(ctx)
require.NoError(t, err)
require.Equal(t, 1, closer.called)
require.NoError(t, closer.ctxErr, "context passed to Close should not be cancelled")
}
func Test_CloserCtxErr(t *testing.T) {
t.Run("success", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
closer := &dummyCloserCtxErr{}
err := CloserCtxErr(closer).Run(ctx)
require.NoError(t, err)
require.Equal(t, 1, closer.called)
require.NoError(t, closer.ctxErr, "context passed to Close should not be cancelled")
})
t.Run("error", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
testErr := &dummyError{"close failed"}
closer := &dummyCloserCtxErr{err: testErr}
err := CloserCtxErr(closer).Run(ctx)
require.EqualError(t, err, "closer: Close() returned an error: close failed")
require.ErrorIs(t, err, testErr)
require.Equal(t, 1, closer.called)
require.NoError(t, closer.ctxErr, "context passed to Close should not be cancelled")
})
}