2020-02-28 15:44:15 +01:00
|
|
|
package vfscache
|
2017-11-06 22:38:52 +01:00
|
|
|
|
|
|
|
import (
|
2018-04-06 20:13:27 +02:00
|
|
|
"context"
|
2017-11-28 15:18:48 +01:00
|
|
|
"fmt"
|
2017-11-06 22:38:52 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-11-28 15:18:48 +01:00
|
|
|
"sort"
|
2017-11-06 22:38:52 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/djherbis/times"
|
2020-02-28 15:44:15 +01:00
|
|
|
_ "github.com/rclone/rclone/backend/local" // import the local backend
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fstest"
|
2020-02-28 15:44:15 +01:00
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
2017-11-06 22:38:52 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-02-28 15:44:15 +01:00
|
|
|
// TestMain drives the tests
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
fstest.TestMain(m)
|
2017-11-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
2017-11-28 15:18:48 +01:00
|
|
|
// convert c.item to a string
|
2020-02-28 15:44:15 +01:00
|
|
|
func itemAsString(c *Cache) []string {
|
2017-11-28 15:18:48 +01:00
|
|
|
c.itemMu.Lock()
|
|
|
|
defer c.itemMu.Unlock()
|
|
|
|
var out []string
|
|
|
|
for name, item := range c.item {
|
2019-10-28 18:05:41 +01:00
|
|
|
out = append(out, fmt.Sprintf("name=%q isFile=%v opens=%d size=%d", filepath.ToSlash(name), item.isFile, item.opens, item.size))
|
2017-11-28 15:18:48 +01:00
|
|
|
}
|
|
|
|
sort.Strings(out)
|
2018-02-26 17:59:14 +01:00
|
|
|
return out
|
2017-11-28 15:18:48 +01:00
|
|
|
}
|
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
func TestCacheNew(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2018-02-16 15:12:46 +01:00
|
|
|
defer cancel()
|
2017-11-06 22:38:52 +01:00
|
|
|
|
2018-02-16 15:12:46 +01:00
|
|
|
// Disable the cache cleaner as it interferes with these tests
|
2020-02-28 15:44:15 +01:00
|
|
|
opt := vfscommon.DefaultOpt
|
2018-02-16 15:12:46 +01:00
|
|
|
opt.CachePollInterval = 0
|
2020-02-28 15:44:15 +01:00
|
|
|
c, err := New(ctx, r.Fremote, &opt)
|
2017-11-06 22:38:52 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Contains(t, c.root, "vfs")
|
2020-02-28 15:44:15 +01:00
|
|
|
assert.Contains(t, c.fcache.Root(), filepath.Base(r.Fremote.Root()))
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string(nil), itemAsString(c))
|
2017-11-06 22:38:52 +01:00
|
|
|
|
|
|
|
// mkdir
|
2020-02-28 15:44:15 +01:00
|
|
|
p, err := c.Mkdir("potato")
|
2017-11-06 22:38:52 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "potato", filepath.Base(p))
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=0 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2017-11-06 22:38:52 +01:00
|
|
|
|
|
|
|
fi, err := os.Stat(filepath.Dir(p))
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, fi.IsDir())
|
|
|
|
|
|
|
|
// get
|
|
|
|
item := c.get("potato")
|
|
|
|
item2 := c.get("potato")
|
|
|
|
assert.Equal(t, item, item2)
|
|
|
|
assert.WithinDuration(t, time.Now(), item.atime, time.Second)
|
|
|
|
|
|
|
|
// updateTime
|
|
|
|
//.. before
|
|
|
|
t1 := time.Now().Add(-60 * time.Minute)
|
2019-02-02 00:35:03 +01:00
|
|
|
c.updateStat("potato", t1, 0)
|
2017-11-06 22:38:52 +01:00
|
|
|
item = c.get("potato")
|
|
|
|
assert.NotEqual(t, t1, item.atime)
|
|
|
|
assert.Equal(t, 0, item.opens)
|
|
|
|
//..after
|
|
|
|
t2 := time.Now().Add(60 * time.Minute)
|
2019-02-02 00:35:03 +01:00
|
|
|
c.updateStat("potato", t2, 0)
|
2017-11-06 22:38:52 +01:00
|
|
|
item = c.get("potato")
|
|
|
|
assert.Equal(t, t2, item.atime)
|
|
|
|
assert.Equal(t, 0, item.opens)
|
|
|
|
|
|
|
|
// open
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="potato" isFile=true opens=0 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Open("/potato")
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=1 size=0`,
|
|
|
|
`name="potato" isFile=true opens=1 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2017-11-06 22:38:52 +01:00
|
|
|
item = c.get("potato")
|
|
|
|
assert.WithinDuration(t, time.Now(), item.atime, time.Second)
|
|
|
|
assert.Equal(t, 1, item.opens)
|
|
|
|
|
|
|
|
// write the file
|
|
|
|
err = ioutil.WriteFile(p, []byte("hello"), 0600)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// read its atime
|
|
|
|
fi, err = os.Stat(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
atime := times.Get(fi).AccessTime()
|
|
|
|
|
|
|
|
// updateAtimes
|
|
|
|
item = c.get("potato")
|
|
|
|
item.atime = time.Now().Add(-24 * time.Hour)
|
2019-02-02 00:35:03 +01:00
|
|
|
err = c.updateStats()
|
2017-11-06 22:38:52 +01:00
|
|
|
require.NoError(t, err)
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=1 size=0`,
|
|
|
|
`name="potato" isFile=true opens=1 size=5`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2017-11-06 22:38:52 +01:00
|
|
|
item = c.get("potato")
|
|
|
|
assert.Equal(t, atime, item.atime)
|
|
|
|
|
2018-02-02 13:06:42 +01:00
|
|
|
// updateAtimes - not in the cache
|
|
|
|
oldItem := item
|
2018-02-15 22:34:37 +01:00
|
|
|
c.itemMu.Lock()
|
2018-02-02 13:06:42 +01:00
|
|
|
delete(c.item, "potato") // remove from cache
|
2018-02-15 22:34:37 +01:00
|
|
|
c.itemMu.Unlock()
|
2019-02-02 00:35:03 +01:00
|
|
|
err = c.updateStats()
|
2018-02-02 13:06:42 +01:00
|
|
|
require.NoError(t, err)
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=1 size=0`,
|
|
|
|
`name="potato" isFile=true opens=0 size=5`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2018-02-02 13:06:42 +01:00
|
|
|
item = c.get("potato")
|
|
|
|
assert.Equal(t, atime, item.atime)
|
2018-02-15 22:34:37 +01:00
|
|
|
c.itemMu.Lock()
|
2018-02-02 13:06:42 +01:00
|
|
|
c.item["potato"] = oldItem // restore to cache
|
2018-02-15 22:34:37 +01:00
|
|
|
c.itemMu.Unlock()
|
2018-02-02 13:06:42 +01:00
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
// try purging with file open
|
|
|
|
c.purgeOld(10 * time.Second)
|
|
|
|
_, err = os.Stat(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// close
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=1 size=0`,
|
|
|
|
`name="potato" isFile=true opens=1 size=5`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2019-02-02 00:35:03 +01:00
|
|
|
c.updateStat("potato", t2, 6)
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=1 size=0`,
|
|
|
|
`name="potato" isFile=true opens=1 size=6`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Close("potato/")
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="potato" isFile=true opens=0 size=5`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2017-11-06 22:38:52 +01:00
|
|
|
item = c.get("potato")
|
|
|
|
assert.WithinDuration(t, time.Now(), item.atime, time.Second)
|
|
|
|
assert.Equal(t, 0, item.opens)
|
|
|
|
|
|
|
|
// try purging with file closed
|
|
|
|
c.purgeOld(10 * time.Second)
|
2020-05-20 12:39:20 +02:00
|
|
|
// ...nothing should happen
|
2017-11-06 22:38:52 +01:00
|
|
|
_, err = os.Stat(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
//.. purge again with -ve age
|
|
|
|
c.purgeOld(-10 * time.Second)
|
|
|
|
_, err = os.Stat(p)
|
|
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
|
|
|
|
// clean - have tested the internals already
|
|
|
|
c.clean()
|
|
|
|
|
|
|
|
// cleanup
|
2020-02-28 15:44:15 +01:00
|
|
|
err = c.CleanUp()
|
2017-11-06 22:38:52 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = os.Stat(c.root)
|
|
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
}
|
2017-11-28 15:18:48 +01:00
|
|
|
|
|
|
|
func TestCacheOpens(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-02-28 15:44:15 +01:00
|
|
|
c, err := New(ctx, r.Fremote, &vfscommon.DefaultOpt)
|
2017-11-28 15:18:48 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string(nil), itemAsString(c))
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Open("potato")
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=1 size=0`,
|
|
|
|
`name="potato" isFile=true opens=1 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Open("potato")
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=2 size=0`,
|
|
|
|
`name="potato" isFile=true opens=2 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Close("potato")
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=1 size=0`,
|
|
|
|
`name="potato" isFile=true opens=1 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Close("potato")
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="potato" isFile=true opens=0 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2017-11-28 15:18:48 +01:00
|
|
|
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Open("potato")
|
|
|
|
c.Open("a//b/c/d/one")
|
|
|
|
c.Open("a/b/c/d/e/two")
|
|
|
|
c.Open("a/b/c/d/e/f/three")
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=4 size=0`,
|
|
|
|
`name="a" isFile=false opens=3 size=0`,
|
|
|
|
`name="a/b" isFile=false opens=3 size=0`,
|
|
|
|
`name="a/b/c" isFile=false opens=3 size=0`,
|
|
|
|
`name="a/b/c/d" isFile=false opens=3 size=0`,
|
|
|
|
`name="a/b/c/d/e" isFile=false opens=2 size=0`,
|
|
|
|
`name="a/b/c/d/e/f" isFile=false opens=1 size=0`,
|
|
|
|
`name="a/b/c/d/e/f/three" isFile=true opens=1 size=0`,
|
|
|
|
`name="a/b/c/d/e/two" isFile=true opens=1 size=0`,
|
|
|
|
`name="a/b/c/d/one" isFile=true opens=1 size=0`,
|
|
|
|
`name="potato" isFile=true opens=1 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Close("potato")
|
|
|
|
c.Close("a/b/c/d/one")
|
|
|
|
c.Close("a/b/c/d/e/two")
|
|
|
|
c.Close("a/b/c//d/e/f/three")
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="a" isFile=false opens=0 size=0`,
|
|
|
|
`name="a/b" isFile=false opens=0 size=0`,
|
|
|
|
`name="a/b/c" isFile=false opens=0 size=0`,
|
|
|
|
`name="a/b/c/d" isFile=false opens=0 size=0`,
|
|
|
|
`name="a/b/c/d/e" isFile=false opens=0 size=0`,
|
|
|
|
`name="a/b/c/d/e/f" isFile=false opens=0 size=0`,
|
|
|
|
`name="a/b/c/d/e/f/three" isFile=true opens=0 size=0`,
|
|
|
|
`name="a/b/c/d/e/two" isFile=true opens=0 size=0`,
|
|
|
|
`name="a/b/c/d/one" isFile=true opens=0 size=0`,
|
|
|
|
`name="potato" isFile=true opens=0 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
// test the open, mkdir, purge, close, purge sequence
|
|
|
|
func TestCacheOpenMkdir(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Disable the cache cleaner as it interferes with these tests
|
2020-02-28 15:44:15 +01:00
|
|
|
opt := vfscommon.DefaultOpt
|
2018-02-26 17:59:14 +01:00
|
|
|
opt.CachePollInterval = 0
|
2020-02-28 15:44:15 +01:00
|
|
|
c, err := New(ctx, r.Fremote, &opt)
|
2018-02-26 17:59:14 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// open
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Open("sub/potato")
|
2018-02-26 17:59:14 +01:00
|
|
|
|
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=1 size=0`,
|
|
|
|
`name="sub" isFile=false opens=1 size=0`,
|
|
|
|
`name="sub/potato" isFile=true opens=1 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
|
|
|
|
|
|
|
// mkdir
|
2020-02-28 15:44:15 +01:00
|
|
|
p, err := c.Mkdir("sub/potato")
|
2018-02-26 17:59:14 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "potato", filepath.Base(p))
|
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=1 size=0`,
|
|
|
|
`name="sub" isFile=false opens=1 size=0`,
|
|
|
|
`name="sub/potato" isFile=true opens=1 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2017-11-28 15:18:48 +01:00
|
|
|
|
2018-02-26 17:59:14 +01:00
|
|
|
// test directory exists
|
|
|
|
fi, err := os.Stat(filepath.Dir(p))
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, fi.IsDir())
|
|
|
|
|
|
|
|
// clean the cache
|
|
|
|
c.purgeOld(-10 * time.Second)
|
|
|
|
|
|
|
|
// test directory still exists
|
|
|
|
fi, err = os.Stat(filepath.Dir(p))
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, fi.IsDir())
|
|
|
|
|
|
|
|
// close
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Close("sub/potato")
|
2018-02-26 17:59:14 +01:00
|
|
|
|
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/potato" isFile=true opens=0 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
|
|
|
|
|
|
|
// clean the cache
|
|
|
|
c.purgeOld(-10 * time.Second)
|
2019-02-02 00:35:03 +01:00
|
|
|
c.purgeEmptyDirs()
|
2018-02-26 17:59:14 +01:00
|
|
|
|
|
|
|
assert.Equal(t, []string(nil), itemAsString(c))
|
|
|
|
|
|
|
|
// test directory does not exist
|
2020-02-12 12:20:54 +01:00
|
|
|
_, err = os.Stat(filepath.Dir(p))
|
2018-02-26 17:59:14 +01:00
|
|
|
require.True(t, os.IsNotExist(err))
|
2017-11-28 15:18:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheCacheDir(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-02-28 15:44:15 +01:00
|
|
|
c, err := New(ctx, r.Fremote, &vfscommon.DefaultOpt)
|
2017-11-28 15:18:48 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string(nil), itemAsString(c))
|
2017-11-28 15:18:48 +01:00
|
|
|
|
|
|
|
c.cacheDir("dir")
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="dir" isFile=false opens=0 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2017-11-28 15:18:48 +01:00
|
|
|
|
|
|
|
c.cacheDir("dir/sub")
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="dir" isFile=false opens=0 size=0`,
|
|
|
|
`name="dir/sub" isFile=false opens=0 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2017-11-28 15:18:48 +01:00
|
|
|
|
|
|
|
c.cacheDir("dir/sub2/subsub2")
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="dir" isFile=false opens=0 size=0`,
|
|
|
|
`name="dir/sub" isFile=false opens=0 size=0`,
|
|
|
|
`name="dir/sub2" isFile=false opens=0 size=0`,
|
|
|
|
`name="dir/sub2/subsub2" isFile=false opens=0 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2017-11-28 15:18:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCachePurgeOld(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-02-28 15:44:15 +01:00
|
|
|
c, err := New(ctx, r.Fremote, &vfscommon.DefaultOpt)
|
2017-11-28 15:18:48 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Test funcs
|
|
|
|
var removed []string
|
|
|
|
removedDir := true
|
|
|
|
removeFile := func(name string) {
|
2019-10-28 18:05:41 +01:00
|
|
|
removed = append(removed, filepath.ToSlash(name))
|
2017-11-28 15:18:48 +01:00
|
|
|
}
|
|
|
|
removeDir := func(name string) bool {
|
|
|
|
if removedDir {
|
2019-10-28 18:05:41 +01:00
|
|
|
removed = append(removed, filepath.ToSlash(name)+"/")
|
2017-11-28 15:18:48 +01:00
|
|
|
}
|
|
|
|
return removedDir
|
|
|
|
}
|
|
|
|
|
|
|
|
removed = nil
|
2019-02-02 00:35:03 +01:00
|
|
|
c._purgeOld(-10*time.Second, removeFile)
|
|
|
|
c._purgeEmptyDirs(removeDir)
|
2017-11-28 15:18:48 +01:00
|
|
|
assert.Equal(t, []string(nil), removed)
|
|
|
|
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Open("sub/dir2/potato2")
|
|
|
|
c.Open("sub/dir/potato")
|
|
|
|
c.Close("sub/dir2/potato2")
|
|
|
|
c.Open("sub/dir/potato")
|
2018-02-26 17:59:14 +01:00
|
|
|
|
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=2 size=0`,
|
|
|
|
`name="sub" isFile=false opens=2 size=0`,
|
|
|
|
`name="sub/dir" isFile=false opens=2 size=0`,
|
|
|
|
`name="sub/dir/potato" isFile=true opens=2 size=0`,
|
|
|
|
`name="sub/dir2" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir2/potato2" isFile=true opens=0 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
|
|
|
|
|
|
|
removed = nil
|
|
|
|
removedDir = true
|
2019-02-02 00:35:03 +01:00
|
|
|
c._purgeOld(-10*time.Second, removeFile)
|
|
|
|
c._purgeEmptyDirs(removeDir)
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
|
|
|
"sub/dir2/potato2",
|
|
|
|
"sub/dir2/",
|
|
|
|
}, removed)
|
|
|
|
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Close("sub/dir/potato")
|
2017-11-28 15:18:48 +01:00
|
|
|
|
2018-02-26 17:59:14 +01:00
|
|
|
removed = nil
|
|
|
|
removedDir = true
|
2019-02-02 00:35:03 +01:00
|
|
|
c._purgeOld(-10*time.Second, removeFile)
|
|
|
|
c._purgeEmptyDirs(removeDir)
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string(nil), removed)
|
|
|
|
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Close("sub/dir/potato")
|
2018-02-26 17:59:14 +01:00
|
|
|
|
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir/potato" isFile=true opens=0 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2017-11-28 15:18:48 +01:00
|
|
|
|
|
|
|
removed = nil
|
|
|
|
removedDir = false
|
2019-02-02 00:35:03 +01:00
|
|
|
c._purgeOld(10*time.Second, removeFile)
|
|
|
|
c._purgeEmptyDirs(removeDir)
|
2017-11-28 15:18:48 +01:00
|
|
|
assert.Equal(t, []string(nil), removed)
|
|
|
|
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string{
|
2019-02-02 00:35:03 +01:00
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir/potato" isFile=true opens=0 size=0`,
|
2018-02-26 17:59:14 +01:00
|
|
|
}, itemAsString(c))
|
2017-11-28 15:18:48 +01:00
|
|
|
|
|
|
|
removed = nil
|
|
|
|
removedDir = true
|
2019-02-02 00:35:03 +01:00
|
|
|
c._purgeOld(-10*time.Second, removeFile)
|
|
|
|
c._purgeEmptyDirs(removeDir)
|
2017-11-28 15:18:48 +01:00
|
|
|
assert.Equal(t, []string{
|
|
|
|
"sub/dir/potato",
|
|
|
|
"sub/dir/",
|
|
|
|
"sub/",
|
|
|
|
"/",
|
|
|
|
}, removed)
|
|
|
|
|
2018-02-26 17:59:14 +01:00
|
|
|
assert.Equal(t, []string(nil), itemAsString(c))
|
2017-11-28 15:18:48 +01:00
|
|
|
}
|
2019-02-02 00:35:03 +01:00
|
|
|
|
|
|
|
func TestCachePurgeOverQuota(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Disable the cache cleaner as it interferes with these tests
|
2020-02-28 15:44:15 +01:00
|
|
|
opt := vfscommon.DefaultOpt
|
2019-02-02 00:35:03 +01:00
|
|
|
opt.CachePollInterval = 0
|
2020-02-28 15:44:15 +01:00
|
|
|
c, err := New(ctx, r.Fremote, &opt)
|
2019-02-02 00:35:03 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Test funcs
|
|
|
|
var removed []string
|
|
|
|
remove := func(name string) {
|
2019-10-28 18:05:41 +01:00
|
|
|
removed = append(removed, filepath.ToSlash(name))
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Remove(name)
|
2019-02-02 00:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
removed = nil
|
|
|
|
c._purgeOverQuota(-1, remove)
|
|
|
|
assert.Equal(t, []string(nil), removed)
|
|
|
|
|
|
|
|
removed = nil
|
|
|
|
c._purgeOverQuota(0, remove)
|
|
|
|
assert.Equal(t, []string(nil), removed)
|
|
|
|
|
|
|
|
removed = nil
|
|
|
|
c._purgeOverQuota(1, remove)
|
|
|
|
assert.Equal(t, []string(nil), removed)
|
|
|
|
|
|
|
|
// Make some test files
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Open("sub/dir/potato")
|
|
|
|
p, err := c.Mkdir("sub/dir/potato")
|
2019-02-02 00:35:03 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = ioutil.WriteFile(p, []byte("hello"), 0600)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-02-28 15:44:15 +01:00
|
|
|
p, err = c.Mkdir("sub/dir2/potato2")
|
|
|
|
c.Open("sub/dir2/potato2")
|
2019-02-02 00:35:03 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = ioutil.WriteFile(p, []byte("hello2"), 0600)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
`name="" isFile=false opens=2 size=0`,
|
|
|
|
`name="sub" isFile=false opens=2 size=0`,
|
|
|
|
`name="sub/dir" isFile=false opens=1 size=0`,
|
|
|
|
`name="sub/dir/potato" isFile=true opens=1 size=0`,
|
|
|
|
`name="sub/dir2" isFile=false opens=1 size=0`,
|
|
|
|
`name="sub/dir2/potato2" isFile=true opens=1 size=0`,
|
|
|
|
}, itemAsString(c))
|
|
|
|
|
|
|
|
// Check nothing removed
|
|
|
|
removed = nil
|
|
|
|
c._purgeOverQuota(1, remove)
|
|
|
|
assert.Equal(t, []string(nil), removed)
|
|
|
|
|
|
|
|
// Close the files
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Close("sub/dir/potato")
|
|
|
|
c.Close("sub/dir2/potato2")
|
2019-02-02 00:35:03 +01:00
|
|
|
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir/potato" isFile=true opens=0 size=5`,
|
|
|
|
`name="sub/dir2" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir2/potato2" isFile=true opens=0 size=6`,
|
|
|
|
}, itemAsString(c))
|
|
|
|
|
|
|
|
// Update the stats to read the total size
|
|
|
|
err = c.updateStats()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(11), c.used)
|
|
|
|
|
2019-02-02 17:26:55 +01:00
|
|
|
// make potato2 definitely after potato
|
|
|
|
t1 := time.Now().Add(10 * time.Second)
|
|
|
|
c.updateStat("sub/dir2/potato2", t1, 6)
|
|
|
|
|
2019-02-02 00:35:03 +01:00
|
|
|
// Check only potato removed to get below quota
|
|
|
|
removed = nil
|
|
|
|
c._purgeOverQuota(10, remove)
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"sub/dir/potato",
|
|
|
|
}, removed)
|
|
|
|
assert.Equal(t, int64(6), c.used)
|
|
|
|
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir2" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir2/potato2" isFile=true opens=0 size=6`,
|
|
|
|
}, itemAsString(c))
|
|
|
|
|
|
|
|
// Put potato back
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Open("sub/dir/potato")
|
|
|
|
p, err = c.Mkdir("sub/dir/potato")
|
2019-02-02 00:35:03 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = ioutil.WriteFile(p, []byte("hello"), 0600)
|
|
|
|
require.NoError(t, err)
|
2020-02-28 15:44:15 +01:00
|
|
|
c.Close("sub/dir/potato")
|
2019-02-02 00:35:03 +01:00
|
|
|
|
|
|
|
// Update the stats to read the total size
|
|
|
|
err = c.updateStats()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(11), c.used)
|
|
|
|
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir/potato" isFile=true opens=0 size=5`,
|
|
|
|
`name="sub/dir2" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir2/potato2" isFile=true opens=0 size=6`,
|
|
|
|
}, itemAsString(c))
|
|
|
|
|
2019-02-02 17:26:55 +01:00
|
|
|
// make potato definitely after potato2
|
|
|
|
t2 := t1.Add(20 * time.Second)
|
|
|
|
c.updateStat("sub/dir/potato", t2, 5)
|
|
|
|
|
2019-02-02 00:35:03 +01:00
|
|
|
// Check only potato2 removed to get below quota
|
|
|
|
removed = nil
|
|
|
|
c._purgeOverQuota(10, remove)
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"sub/dir2/potato2",
|
|
|
|
}, removed)
|
|
|
|
assert.Equal(t, int64(5), c.used)
|
|
|
|
c.purgeEmptyDirs()
|
|
|
|
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
`name="" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir" isFile=false opens=0 size=0`,
|
|
|
|
`name="sub/dir/potato" isFile=true opens=0 size=5`,
|
|
|
|
}, itemAsString(c))
|
|
|
|
|
|
|
|
// Now purge everything
|
|
|
|
removed = nil
|
|
|
|
c._purgeOverQuota(1, remove)
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"sub/dir/potato",
|
|
|
|
}, removed)
|
|
|
|
assert.Equal(t, int64(0), c.used)
|
|
|
|
c.purgeEmptyDirs()
|
|
|
|
|
|
|
|
assert.Equal(t, []string(nil), itemAsString(c))
|
|
|
|
|
|
|
|
// Check nothing left behind
|
|
|
|
c.clean()
|
|
|
|
assert.Equal(t, int64(0), c.used)
|
|
|
|
assert.Equal(t, []string(nil), itemAsString(c))
|
|
|
|
}
|