Skip to content

Commit

Permalink
Merge branch 'master' into joaks/self
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobOaks authored May 16, 2024
2 parents 724bb8d + cb9cccf commit afe49a5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func (sig ShutdownSignal) String() string {

func newSignalReceivers() signalReceivers {
return signalReceivers{
notify: signal.Notify,
signals: make(chan os.Signal, 1),
notify: signal.Notify,
stopNotify: signal.Stop,
signals: make(chan os.Signal, 1),
}
}

Expand All @@ -64,7 +65,8 @@ type signalReceivers struct {
finished chan struct{}

// this stub allows us to unit test signal relay functionality
notify func(c chan<- os.Signal, sig ...os.Signal)
notify func(c chan<- os.Signal, sig ...os.Signal)
stopNotify func(c chan<- os.Signal)

// last will contain a pointer to the last ShutdownSignal received, or
// nil if none, if a new channel is created by Wait or Done, this last
Expand Down Expand Up @@ -118,6 +120,7 @@ func (recv *signalReceivers) Start(ctx context.Context) {
func (recv *signalReceivers) Stop(ctx context.Context) error {
recv.m.Lock()
defer recv.m.Unlock()
recv.stopNotify(recv.signals)

// if the relayer is not running; return nil error
if !recv.running() {
Expand Down
5 changes: 5 additions & 0 deletions signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func TestSignal(t *testing.T) {
}
}()
}
var stopCalledTimes int
recv.stopNotify = func(ch chan<- os.Signal) {
stopCalledTimes++
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
recv.Start(ctx)
Expand All @@ -110,6 +114,7 @@ func TestSignal(t *testing.T) {
sig := <-recv.Wait()
require.Equal(t, syscall.SIGTERM, sig.Signal)
require.NoError(t, recv.Stop(ctx))
require.Equal(t, 1, stopCalledTimes)
close(stub)
})
})
Expand Down

0 comments on commit afe49a5

Please sign in to comment.