A listing whose pages were read before a file or folder was made in, copied into or trashed from the folder cached what it read when it finished, overwriting the change. As new files are added to a cached listing rather than dropping it, the stale listing then lasted: a file just uploaded stayed missing, and a trashed one stayed listed, until something else dropped the listing. The node cache now counts changes to the children of each folder being listed, and a listing is cached only if none happened since its first page was read. The count is kept only while a listing is in progress. The fake server gains a Hold fault, which answers a request with the response made on arrival only once released, to test this without sleeps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
185 lines
5.1 KiB
Go
185 lines
5.1 KiB
Go
package protondrive
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestNodeCacheDisabled(t *testing.T) {
|
|
c := newNodeCache(false)
|
|
require.Nil(t, c)
|
|
n := &node{ID: "a", ParentID: "root"}
|
|
c.put(n)
|
|
c.setChildren("root", []*node{n})
|
|
assert.Nil(t, c.get("a"))
|
|
_, ok := c.children("root")
|
|
assert.False(t, ok)
|
|
c.invalidate("a")
|
|
c.invalidateChildren("root")
|
|
c.clear()
|
|
}
|
|
|
|
func TestNodeCache(t *testing.T) {
|
|
c := newNodeCache(true)
|
|
dir := &node{ID: "dir", ParentID: "root", IsFolder: true}
|
|
file := &node{ID: "file", ParentID: "dir"}
|
|
|
|
assert.Nil(t, c.get("dir"))
|
|
_, ok := c.children("root")
|
|
assert.False(t, ok)
|
|
|
|
c.setChildren("root", []*node{dir})
|
|
c.setChildren("dir", []*node{file})
|
|
assert.Same(t, dir, c.get("dir"))
|
|
assert.Same(t, file, c.get("file"))
|
|
got, ok := c.children("dir")
|
|
require.True(t, ok)
|
|
assert.Equal(t, []*node{file}, got)
|
|
|
|
// An empty listing is a listing.
|
|
c.setChildren("empty", nil)
|
|
got, ok = c.children("empty")
|
|
assert.True(t, ok)
|
|
assert.Empty(t, got)
|
|
|
|
// put replaces the node in the listing.
|
|
renamed := &node{ID: "file", ParentID: "dir", Name: "new"}
|
|
c.put(renamed)
|
|
got, ok = c.children("dir")
|
|
require.True(t, ok)
|
|
assert.Equal(t, []*node{renamed}, got)
|
|
|
|
// put of a node with another parent forgets both listings.
|
|
c.setChildren("other", nil)
|
|
moved := &node{ID: "file", ParentID: "other"}
|
|
c.put(moved)
|
|
assert.Same(t, moved, c.get("file"))
|
|
_, ok = c.children("dir")
|
|
assert.False(t, ok)
|
|
_, ok = c.children("other")
|
|
assert.False(t, ok)
|
|
_, ok = c.children("root")
|
|
assert.True(t, ok)
|
|
c.setChildren("dir", []*node{renamed})
|
|
|
|
// invalidate forgets the node and its parent's listing.
|
|
c.invalidate("file")
|
|
assert.Nil(t, c.get("file"))
|
|
_, ok = c.children("dir")
|
|
assert.False(t, ok)
|
|
_, ok = c.children("root")
|
|
assert.True(t, ok)
|
|
|
|
// and a folder's own listing.
|
|
c.setChildren("dir", []*node{file})
|
|
c.invalidate("dir")
|
|
_, ok = c.children("dir")
|
|
assert.False(t, ok)
|
|
_, ok = c.children("root")
|
|
assert.False(t, ok)
|
|
assert.Same(t, file, c.get("file"))
|
|
|
|
c.setChildren("dir", []*node{file})
|
|
c.invalidateChildren("dir")
|
|
_, ok = c.children("dir")
|
|
assert.False(t, ok)
|
|
assert.Same(t, file, c.get("file"))
|
|
|
|
c.setChildren("dir", []*node{file})
|
|
c.clear()
|
|
assert.Nil(t, c.get("file"))
|
|
_, ok = c.children("dir")
|
|
assert.False(t, ok)
|
|
}
|
|
|
|
func TestNodeCacheAdd(t *testing.T) {
|
|
c := newNodeCache(true)
|
|
a := &node{ID: "a", ParentID: "dir"}
|
|
b := &node{ID: "b", ParentID: "dir"}
|
|
|
|
// Without a listing of its parent, only the node is cached.
|
|
c.add(a)
|
|
assert.Same(t, a, c.get("a"))
|
|
_, ok := c.children("dir")
|
|
assert.False(t, ok)
|
|
|
|
c.setChildren("dir", []*node{a})
|
|
c.add(b)
|
|
got, ok := c.children("dir")
|
|
require.True(t, ok)
|
|
assert.Equal(t, []*node{a, b}, got)
|
|
|
|
// A node already listed is replaced, not listed twice.
|
|
b2 := &node{ID: "b", ParentID: "dir", Name: "new"}
|
|
c.add(b2)
|
|
got, ok = c.children("dir")
|
|
require.True(t, ok)
|
|
assert.Equal(t, []*node{a, b2}, got)
|
|
|
|
newNodeCache(false).add(a)
|
|
}
|
|
|
|
func TestNodeCacheFetch(t *testing.T) {
|
|
a := &node{ID: "a", ParentID: "dir"}
|
|
listed := func(c *nodeCache) bool {
|
|
_, ok := c.children("dir")
|
|
return ok
|
|
}
|
|
|
|
c := newNodeCache(true)
|
|
token := c.startFetch("dir")
|
|
c.endFetch("dir", token, []*node{a}, true)
|
|
assert.True(t, listed(c), "unchanged")
|
|
assert.Empty(t, c.fetching)
|
|
|
|
c = newNodeCache(true)
|
|
token = c.startFetch("dir")
|
|
c.endFetch("dir", token, []*node{a}, false)
|
|
assert.False(t, listed(c), "incomplete")
|
|
assert.Empty(t, c.fetching)
|
|
|
|
for name, change := range map[string]func(c *nodeCache){
|
|
"add": func(c *nodeCache) { c.add(&node{ID: "b", ParentID: "dir"}) },
|
|
"invalidate child": func(c *nodeCache) { c.invalidate("a") },
|
|
"invalidate unknown": func(c *nodeCache) { c.invalidate("unknown") },
|
|
"invalidate folder": func(c *nodeCache) { c.invalidate("dir") },
|
|
"invalidateChildren": func(c *nodeCache) { c.invalidateChildren("dir") },
|
|
"put moved in": func(c *nodeCache) { c.put(&node{ID: "other", ParentID: "dir"}) },
|
|
"put moved out": func(c *nodeCache) { c.put(&node{ID: "a", ParentID: "elsewhere"}) },
|
|
"clear": func(c *nodeCache) { c.clear() },
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
c := newNodeCache(true)
|
|
c.put(a)
|
|
c.put(&node{ID: "other", ParentID: "elsewhere"})
|
|
first := c.startFetch("dir")
|
|
second := c.startFetch("dir")
|
|
change(c)
|
|
c.endFetch("dir", first, []*node{a}, true)
|
|
assert.False(t, listed(c), "first listing cached after a change")
|
|
// A fetch begun after the change is cached.
|
|
third := c.startFetch("dir")
|
|
c.endFetch("dir", second, []*node{a}, true)
|
|
assert.False(t, listed(c), "second listing cached after a change")
|
|
c.endFetch("dir", third, []*node{a}, true)
|
|
assert.True(t, listed(c))
|
|
assert.Empty(t, c.fetching, "fetch state kept")
|
|
})
|
|
}
|
|
|
|
// Changes elsewhere don't matter.
|
|
c = newNodeCache(true)
|
|
c.put(&node{ID: "x", ParentID: "other"})
|
|
token = c.startFetch("dir")
|
|
c.add(&node{ID: "y", ParentID: "other"})
|
|
c.invalidate("x")
|
|
c.invalidateChildren("other")
|
|
c.endFetch("dir", token, []*node{a}, true)
|
|
assert.True(t, listed(c))
|
|
|
|
var disabled *nodeCache
|
|
disabled.endFetch("dir", disabled.startFetch("dir"), nil, true)
|
|
}
|