MCPcopy
hub / github.com/gofiber/fiber / Test_Hook_OnPostShutdown

Function Test_Hook_OnPostShutdown

hooks_test.go:202–267  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

200}
201
202func Test_Hook_OnPostShutdown(t *testing.T) {
203 t.Run("should execute post shutdown hook with error", func(t *testing.T) {
204 app := New()
205 expectedErr := errors.New("test shutdown error")
206
207 hookCalled := make(chan error, 1)
208 defer close(hookCalled)
209
210 app.Hooks().OnPostShutdown(func(err error) error {
211 hookCalled <- err
212 return nil
213 })
214
215 go func() {
216 if err := app.Listen(":0"); err != nil {
217 return
218 }
219 }()
220
221 time.Sleep(100 * time.Millisecond)
222
223 app.hooks.executeOnPostShutdownHooks(expectedErr)
224
225 select {
226 case err := <-hookCalled:
227 require.Equal(t, expectedErr, err)
228 case <-time.After(time.Second):
229 t.Fatal("hook execution timeout")
230 }
231
232 require.NoError(t, app.Shutdown())
233 })
234
235 t.Run("should execute multiple hooks in order", func(t *testing.T) {
236 app := New()
237
238 execution := make([]int, 0)
239
240 app.Hooks().OnPostShutdown(func(_ error) error {
241 execution = append(execution, 1)
242 return nil
243 })
244
245 app.Hooks().OnPostShutdown(func(_ error) error {
246 execution = append(execution, 2)
247 return nil
248 })
249
250 app.hooks.executeOnPostShutdownHooks(nil)
251
252 require.Len(t, execution, 2, "expected 2 hooks to execute")
253 require.Equal(t, []int{1, 2}, execution, "hooks executed in wrong order")
254 })
255
256 t.Run("should handle hook error", func(_ *testing.T) {
257 app := New()
258 hookErr := errors.New("hook error")
259

Callers

nothing calls this directly

Calls 9

OnPostShutdownMethod · 0.80
HooksMethod · 0.80
ListenMethod · 0.80
ShutdownMethod · 0.80
NewFunction · 0.70
NewMethod · 0.65
FatalMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected