Home / Function/ Test_App_Test_Goroutine_Leak_Compare() — fiber Function Reference

Test_App_Test_Goroutine_Leak_Compare() — fiber Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

app_test.go lines 84–175

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

	testCases := []struct {
		handler    Handler
		name       string
		timeout    time.Duration
		sleepTime  time.Duration
		expectLeak bool
	}{
		{
			name: "With timeout (potential leak)",
			handler: func(c Ctx) error {
				time.Sleep(300 * time.Millisecond) // Simulate time-consuming operation
				return c.SendString("ok")
			},
			timeout:    50 * time.Millisecond,  // // Short timeout to ensure triggering
			sleepTime:  500 * time.Millisecond, // Wait time longer than handler execution time
			expectLeak: true,
		},
		{
			name: "Without timeout (no leak)",
			handler: func(c Ctx) error {
				return c.SendString("ok") // Return immediately
			},
			timeout:    0, // Disable timeout
			sleepTime:  100 * time.Millisecond,
			expectLeak: false,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			app := New()

			// Record initial goroutine count
			initialGoroutines := runtime.NumGoroutine()
			t.Logf("[%s] Initial goroutines: %d", tc.name, initialGoroutines)

			app.Get("/", tc.handler)

			// Send 10 requests
			numRequests := 10
			for range numRequests {
				req := httptest.NewRequest(MethodGet, "/", http.NoBody)

				if tc.timeout > 0 {
					_, err := app.Test(req, TestConfig{
						Timeout:       tc.timeout,
						FailOnTimeout: true,
					})
					require.Error(t, err)
					require.ErrorIs(t, err, os.ErrDeadlineExceeded)
				} else if resp, err := app.Test(req); err != nil {
					t.Errorf("unexpected error: %v", err)
				} else {
					require.Equal(t, 200, resp.StatusCode)
				}
			}

			// Wait for normal goroutines to complete
			time.Sleep(tc.sleepTime)

			// Check final goroutine count
			finalGoroutines := runtime.NumGoroutine()
			leakedGoroutines := finalGoroutines - initialGoroutines
			if leakedGoroutines < 0 {
				leakedGoroutines = 0
			}
			t.Logf("[%s] Final goroutines: %d (leaked: %d)",
				tc.name, finalGoroutines, leakedGoroutines)

			if tc.expectLeak {
				// We allow up to 3x the request count to account for noise; zero is tolerated.
				maxLeak := numRequests * 3
				if leakedGoroutines > maxLeak {
					t.Errorf("[%s] Expected at most %d leaked goroutines, but got %d",
						tc.name, maxLeak, leakedGoroutines)
				}
				return

Domain

Subdomains

Defined In

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free