Home / Function/ Test_App_ShutdownWithContext() — fiber Function Reference

Test_App_ShutdownWithContext() — fiber Function Reference

Architecture documentation for the Test_App_ShutdownWithContext() function in app_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  42d5d81c_b609_eb0e_edf6_cf0480d38d66["Test_App_ShutdownWithContext()"]
  e728fdd2_242f_706b_c1d2_041b3d6badb5["app_test.go"]
  42d5d81c_b609_eb0e_edf6_cf0480d38d66 -->|defined in| e728fdd2_242f_706b_c1d2_041b3d6badb5
  style 42d5d81c_b609_eb0e_edf6_cf0480d38d66 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

app_test.go lines 1413–1540

func Test_App_ShutdownWithContext(t *testing.T) {
	t.Parallel()

	t.Run("successful shutdown", func(t *testing.T) {
		t.Parallel()
		app := New()

		// Fast request that should complete
		app.Get("/", func(c Ctx) error {
			return c.SendString("OK")
		})

		ln := fasthttputil.NewInmemoryListener()
		serverStarted := make(chan bool, 1)

		go func() {
			serverStarted <- true
			if err := app.Listener(ln); err != nil {
				t.Errorf("Failed to start listener: %v", err)
			}
		}()

		<-serverStarted

		// Execute normal request
		conn, err := ln.Dial()
		require.NoError(t, err)
		_, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"))
		require.NoError(t, err)

		// Shutdown with sufficient timeout
		ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
		defer cancel()

		err = app.ShutdownWithContext(ctx)
		require.NoError(t, err, "Expected successful shutdown")
	})

	t.Run("shutdown with hooks", func(t *testing.T) {
		t.Parallel()
		app := New()

		hookOrder := make([]string, 0)
		var hookMutex sync.Mutex

		app.Hooks().OnPreShutdown(func() error {
			hookMutex.Lock()
			hookOrder = append(hookOrder, "pre")
			hookMutex.Unlock()
			return nil
		})

		app.Hooks().OnPostShutdown(func(_ error) error {
			hookMutex.Lock()
			hookOrder = append(hookOrder, "post")
			hookMutex.Unlock()
			return nil
		})

		ln := fasthttputil.NewInmemoryListener()
		go func() {
			if err := app.Listener(ln); err != nil {
				t.Errorf("Failed to start listener: %v", err)
			}
		}()

		time.Sleep(100 * time.Millisecond)

		err := app.ShutdownWithContext(context.Background())
		require.NoError(t, err)

		require.Equal(t, []string{"pre", "post"}, hookOrder, "Hooks should execute in order")
	})

	t.Run("timeout with long running request", func(t *testing.T) {
		t.Parallel()
		app := New()

		requestStarted := make(chan struct{})
		requestProcessing := make(chan struct{})

Domain

Subdomains

Defined In

Frequently Asked Questions

What does Test_App_ShutdownWithContext() do?
Test_App_ShutdownWithContext() is a function in the fiber codebase, defined in app_test.go.
Where is Test_App_ShutdownWithContext() defined?
Test_App_ShutdownWithContext() is defined in app_test.go at line 1413.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free