Home / Function/ testGracefulShutdown() — fiber Function Reference

testGracefulShutdown() — fiber Function Reference

Architecture documentation for the testGracefulShutdown() function in listen_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  fc797446_626b_fd16_3091_e642de92def1["testGracefulShutdown()"]
  edd60c6a_f37d_c392_5301_d2d5a002990a["listen_test.go"]
  fc797446_626b_fd16_3091_e642de92def1 -->|defined in| edd60c6a_f37d_c392_5301_d2d5a002990a
  92f7a176_3470_1493_3e76_20a4dff1ff04["Test_Listen_Graceful_Shutdown()"]
  92f7a176_3470_1493_3e76_20a4dff1ff04 -->|calls| fc797446_626b_fd16_3091_e642de92def1
  style fc797446_626b_fd16_3091_e642de92def1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

listen_test.go lines 55–164

func testGracefulShutdown(t *testing.T, shutdownTimeout time.Duration) {
	t.Helper()

	var mu sync.Mutex
	var shutdown bool
	var receivedErr error

	app := New()
	app.Get("/", func(c Ctx) error {
		time.Sleep(10 * time.Millisecond)
		return c.SendString(c.Hostname())
	})

	ln := fasthttputil.NewInmemoryListener()
	errs := make(chan error, 1)

	app.hooks.OnPostShutdown(func(err error) error {
		mu.Lock()
		defer mu.Unlock()
		shutdown = true
		receivedErr = err
		return nil
	})

	go func() {
		ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
		defer cancel()

		errs <- app.Listener(ln, ListenConfig{
			DisableStartupMessage: true,
			GracefulContext:       ctx,
			ShutdownTimeout:       shutdownTimeout,
		})
	}()

	require.Eventually(t, func() bool {
		conn, err := ln.Dial()
		if err == nil {
			if err := conn.Close(); err != nil {
				t.Logf("error closing connection: %v", err)
			}
			return true
		}
		return false
	}, time.Second, 100*time.Millisecond, "Server failed to become ready")

	client := fasthttp.HostClient{
		Dial: func(_ string) (net.Conn, error) { return ln.Dial() },
	}

	type testCase struct {
		expectedErr        error
		expectedBody       string
		name               string
		waitTime           time.Duration
		expectedStatusCode int
		closeConnection    bool
	}

	testCases := []testCase{
		{
			name:               "Server running normally",
			waitTime:           500 * time.Millisecond,
			expectedBody:       "example.com",
			expectedStatusCode: StatusOK,
			expectedErr:        nil,
			closeConnection:    true,
		},
		{
			name:               "Server shutdown complete",
			waitTime:           3 * time.Second,
			expectedBody:       "",
			expectedStatusCode: StatusOK,
			expectedErr:        fasthttputil.ErrInmemoryListenerClosed,
			closeConnection:    true,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			time.Sleep(tc.waitTime)

Domain

Subdomains

Defined In

Frequently Asked Questions

What does testGracefulShutdown() do?
testGracefulShutdown() is a function in the fiber codebase, defined in listen_test.go.
Where is testGracefulShutdown() defined?
testGracefulShutdown() is defined in listen_test.go at line 55.
What calls testGracefulShutdown()?
testGracefulShutdown() is called by 1 function(s): Test_Listen_Graceful_Shutdown.

Analyze Your Own Codebase

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

Try Supermodel Free