-
Notifications
You must be signed in to change notification settings - Fork 4
/
closer_test.go
51 lines (39 loc) · 1.07 KB
/
closer_test.go
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
package runnable
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type dummyCloser struct {
err error
called int
}
func (c *dummyCloser) Close() error {
c.called++
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.IsType(t, &RunnableError{}, err)
require.True(t, errors.Is(err, testErr))
require.Equal(t, 1, closer.called)
}