Skip to content

Commit

Permalink
Adding test to validate that the cache invalidator is not called if n…
Browse files Browse the repository at this point in the history
…o cache entry exist

Signed-off-by: brunodmartins <bdm2943@icloud.com>
  • Loading branch information
brunodmartins committed Jul 19, 2024
1 parent 3395cd5 commit ad51c7b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion middleware/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func New(config ...Config) fiber.Handler {
// Get timestamp
ts := atomic.LoadUint64(&timestamp)

//Cache Entry not found
// Cache Entry not found
if e != nil {
// Invalidate cache if requested
if cfg.CacheInvalidator != nil && cfg.CacheInvalidator(c) {
Expand Down
70 changes: 45 additions & 25 deletions middleware/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,35 +700,55 @@ func Test_CustomCacheHeader(t *testing.T) {
func Test_CacheInvalidation(t *testing.T) {
t.Parallel()

app := fiber.New()
app.Use(New(Config{
CacheControl: true,
CacheInvalidator: func(c fiber.Ctx) bool {
return fiber.Query[bool](c, "invalidate")
},
}))
t.Run("Invalidation by requests", func(t *testing.T) {
t.Parallel()
app := fiber.New()
app.Use(New(Config{
CacheControl: true,
CacheInvalidator: func(c fiber.Ctx) bool {
return fiber.Query[bool](c, "invalidate")
},
}))

app.Get("/", func(c fiber.Ctx) error {
return c.SendString(time.Now().String())
})

app.Get("/", func(c fiber.Ctx) error {
return c.SendString(time.Now().String())
})
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
require.NoError(t, err)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
require.NoError(t, err)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
respCached, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
require.NoError(t, err)
bodyCached, err := io.ReadAll(respCached.Body)
require.NoError(t, err)
require.True(t, bytes.Equal(body, bodyCached))
require.NotEmpty(t, respCached.Header.Get(fiber.HeaderCacheControl))

respCached, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
require.NoError(t, err)
bodyCached, err := io.ReadAll(respCached.Body)
require.NoError(t, err)
require.True(t, bytes.Equal(body, bodyCached))
require.NotEmpty(t, respCached.Header.Get(fiber.HeaderCacheControl))
respInvalidate, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?invalidate=true", nil))
require.NoError(t, err)
bodyInvalidate, err := io.ReadAll(respInvalidate.Body)
require.NoError(t, err)
require.NotEqual(t, body, bodyInvalidate)
})

respInvalidate, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?invalidate=true", nil))
require.NoError(t, err)
bodyInvalidate, err := io.ReadAll(respInvalidate.Body)
require.NoError(t, err)
require.NotEqual(t, body, bodyInvalidate)
t.Run("Cache Invalidator should not be called if no cache entry exist ", func(t *testing.T) {
t.Parallel()
app := fiber.New()
cacheInvalidatorExecuted := false
app.Use(New(Config{
CacheControl: true,
CacheInvalidator: func(c fiber.Ctx) bool {
cacheInvalidatorExecuted = true
return fiber.Query[bool](c, "invalidate")
},
MaxBytes: 10 * 1024 * 1024,
}))
_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?invalidate=true", nil))
require.NoError(t, err)
require.False(t, cacheInvalidatorExecuted)
})
}

// Because time points are updated once every X milliseconds, entries in tests can often have
Expand Down
5 changes: 3 additions & 2 deletions middleware/cache/manager_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cache

import (
"github.com/gofiber/utils/v2"
"github.com/stretchr/testify/assert"
"testing"
"time"

"github.com/gofiber/utils/v2"
"github.com/stretchr/testify/assert"
)

func Test_manager_get(t *testing.T) {
Expand Down

0 comments on commit ad51c7b

Please sign in to comment.