274 lines
7.8 KiB
Go
274 lines
7.8 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.invalidateTree("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)
|
|
}
|
|
|
|
func TestInvalidateTree(t *testing.T) {
|
|
c := newNodeCache(true)
|
|
c.setChildren("root", []*node{
|
|
{ID: "dir", ParentID: "root", IsFolder: true},
|
|
{ID: "keep", ParentID: "root"},
|
|
})
|
|
c.setChildren("dir", []*node{
|
|
{ID: "sub", ParentID: "dir", IsFolder: true},
|
|
{ID: "file", ParentID: "dir"},
|
|
})
|
|
c.setChildren("sub", []*node{{ID: "deep", ParentID: "sub"}})
|
|
// A descendant cached without its parent's listing.
|
|
c.put(&node{ID: "lone", ParentID: "sub"})
|
|
c.setChildren("elsewhere", []*node{{ID: "x", ParentID: "elsewhere"}})
|
|
|
|
dirFetch := c.startFetch("dir")
|
|
subFetch := c.startFetch("sub")
|
|
otherFetch := c.startFetch("elsewhere")
|
|
c.invalidateTree("dir")
|
|
|
|
for _, id := range []string{"dir", "sub", "file", "deep", "lone"} {
|
|
assert.Nil(t, c.get(id), id)
|
|
}
|
|
for _, id := range []string{"root", "dir", "sub"} {
|
|
_, ok := c.listings[id]
|
|
assert.False(t, ok, "listing %s kept", id)
|
|
}
|
|
assert.NotNil(t, c.get("keep"))
|
|
assert.NotNil(t, c.get("x"))
|
|
_, ok := c.children("elsewhere")
|
|
assert.True(t, ok)
|
|
|
|
// Listings of the tree in flight aren't cached; others are.
|
|
c.endFetch("dir", dirFetch, []*node{{ID: "file", ParentID: "dir"}}, true)
|
|
c.endFetch("sub", subFetch, []*node{{ID: "deep", ParentID: "sub"}}, true)
|
|
c.endFetch("elsewhere", otherFetch, []*node{{ID: "x", ParentID: "elsewhere"}}, true)
|
|
_, ok = c.children("dir")
|
|
assert.False(t, ok)
|
|
_, ok = c.children("sub")
|
|
assert.False(t, ok)
|
|
_, ok = c.children("elsewhere")
|
|
assert.True(t, ok)
|
|
assert.Empty(t, c.fetching)
|
|
|
|
// An unknown folder: its cached children go, and every listing in
|
|
// flight may hold it.
|
|
c = newNodeCache(true)
|
|
c.setChildren("ghost", []*node{{ID: "child", ParentID: "ghost"}})
|
|
c.put(&node{ID: "y", ParentID: "other"})
|
|
token := c.startFetch("other")
|
|
c.invalidateTree("ghost")
|
|
assert.Nil(t, c.get("child"))
|
|
assert.NotNil(t, c.get("y"))
|
|
c.endFetch("other", token, []*node{{ID: "y", ParentID: "other"}}, true)
|
|
_, ok = c.children("other")
|
|
assert.False(t, ok)
|
|
|
|
// A cycle, as a hostile server could report, ends.
|
|
c = newNodeCache(true)
|
|
c.put(&node{ID: "a", ParentID: "b", IsFolder: true})
|
|
c.put(&node{ID: "b", ParentID: "a", IsFolder: true})
|
|
c.invalidateTree("a")
|
|
assert.True(t, c.empty())
|
|
}
|
|
|
|
func TestNodeCacheEmpty(t *testing.T) {
|
|
var disabled *nodeCache
|
|
assert.True(t, disabled.empty())
|
|
|
|
c := newNodeCache(true)
|
|
assert.True(t, c.empty())
|
|
c.put(&node{ID: "a", ParentID: "root"})
|
|
assert.False(t, c.empty())
|
|
c.invalidate("a")
|
|
assert.True(t, c.empty())
|
|
|
|
// An empty listing is something cached.
|
|
c.setChildren("root", nil)
|
|
assert.False(t, c.empty())
|
|
c.clear()
|
|
assert.True(t, c.empty())
|
|
|
|
// Listings in flight aren't.
|
|
token := c.startFetch("root")
|
|
assert.True(t, c.empty())
|
|
c.endFetch("root", token, nil, false)
|
|
}
|