(t *testing.T)
| 200 | } |
| 201 | |
| 202 | func 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 |
nothing calls this directly
no test coverage detected