Home / Function/ Test_Cache_MaxBytes_ConcurrencyAndRaceConditions() — fiber Function Reference

Test_Cache_MaxBytes_ConcurrencyAndRaceConditions() — fiber Function Reference

Architecture documentation for the Test_Cache_MaxBytes_ConcurrencyAndRaceConditions() function in cache_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  510bbefd_12e3_f421_6e3a_806f4a065b1c["Test_Cache_MaxBytes_ConcurrencyAndRaceConditions()"]
  8453a087_9678_fe96_1b20_2d125b6f8656["cache_test.go"]
  510bbefd_12e3_f421_6e3a_806f4a065b1c -->|defined in| 8453a087_9678_fe96_1b20_2d125b6f8656
  style 510bbefd_12e3_f421_6e3a_806f4a065b1c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/cache/cache_test.go lines 3917–4007

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

	t.Run("concurrent requests with MaxBytes limit", func(t *testing.T) {
		t.Parallel()
		app := fiber.New()

		const maxBytes = uint(1000)
		const numGoroutines = 20
		const requestsPerGoroutine = 5

		app.Use(New(Config{
			MaxBytes:   maxBytes,
			Expiration: 10 * time.Second,
		}))

		app.Get("/*", func(c fiber.Ctx) error {
			// Return data that will fill up the cache
			return c.Send(make([]byte, 50))
		})

		// Launch multiple goroutines making concurrent requests
		var wg sync.WaitGroup
		errChan := make(chan error, numGoroutines*requestsPerGoroutine)

		for i := 0; i < numGoroutines; i++ {
			id := i
			wg.Add(1) //nolint:revive // Standard WaitGroup pattern is appropriate here
			go func() {
				defer wg.Done()
				for j := 0; j < requestsPerGoroutine; j++ {
					path := fmt.Sprintf("/test-%d-%d", id, j)
					req := httptest.NewRequest(fiber.MethodGet, path, http.NoBody)
					_, err := app.Test(req)
					if err != nil {
						errChan <- err
					}
				}
			}()
		}

		wg.Wait()
		close(errChan)

		// Check for errors
		for err := range errChan {
			require.NoError(t, err, "concurrent request failed")
		}

		// The test passes if no errors occurred and no race conditions were detected by -race flag
	})

	t.Run("concurrent requests near capacity triggers eviction", func(t *testing.T) {
		t.Parallel()
		app := fiber.New()

		const maxBytes = uint(200)
		const numRequests = 10

		app.Use(New(Config{
			MaxBytes:   maxBytes,
			Expiration: 10 * time.Second,
		}))

		app.Get("/*", func(c fiber.Ctx) error {
			// Each response is about 50 bytes, so we'll exceed capacity
			return c.Send(make([]byte, 50))
		})

		// Make concurrent requests that will trigger evictions
		var wg sync.WaitGroup
		for i := 0; i < numRequests; i++ {
			id := i
			wg.Add(1) //nolint:revive // Standard WaitGroup pattern is appropriate here
			go func() {
				defer wg.Done()
				path := fmt.Sprintf("/item-%d", id)
				req := httptest.NewRequest(fiber.MethodGet, path, http.NoBody)
				_, err := app.Test(req)
				if err != nil {
					t.Logf("request error: %v", err)

Subdomains

Frequently Asked Questions

What does Test_Cache_MaxBytes_ConcurrencyAndRaceConditions() do?
Test_Cache_MaxBytes_ConcurrencyAndRaceConditions() is a function in the fiber codebase, defined in middleware/cache/cache_test.go.
Where is Test_Cache_MaxBytes_ConcurrencyAndRaceConditions() defined?
Test_Cache_MaxBytes_ConcurrencyAndRaceConditions() is defined in middleware/cache/cache_test.go at line 3917.

Analyze Your Own Codebase

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

Try Supermodel Free