-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathharness_test.go
127 lines (119 loc) · 3.16 KB
/
harness_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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package harness_test
import (
"context"
"errors"
"syscall"
"testing"
"time"
gomock "github.com/golang/mock/gomock"
"github.com/netologist/harness"
"github.com/stretchr/testify/assert"
)
func TestNewHarness(t *testing.T) {
tests := []struct {
name string
runFunc func(context.Context) error
shutdownFunc func(harness.ExitType)
signalFunc func()
expectedErr error
expectedExitType harness.ExitType
}{
{
name: "Successful Run and Shutdown",
runFunc: func(ctx context.Context) error {
// Simulate some work
return nil
},
signalFunc: func() {},
shutdownFunc: func(exitType harness.ExitType) {
assert.Equal(t, harness.ExitTypeNormal, exitType)
},
expectedErr: nil,
},
{
name: "Handle When Run Returns Error",
runFunc: func(ctx context.Context) error {
return errors.New("run error")
},
signalFunc: func() {},
shutdownFunc: func(exitType harness.ExitType) {
assert.Equal(t, harness.ExitTypeCancel, exitType)
},
expectedErr: errors.New("run error"),
},
{
name: "Handle Run When Throw Panic",
runFunc: func(ctx context.Context) error {
panic("run panic error")
},
signalFunc: func() {},
shutdownFunc: func(exitType harness.ExitType) {
assert.Equal(t, harness.ExitTypeCancel, exitType)
},
expectedErr: errors.New("recovered run panic error"),
},
{
name: "Handle Shutdown When Throw Panic",
runFunc: func(ctx context.Context) error {
// Simulate successful execution
return nil
},
signalFunc: func() {},
shutdownFunc: func(exitType harness.ExitType) {
assert.Equal(t, harness.ExitTypeNormal, exitType)
panic("shutdown panic")
},
expectedErr: errors.New("recovered shutdown panic"),
},
{
name: "Handle Terminate When Send Signal",
runFunc: func(ctx context.Context) error {
time.Sleep(2 * time.Second)
return nil
},
signalFunc: func() {
go func() {
time.Sleep(100 * time.Microsecond)
assert.Nil(t, syscall.Kill(syscall.Getpid(), syscall.SIGTERM))
}()
},
shutdownFunc: func(exitType harness.ExitType) {
assert.Equal(t, harness.ExitTypeSignal, exitType)
},
expectedErr: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var actualErr error
var isGracefullyShutdown bool
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockRunner := harness.NewMockRunner(ctrl)
mockRunner.EXPECT().Run(gomock.Any()).DoAndReturn(test.runFunc).Times(1)
mockRunner.EXPECT().Shutdown(gomock.Any()).DoAndReturn(test.shutdownFunc).Times(1)
if test.expectedErr != nil {
mockRunner.EXPECT().OnError(gomock.Any()).Times(1)
} else {
mockRunner.EXPECT().OnError(gomock.Any()).Times(0)
}
test.signalFunc()
harness.New(
harness.Register(mockRunner),
harness.OnError(func(err error) {
actualErr = err
}),
harness.OnCompleted(func() {
isGracefullyShutdown = true
}),
).Start(context.Background())
// Check the results
if test.expectedErr != nil {
assert.Equal(t, test.expectedErr, actualErr)
} else {
assert.Empty(t, actualErr)
}
assert.True(t, isGracefullyShutdown)
})
}
}