-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner_test.go
181 lines (164 loc) · 3.86 KB
/
runner_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.
package nilgo_test
import (
"context"
"errors"
"os"
"syscall"
"testing"
"time"
"github.com/nil-go/nilgo"
"github.com/nil-go/nilgo/internal/assert"
)
func TestRunner_Run(t *testing.T) {
t.Parallel()
testcases := []struct {
description string
runner nilgo.Runner
ran bool
err string
}{
{
description: "empty runner",
ran: true,
},
{
description: "run error",
ran: true,
err: "run error",
},
{
description: "with pre-run",
runner: nilgo.New(nilgo.WithPreRun(func(ctx context.Context) error {
<-ctx.Done()
return nil
})),
ran: true,
},
{
description: "pre-run error",
runner: nilgo.New(nilgo.WithPreRun(func(context.Context) error { return errors.New("pre-run error") })),
err: "pre-run error",
ran: true,
},
{
description: "with post-run",
runner: nilgo.New(nilgo.WithPostRun(func(context.Context) error {
return nil
})),
ran: true,
},
{
description: "post-run error",
runner: nilgo.New(nilgo.WithPostRun(func(context.Context) error {
return errors.New("post-run error")
}),
),
err: "post-run error",
ran: true,
},
{
description: "with start gate",
runner: nilgo.New(nilgo.WithStartGate(func(context.Context) error { return nil })),
ran: true,
},
{
description: "start gate error",
runner: nilgo.New(nilgo.WithStartGate(func(context.Context) error { return errors.New("start gate error") })),
err: "start gate error",
},
{
description: "with stop gate",
runner: nilgo.New(nilgo.WithStopGate(func(context.Context) error { return nil })),
ran: true,
},
{
description: "stop gate error",
runner: nilgo.New(nilgo.WithStopGate(func(context.Context) error { return errors.New("stop gate error") })),
ran: true,
err: "stop gate error",
},
}
for _, testcase := range testcases {
var ran bool
t.Run(testcase.description, func(t *testing.T) {
t.Parallel()
err := testcase.runner.Run(
context.Background(),
func(context.Context) error {
ran = true
if testcase.err != "" {
return errors.New(testcase.err)
}
return nil
},
)
assert.Equal(t, testcase.ran, ran)
if testcase.err == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, testcase.err)
}
})
}
}
func TestRunner_Run_signal(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
startTime := time.Now()
var ran bool
runner := nilgo.New(
nilgo.WithPostRun(func(context.Context) error {
ran = ctx.Err() == nil
return nil
}),
)
assert.NoError(t, runner.Run(ctx,
func(ctx context.Context) error {
timer := time.NewTimer(time.Minute)
defer timer.Stop()
select {
case <-ctx.Done():
return nil
case <-timer.C:
return errors.New("timeout")
}
},
func(context.Context) error {
return syscall.Kill(os.Getpid(), syscall.SIGINT)
},
))
assert.Equal(t, true, ran)
assert.Equal(t, true, time.Since(startTime) < time.Minute)
}
func TestRunner_Run_cancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
startTime := time.Now()
var ran bool
runner := nilgo.New(
nilgo.WithPostRun(func(ctx context.Context) error {
ran = ctx.Err() == nil
return nil
}),
)
assert.NoError(t, runner.Run(ctx,
func(ctx context.Context) error {
timer := time.NewTimer(time.Minute)
defer timer.Stop()
select {
case <-ctx.Done():
return nil
case <-timer.C:
return errors.New("timeout")
}
},
func(context.Context) error {
cancel()
return nil
},
))
assert.Equal(t, true, ran)
assert.Equal(t, true, time.Since(startTime) < time.Minute)
}