2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-23 13:17:42 +00:00
restic/walk_test.go

86 lines
1.9 KiB
Go
Raw Normal View History

2015-03-02 13:48:47 +00:00
package restic_test
import (
"flag"
"path/filepath"
"testing"
"github.com/restic/restic"
"github.com/restic/restic/pipe"
2015-04-09 19:15:48 +00:00
. "github.com/restic/restic/test"
2015-03-02 13:48:47 +00:00
)
var testWalkDirectory = flag.String("test.walkdir", ".", "test walking a directory (globbing pattern, default: .)")
func TestWalkTree(t *testing.T) {
dirs, err := filepath.Glob(*testWalkDirectory)
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-03-02 13:48:47 +00:00
server := setupBackend(t)
defer teardownBackend(t, server)
key := setupKey(t, server, "geheim")
server.SetKey(key)
2015-03-02 13:48:47 +00:00
// archive a few files
arch, err := restic.NewArchiver(server)
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-03-02 13:48:47 +00:00
sn, _, err := arch.Snapshot(nil, dirs, nil)
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-03-02 13:48:47 +00:00
// start benchmark
// t.ResetTimer()
// for i := 0; i < t.N; i++ {
done := make(chan struct{})
// start tree walker
treeJobs := make(chan restic.WalkTreeJob)
go restic.WalkTree(server, sn.Tree, done, treeJobs)
2015-03-02 13:48:47 +00:00
// start filesystem walker
2015-03-07 10:53:32 +00:00
fsJobs := make(chan pipe.Job)
resCh := make(chan pipe.Result, 1)
go pipe.Walk(dirs, done, fsJobs, resCh)
2015-03-02 13:48:47 +00:00
for {
// receive fs job
fsJob, fsChOpen := <-fsJobs
2015-04-09 19:15:48 +00:00
Assert(t, !fsChOpen || fsJob != nil,
2015-03-02 13:48:47 +00:00
"received nil job from filesystem: %v %v", fsJob, fsChOpen)
var path string
fsEntries := 1
switch j := fsJob.(type) {
case pipe.Dir:
2015-03-07 10:53:32 +00:00
path = j.Path()
2015-03-02 13:48:47 +00:00
fsEntries = len(j.Entries)
case pipe.Entry:
2015-03-07 10:53:32 +00:00
path = j.Path()
2015-03-02 13:48:47 +00:00
}
// receive tree job
treeJob, treeChOpen := <-treeJobs
treeEntries := 1
if treeJob.Tree != nil {
treeEntries = len(treeJob.Tree.Nodes)
}
2015-04-09 19:15:48 +00:00
Assert(t, fsChOpen == treeChOpen,
2015-03-02 13:48:47 +00:00
"one channel closed too early: fsChOpen %v, treeChOpen %v",
fsChOpen, treeChOpen)
if !fsChOpen || !treeChOpen {
break
}
2015-04-09 19:15:48 +00:00
Assert(t, filepath.Base(path) == filepath.Base(treeJob.Path),
2015-03-02 13:48:47 +00:00
"paths do not match: %q != %q", filepath.Base(path), filepath.Base(treeJob.Path))
2015-04-09 19:15:48 +00:00
Assert(t, fsEntries == treeEntries,
2015-03-02 13:48:47 +00:00
"wrong number of entries: %v != %v", fsEntries, treeEntries)
}
// }
}