@@ -3,6 +3,7 @@ package core
33import (
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