Skip to content

Commit

Permalink
fix: signal channel should be buffered (#2)
Browse files Browse the repository at this point in the history
* fix: signal channel should be buffered
  • Loading branch information
clambin authored Sep 1, 2021
1 parent eff19b6 commit 59aa4ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/shutdown/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func init() {
signal.Notify(signals, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
}

var signals = make(chan os.Signal)
var signals = make(chan os.Signal, 1)

func Chan() <-chan struct{} {
shutdown := make(chan struct{})
Expand Down
18 changes: 18 additions & 0 deletions pkg/shutdown/shutdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ import (
"github.com/stretchr/testify/assert"
)

func TestBuffered(t *testing.T) {
process, err := os.FindProcess(os.Getpid())
assert.NoError(t, err)

go func() {
time.Sleep(time.Millisecond)
err := process.Signal(os.Interrupt)
assert.NoError(t, err)
}()

time.Sleep(time.Millisecond * 10)
select {
case <-Chan():
case <-time.After(time.Second):
assert.Fail(t, "shutdown never happened")
}
}

func TestChan(t *testing.T) {
testShutdown(t, Chan())
}
Expand Down

0 comments on commit 59aa4ed

Please sign in to comment.