Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app: Test that PID file is removed #31

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ func (a *App) runShutdownHooks(ctx context.Context) error {
return nil
}

var background = context.Background()

// Run will start the App, running the startup Hooks, then the Processes.
// It will wait for any signals before shutting down first the Processes then the shutdown Hooks.
// This behaviour can be customised by using Launch, WaitForShutdown, and Shutdown.
func (a *App) Run() int {
ac := NewAppContext(context.Background())
ac := NewAppContext(background)
defer ac.Stop()
defer a.cleanup(ac.TerminationContext)

Expand Down
12 changes: 12 additions & 0 deletions app_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lu

import (
"context"
"testing"
)

func SetBackgroundContextForTesting(t *testing.T, ctx context.Context) {
old := background
t.Cleanup(func() { background = old })
background = ctx
}
66 changes: 66 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package lu_test

import (
"context"
"io"
"os"
"testing"
"time"

"github.com/luno/jettison/jtest"
"github.com/luno/jettison/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/luno/lu"
Expand Down Expand Up @@ -237,3 +240,66 @@ func TestRunningProcesses(t *testing.T) {
})
}
}

func TestPIDRemoved(t *testing.T) {
tests := []struct {
name string
app func(t *testing.T) *lu.App
expExit int
}{
{
name: "not using pid",
app: func(t *testing.T) *lu.App {
var a lu.App
a.AddProcess(process.NoOp())
return &a
},
expExit: 1,
},
{
name: "normal app",
app: func(t *testing.T) *lu.App {
a := lu.App{UseProcessFile: true}
a.AddProcess(process.NoOp())
return &a
},
expExit: 1,
},
{
name: "app fails to start",
app: func(t *testing.T) *lu.App {
a := lu.App{UseProcessFile: true}
a.OnStartUp(func(ctx context.Context) error {
return io.ErrUnexpectedEOF
})
a.AddProcess(process.NoOp())
return &a
},
expExit: 1,
},
{
name: "app fails to shutdown",
app: func(t *testing.T) *lu.App {
a := lu.App{UseProcessFile: true}
a.OnShutdown(func(ctx context.Context) error {
return io.ErrUnexpectedEOF
})
a.AddProcess(process.NoOp())
return &a
},
expExit: 1,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)
lu.SetBackgroundContextForTesting(t, ctx)

exit := tc.app(t).Run()
assert.Equal(t, tc.expExit, exit)
_, err := os.Open("/tmp/lu.pid")
assert.True(t, os.IsNotExist(err))
})
}
}
Loading