Add TestFilesystemsPurgeCache

This commit is contained in:
Ingo Oppermann 2023-04-03 14:02:41 +02:00
parent 31a60598ee
commit 0dd4a8fb60
No known key found for this signature in database
GPG Key ID: 2AB32426E9DD229E
2 changed files with 66 additions and 5 deletions

View File

@ -11,6 +11,7 @@ import (
"time"
"github.com/datarhei/core/v16/http/api"
"github.com/datarhei/core/v16/http/cache"
httpfs "github.com/datarhei/core/v16/http/fs"
"github.com/datarhei/core/v16/http/handler"
"github.com/datarhei/core/v16/http/mock"
@ -392,7 +393,7 @@ func TestFilesystemsDeleteFilesLastmod(t *testing.T) {
require.Equal(t, int64(2), memfs.Files())
}
func TestFileOperation(t *testing.T) {
func TestFilesystemsFileOperation(t *testing.T) {
memfs1, err := fs.NewMemFilesystem(fs.MemConfig{})
require.NoError(t, err)
@ -513,3 +514,63 @@ func TestFileOperation(t *testing.T) {
require.Equal(t, filedata, response.Raw)
}
func TestFilesystemsPurgeCache(t *testing.T) {
memfs, err := fs.NewMemFilesystem(fs.MemConfig{})
require.NoError(t, err)
memfs.MkdirAll("/path", 0744)
cache, err := cache.NewLRUCache(cache.LRUConfig{
TTL: 5 * time.Minute,
})
require.NoError(t, err)
filesystems := []httpfs.FS{
{
Name: "foo",
Mountpoint: "/foo",
AllowWrite: true,
Filesystem: memfs,
DefaultFile: "index.html",
Cache: cache,
},
}
router, err := getDummyFilesystemsRouter(filesystems)
require.NoError(t, err)
data := mock.Read(t, "./fixtures/addProcess.json")
require.NotNil(t, data)
mock.Request(t, http.StatusCreated, router, "PUT", "/foo/path/index.html", data)
cache.Put("/path/", "foobar", 42)
cache.Put("/path/index.html", "barfoo", 42)
mock.Request(t, http.StatusOK, router, "DELETE", "/foo/path/index.html", nil)
f, _, err := cache.Get("/path/")
require.Nil(t, f)
require.NoError(t, err)
f, _, err = cache.Get("/path/index.html")
require.Nil(t, f)
require.NoError(t, err)
cache.Put("/path/", "foobar", 42)
cache.Put("/path/index.html", "barfoo", 42)
data = mock.Read(t, "./fixtures/addProcess.json")
require.NotNil(t, data)
mock.Request(t, http.StatusCreated, router, "PUT", "/foo/path/index.html", data)
f, _, err = cache.Get("/path/")
require.Nil(t, f)
require.NoError(t, err)
f, _, err = cache.Get("/path/index.html")
require.Nil(t, f)
require.NoError(t, err)
}

View File

@ -166,10 +166,6 @@ func (h *FSHandler) DeleteFile(c echo.Context) error {
size := h.FS.Filesystem.Remove(path)
if size < 0 {
return api.Err(http.StatusNotFound, "File not found", path)
}
if h.FS.Cache != nil {
h.FS.Cache.Delete(path)
@ -181,6 +177,10 @@ func (h *FSHandler) DeleteFile(c echo.Context) error {
}
}
if size < 0 {
return api.Err(http.StatusNotFound, "File not found", path)
}
return c.String(http.StatusOK, "Deleted: "+path)
}