This repository was archived by the owner on Dec 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshutdown_test.go
More file actions
73 lines (56 loc) · 1.39 KB
/
shutdown_test.go
File metadata and controls
73 lines (56 loc) · 1.39 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
package shutdown_test
import (
"context"
"errors"
"sync/atomic"
"testing"
"github.com/stretchr/testify/require"
"github.com/gripmock/shutdown"
)
var (
ErrSame = errors.New("some error")
ErrSame1 = errors.New("some error 1")
ErrSame2 = errors.New("some error 2")
ErrSame3 = errors.New("some error 3")
)
type logger struct {
errors []error
}
func (l *logger) Err(err error) {
l.errors = append(l.errors, err)
}
func TestShutdown_LoggerNil(t *testing.T) {
var val atomic.Bool
s := shutdown.New(nil)
s.Add(func(_ context.Context) error {
val.Store(true)
return ErrSame
})
s.Do(t.Context())
require.True(t, val.Load())
}
func TestShutdown_Stack_ErrorAll(t *testing.T) {
l := &logger{}
s := shutdown.New(l)
s.Add(
func(_ context.Context) error { return ErrSame1 },
func(_ context.Context) error { return ErrSame2 },
func(_ context.Context) error { return ErrSame3 },
)
s.Do(t.Context())
require.ErrorIs(t, l.errors[0], ErrSame3)
require.ErrorIs(t, l.errors[1], ErrSame2)
require.ErrorIs(t, l.errors[2], ErrSame1)
}
func TestShutdown_Stack_Error(t *testing.T) {
l := &logger{}
s := shutdown.New(l)
s.Add(
func(_ context.Context) error { return ErrSame1 },
func(_ context.Context) error { return nil },
func(_ context.Context) error { return ErrSame3 },
)
s.Do(t.Context())
require.ErrorIs(t, l.errors[0], ErrSame3)
require.ErrorIs(t, l.errors[1], ErrSame1)
}