Skip to content

Commit 1dc6157

Browse files
authored
fix(serve): signal group couldn't be canceled (#208)
* fix(serve): signal group couldn't be canceled * fix: remove dupe
1 parent e72a19d commit 1dc6157

2 files changed

Lines changed: 36 additions & 18 deletions

File tree

serve.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func (s serveIn) cronServe(ctx context.Context, logger logging.LevelLogger) (fun
157157
func (s serveIn) signalWatch(ctx context.Context, logger logging.LevelLogger) (func() error, func(err error), error) {
158158
sig := make(chan os.Signal, 1)
159159
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
160+
ctx, cancel := context.WithCancel(ctx)
160161
return func() error {
161162
select {
162163
case n := <-sig:
@@ -167,6 +168,7 @@ func (s serveIn) signalWatch(ctx context.Context, logger logging.LevelLogger) (f
167168
return nil
168169
}, func(err error) {
169170
signal.Stop(sig)
171+
cancel()
170172
}, nil
171173
}
172174

serve_test.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"os"
78
"testing"
89
"time"
@@ -19,22 +20,37 @@ func TestServeIn_signalWatch(t *testing.T) {
1920
do, cancel, err := in.signalWatch(context.Background(), logging.WithLevel(log.NewLogfmtLogger(&buf)))
2021
assert.NoError(t, err)
2122

22-
var group run.Group
23-
group.Add(do, cancel)
24-
group.Add(func() error {
25-
time.Sleep(time.Second)
26-
p, err := os.FindProcess(os.Getpid())
27-
if err != nil {
28-
t.Skip("TestServeIn_signalWatch only works on unix")
29-
}
30-
if err := p.Signal(os.Interrupt); err != nil {
31-
t.Skip("TestServeIn_signalWatch only works on unix")
32-
}
33-
// trigger the signal twice should be ok.
34-
p.Signal(os.Interrupt)
35-
return nil
36-
}, func(err error) {})
37-
err = group.Run()
38-
assert.NoError(t, err)
39-
assert.Contains(t, buf.String(), "signal received: interrupt")
23+
t.Run("stop when signal received", func(t *testing.T) {
24+
var group run.Group
25+
group.Add(do, cancel)
26+
group.Add(func() error {
27+
time.Sleep(time.Second)
28+
p, err := os.FindProcess(os.Getpid())
29+
if err != nil {
30+
t.Skip("TestServeIn_signalWatch only works on unix")
31+
}
32+
if err := p.Signal(os.Interrupt); err != nil {
33+
t.Skip("TestServeIn_signalWatch only works on unix")
34+
}
35+
// trigger the signal twice should be ok.
36+
p.Signal(os.Interrupt)
37+
return nil
38+
}, func(err error) {})
39+
err = group.Run()
40+
assert.NoError(t, err)
41+
assert.Contains(t, buf.String(), "signal received: interrupt")
42+
})
43+
44+
t.Run("cancel when cancel func is called", func(t *testing.T) {
45+
var group run.Group
46+
group.Add(do, cancel)
47+
group.Add(func() error {
48+
return errors.New("some err")
49+
}, func(err error) {
50+
51+
})
52+
err = group.Run()
53+
assert.Contains(t, buf.String(), "context canceled")
54+
})
55+
4056
}

0 commit comments

Comments
 (0)