Skip to content

Commit b3cfd31

Browse files
committed
test(app): add test for OnStart failure cleanup behavior
Verifies that when an OnStart hook fails, Start() returns the error and OnStop hooks do not run (only internal cleanup like telemetry flush executes).
1 parent 6b941e9 commit b3cfd31

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

app/app_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,26 @@ func TestAppStartAndShutdown(t *testing.T) {
174174

175175
cancel()
176176
})
177+
178+
t.Run("onStart failure returns error and runs cleanup", func(t *testing.T) {
179+
var cleanupRan bool
180+
a, err := app.New(
181+
app.WithAddr("127.0.0.1:18955"),
182+
app.WithOnStart(func(_ context.Context) error {
183+
return fmt.Errorf("migration failed")
184+
}),
185+
app.WithOnStop(func(_ context.Context) error {
186+
cleanupRan = true
187+
return nil
188+
}),
189+
)
190+
require.NoError(t, err)
191+
192+
err = a.Start(context.Background())
193+
assert.Error(t, err)
194+
assert.Contains(t, err.Error(), "migration failed")
195+
// OnStop should NOT run — it runs only on graceful shutdown.
196+
// cleanup() (telemetry flush) runs, but not onStop hooks.
197+
assert.False(t, cleanupRan, "onStop hooks should not run on startup failure")
198+
})
177199
}

0 commit comments

Comments
 (0)