Skip to content

Commit

Permalink
feat: Add option to disable signal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Jun 17, 2024
1 parent d0d12e9 commit f445148
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,23 @@ func (l loggerOption) String() string {
return fmt.Sprintf("fx.Logger(%v)", l.p)
}

func DisableSignalHandler() Option {

Check failure on line 259 in app.go

View workflow job for this annotation

GitHub Actions / Lint

exported: exported function DisableSignalHandler should have comment or be unexported (revive)
return disableSignalHandler{}
}

type disableSignalHandler struct{}

var _ Option = disableSignalHandler{}

func (disableSignalHandler) apply(m *module) {
m.app.receivers.notify = func(c chan<- os.Signal, sig ...os.Signal) {}
m.app.receivers.stopNotify = func(c chan<- os.Signal) {}
}

func (disableSignalHandler) String() string {
return "fx.DisableSignalHandler()"

Check warning on line 273 in app.go

View check run for this annotation

Codecov / codecov/patch

app.go#L272-L273

Added lines #L272 - L273 were not covered by tests
}

// NopLogger disables the application's log output.
//
// Note that this makes some failures difficult to debug,
Expand Down
36 changes: 36 additions & 0 deletions app_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
package fx

import (
"context"
"errors"
"fmt"
"os"
"sync"
"testing"

Expand Down Expand Up @@ -115,3 +117,37 @@ func TestAnnotationError(t *testing.T) {
assert.ErrorIs(t, err, wantErr)
assert.Contains(t, err.Error(), wantErr.Error())
}

// TestDisableSignalHandler tests disabling the default signal handler.
// It's an internal test to allow us replace the app.receivers.notify function.
func TestDisableSignalHandler(t *testing.T) {
for _, disableSignalHandler := range []bool{false, true} {
testName := "Not disabling signal handler"
if disableSignalHandler {
testName = "Disabling signal handler"
}
t.Run(testName, func(t *testing.T) {
app := New()
calledNotify := false

app.receivers.notify = func(c chan<- os.Signal, sig ...os.Signal) {
calledNotify = true
}

if disableSignalHandler {
opt := DisableSignalHandler()
opt.apply(app.root)
}

app.Start(context.Background())
app.Stop(context.Background())

if disableSignalHandler {
assert.False(t, calledNotify)
} else {
assert.True(t, calledNotify)
}
})

}
}

0 comments on commit f445148

Please sign in to comment.