Skip to content

Commit

Permalink
refactor: refactor signal handling and update build tags
Browse files Browse the repository at this point in the history
- Refactor signal handling tests in `manager_test.go` to use a new `testingSignal` helper function
- Update build tags in `signals_unix.go` to exclude Windows systems

Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy committed Mar 13, 2024
1 parent e167c34 commit 72fefe5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
37 changes: 6 additions & 31 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,40 +225,15 @@ func TestGetShutdonwContext(t *testing.T) {

func TestWithSignalSIGINT(t *testing.T) {
setup()
var count int32 = 0
m := NewManager()

m.AddShutdownJob(func() error {
atomic.AddInt32(&count, 1)
return nil
})

m.AddShutdownJob(func() error {
<-m.ShutdownContext().Done()
atomic.AddInt32(&count, 1)
return nil
})

go func() {
time.Sleep(50 * time.Millisecond)
process, err := os.FindProcess(syscall.Getpid())
if err != nil {
t.Errorf("os.FindProcess error: %v", err)
}
if err := process.Signal(syscall.SIGINT); err != nil {
t.Errorf("process.Signal error: %v", err)
}
}()

<-m.Done()

if atomic.LoadInt32(&count) != 2 {
t.Errorf("count error: %v", atomic.LoadInt32(&count))
}
testingSignal(t, syscall.SIGINT)
}

func TestWithSignalSIGTERM(t *testing.T) {
setup()
testingSignal(t, syscall.SIGTERM)
}

func testingSignal(t *testing.T, signal os.Signal) {
var count int32 = 0
m := NewManager()

Expand All @@ -279,7 +254,7 @@ func TestWithSignalSIGTERM(t *testing.T) {
if err != nil {
t.Errorf("os.FindProcess error: %v", err)
}
if err := process.Signal(syscall.SIGTERM); err != nil {
if err := process.Signal(signal); err != nil {
t.Errorf("process.Signal error: %v", err)
}
}()
Expand Down
4 changes: 2 additions & 2 deletions signals_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build linux || bsd || darwin
// +build linux bsd darwin
//go:build !windows
// +build !windows

package graceful

Expand Down

0 comments on commit 72fefe5

Please sign in to comment.