2015-02-15 11:57:09 +00:00
|
|
|
package pipe_test
|
|
|
|
|
|
|
|
import (
|
2015-11-06 18:41:57 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-02-15 11:57:09 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/pipe"
|
2015-04-09 19:15:48 +00:00
|
|
|
. "github.com/restic/restic/test"
|
2015-02-15 11:57:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func isFile(fi os.FileInfo) bool {
|
|
|
|
return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
type stats struct {
|
|
|
|
dirs, files int
|
|
|
|
}
|
|
|
|
|
2015-07-19 22:13:39 +00:00
|
|
|
func acceptAll(string, os.FileInfo) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-02-15 11:57:09 +00:00
|
|
|
func statPath(path string) (stats, error) {
|
|
|
|
var s stats
|
|
|
|
|
|
|
|
// count files and directories with filepath.Walk()
|
2015-06-28 11:15:35 +00:00
|
|
|
err := filepath.Walk(TestWalkerPath, func(p string, fi os.FileInfo, err error) error {
|
2015-02-15 11:57:09 +00:00
|
|
|
if fi == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
s.dirs++
|
2015-02-15 13:44:54 +00:00
|
|
|
} else {
|
2015-02-15 11:57:09 +00:00
|
|
|
s.files++
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
const maxWorkers = 100
|
|
|
|
|
2015-03-02 13:48:47 +00:00
|
|
|
func TestPipelineWalkerWithSplit(t *testing.T) {
|
2015-06-28 11:15:35 +00:00
|
|
|
if TestWalkerPath == "" {
|
2015-03-02 13:48:47 +00:00
|
|
|
t.Skipf("walkerpath not set, skipping TestPipelineWalker")
|
2015-02-15 11:57:09 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
before, err := statPath(TestWalkerPath)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-15 11:57:09 +00:00
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
t.Logf("walking path %s with %d dirs, %d files", TestWalkerPath,
|
2015-02-15 11:57:09 +00:00
|
|
|
before.dirs, before.files)
|
|
|
|
|
2015-03-08 19:57:21 +00:00
|
|
|
// account for top level dir
|
|
|
|
before.dirs++
|
|
|
|
|
2015-02-15 11:57:09 +00:00
|
|
|
after := stats{}
|
|
|
|
m := sync.Mutex{}
|
|
|
|
|
2015-02-15 13:44:54 +00:00
|
|
|
worker := func(wg *sync.WaitGroup, done <-chan struct{}, entCh <-chan pipe.Entry, dirCh <-chan pipe.Dir) {
|
2015-02-15 11:57:09 +00:00
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e, ok := <-entCh:
|
|
|
|
if !ok {
|
|
|
|
// channel is closed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
after.files++
|
|
|
|
m.Unlock()
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
e.Result() <- true
|
2015-02-15 11:57:09 +00:00
|
|
|
|
|
|
|
case dir, ok := <-dirCh:
|
|
|
|
if !ok {
|
|
|
|
// channel is closed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for all content
|
|
|
|
for _, ch := range dir.Entries {
|
|
|
|
<-ch
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
after.dirs++
|
|
|
|
m.Unlock()
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
dir.Result() <- true
|
2015-02-15 11:57:09 +00:00
|
|
|
case <-done:
|
|
|
|
// pipeline was cancelled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 13:44:54 +00:00
|
|
|
var wg sync.WaitGroup
|
2015-02-15 11:57:09 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
entCh := make(chan pipe.Entry)
|
|
|
|
dirCh := make(chan pipe.Dir)
|
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
for i := 0; i < maxWorkers; i++ {
|
2015-02-15 11:57:09 +00:00
|
|
|
wg.Add(1)
|
2015-02-15 13:44:54 +00:00
|
|
|
go worker(&wg, done, entCh, dirCh)
|
2015-02-15 11:57:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
jobs := make(chan pipe.Job, 200)
|
2015-03-02 13:48:47 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
pipe.Split(jobs, dirCh, entCh)
|
|
|
|
close(entCh)
|
|
|
|
close(dirCh)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
resCh := make(chan pipe.Result, 1)
|
2015-11-06 18:41:57 +00:00
|
|
|
pipe.Walk([]string{TestWalkerPath}, acceptAll, done, jobs, resCh)
|
2015-03-02 13:48:47 +00:00
|
|
|
|
|
|
|
// wait for all workers to terminate
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// wait for top-level blob
|
|
|
|
<-resCh
|
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
t.Logf("walked path %s with %d dirs, %d files", TestWalkerPath,
|
2015-03-02 13:48:47 +00:00
|
|
|
after.dirs, after.files)
|
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, before == after, "stats do not match, expected %v, got %v", before, after)
|
2015-03-02 13:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPipelineWalker(t *testing.T) {
|
2015-06-28 11:15:35 +00:00
|
|
|
if TestWalkerPath == "" {
|
2015-03-02 13:48:47 +00:00
|
|
|
t.Skipf("walkerpath not set, skipping TestPipelineWalker")
|
|
|
|
}
|
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
before, err := statPath(TestWalkerPath)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-02 13:48:47 +00:00
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
t.Logf("walking path %s with %d dirs, %d files", TestWalkerPath,
|
2015-03-02 13:48:47 +00:00
|
|
|
before.dirs, before.files)
|
|
|
|
|
2015-03-08 19:57:21 +00:00
|
|
|
// account for top level dir
|
|
|
|
before.dirs++
|
|
|
|
|
2015-03-02 13:48:47 +00:00
|
|
|
after := stats{}
|
|
|
|
m := sync.Mutex{}
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
worker := func(wg *sync.WaitGroup, done <-chan struct{}, jobs <-chan pipe.Job) {
|
2015-03-02 13:48:47 +00:00
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case job, ok := <-jobs:
|
|
|
|
if !ok {
|
|
|
|
// channel is closed
|
|
|
|
return
|
|
|
|
}
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, job != nil, "job is nil")
|
2015-03-02 13:48:47 +00:00
|
|
|
|
|
|
|
switch j := job.(type) {
|
|
|
|
case pipe.Dir:
|
|
|
|
// wait for all content
|
|
|
|
for _, ch := range j.Entries {
|
|
|
|
<-ch
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
after.dirs++
|
|
|
|
m.Unlock()
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
j.Result() <- true
|
2015-03-02 13:48:47 +00:00
|
|
|
case pipe.Entry:
|
|
|
|
m.Lock()
|
|
|
|
after.files++
|
|
|
|
m.Unlock()
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
j.Result() <- true
|
2015-03-02 13:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-done:
|
|
|
|
// pipeline was cancelled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
done := make(chan struct{})
|
2015-03-07 10:53:32 +00:00
|
|
|
jobs := make(chan pipe.Job)
|
2015-03-02 13:48:47 +00:00
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
for i := 0; i < maxWorkers; i++ {
|
2015-03-02 13:48:47 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go worker(&wg, done, jobs)
|
|
|
|
}
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
resCh := make(chan pipe.Result, 1)
|
2015-11-06 18:41:57 +00:00
|
|
|
pipe.Walk([]string{TestWalkerPath}, acceptAll, done, jobs, resCh)
|
2015-02-15 11:57:09 +00:00
|
|
|
|
|
|
|
// wait for all workers to terminate
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// wait for top-level blob
|
|
|
|
<-resCh
|
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
t.Logf("walked path %s with %d dirs, %d files", TestWalkerPath,
|
2015-02-15 11:57:09 +00:00
|
|
|
after.dirs, after.files)
|
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, before == after, "stats do not match, expected %v, got %v", before, after)
|
2015-02-15 11:57:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 18:41:57 +00:00
|
|
|
func createFile(filename, data string) error {
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Write([]byte(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPipeWalkerError(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "restic-test-")
|
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
base := filepath.Base(dir)
|
|
|
|
|
|
|
|
var testjobs = []struct {
|
|
|
|
path []string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{[]string{base, "a", "file_a"}, false},
|
|
|
|
{[]string{base, "a"}, false},
|
|
|
|
{[]string{base, "b"}, true},
|
|
|
|
{[]string{base, "c", "file_c"}, false},
|
|
|
|
{[]string{base, "c"}, false},
|
|
|
|
{[]string{base}, false},
|
|
|
|
{[]string{}, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
OK(t, os.Mkdir(filepath.Join(dir, "a"), 0755))
|
|
|
|
OK(t, os.Mkdir(filepath.Join(dir, "b"), 0755))
|
|
|
|
OK(t, os.Mkdir(filepath.Join(dir, "c"), 0755))
|
|
|
|
|
|
|
|
OK(t, createFile(filepath.Join(dir, "a", "file_a"), "file a"))
|
|
|
|
OK(t, createFile(filepath.Join(dir, "b", "file_b"), "file b"))
|
|
|
|
OK(t, createFile(filepath.Join(dir, "c", "file_c"), "file c"))
|
|
|
|
|
|
|
|
OK(t, os.Chmod(filepath.Join(dir, "b"), 0))
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
ch := make(chan pipe.Job)
|
|
|
|
resCh := make(chan pipe.Result, 1)
|
|
|
|
|
|
|
|
go pipe.Walk([]string{dir}, acceptAll, done, ch, resCh)
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for job := range ch {
|
|
|
|
if i == len(testjobs) {
|
|
|
|
t.Errorf("too many jobs received")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("job %+v: %+v\n", job.Path(), job)
|
|
|
|
|
|
|
|
p := filepath.Join(testjobs[i].path...)
|
|
|
|
if p != job.Path() {
|
|
|
|
t.Errorf("job %d has wrong path: expected %q, got %q", i, p, job.Path())
|
|
|
|
}
|
|
|
|
|
|
|
|
if testjobs[i].err {
|
|
|
|
if job.Error() == nil {
|
|
|
|
t.Errorf("job %d expected error but got nil", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if job.Error() != nil {
|
|
|
|
t.Errorf("job %d expected no error but got %v", i, job.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if i != len(testjobs) {
|
|
|
|
t.Errorf("expected %d jobs, got %d", len(testjobs), i)
|
|
|
|
}
|
|
|
|
|
|
|
|
close(done)
|
|
|
|
|
|
|
|
OK(t, os.Chmod(filepath.Join(dir, "b"), 0755))
|
|
|
|
OK(t, os.RemoveAll(dir))
|
|
|
|
}
|
|
|
|
|
2015-02-15 11:57:09 +00:00
|
|
|
func BenchmarkPipelineWalker(b *testing.B) {
|
2015-06-28 11:15:35 +00:00
|
|
|
if TestWalkerPath == "" {
|
2015-03-02 13:48:47 +00:00
|
|
|
b.Skipf("walkerpath not set, skipping BenchPipelineWalker")
|
2015-02-15 11:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var max time.Duration
|
|
|
|
m := sync.Mutex{}
|
|
|
|
|
2015-02-15 13:44:54 +00:00
|
|
|
fileWorker := func(wg *sync.WaitGroup, done <-chan struct{}, ch <-chan pipe.Entry) {
|
2015-02-15 11:57:09 +00:00
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
2015-02-15 13:44:54 +00:00
|
|
|
case e, ok := <-ch:
|
2015-02-15 11:57:09 +00:00
|
|
|
if !ok {
|
|
|
|
// channel is closed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// simulate backup
|
2015-02-15 13:44:54 +00:00
|
|
|
//time.Sleep(10 * time.Millisecond)
|
2015-02-15 11:57:09 +00:00
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
e.Result() <- true
|
2015-02-15 13:44:54 +00:00
|
|
|
case <-done:
|
|
|
|
// pipeline was cancelled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-15 11:57:09 +00:00
|
|
|
|
2015-02-15 13:44:54 +00:00
|
|
|
dirWorker := func(wg *sync.WaitGroup, done <-chan struct{}, ch <-chan pipe.Dir) {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case dir, ok := <-ch:
|
2015-02-15 11:57:09 +00:00
|
|
|
if !ok {
|
|
|
|
// channel is closed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
// wait for all content
|
|
|
|
for _, ch := range dir.Entries {
|
|
|
|
<-ch
|
|
|
|
}
|
|
|
|
|
|
|
|
d := time.Since(start)
|
|
|
|
m.Lock()
|
|
|
|
if d > max {
|
|
|
|
max = d
|
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
dir.Result() <- true
|
2015-02-15 11:57:09 +00:00
|
|
|
case <-done:
|
|
|
|
// pipeline was cancelled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-02-15 13:44:54 +00:00
|
|
|
max = 0
|
2015-02-15 11:57:09 +00:00
|
|
|
done := make(chan struct{})
|
2015-02-15 13:44:54 +00:00
|
|
|
entCh := make(chan pipe.Entry, 200)
|
|
|
|
dirCh := make(chan pipe.Dir, 200)
|
2015-02-15 11:57:09 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2015-06-28 11:15:35 +00:00
|
|
|
b.Logf("starting %d workers", maxWorkers)
|
|
|
|
for i := 0; i < maxWorkers; i++ {
|
2015-02-15 13:44:54 +00:00
|
|
|
wg.Add(2)
|
|
|
|
go dirWorker(&wg, done, dirCh)
|
|
|
|
go fileWorker(&wg, done, entCh)
|
2015-02-15 11:57:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
jobs := make(chan pipe.Job, 200)
|
2015-03-02 13:48:47 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
pipe.Split(jobs, dirCh, entCh)
|
|
|
|
close(entCh)
|
|
|
|
close(dirCh)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
resCh := make(chan pipe.Result, 1)
|
2015-11-06 18:41:57 +00:00
|
|
|
pipe.Walk([]string{TestWalkerPath}, acceptAll, done, jobs, resCh)
|
2015-02-15 11:57:09 +00:00
|
|
|
|
|
|
|
// wait for all workers to terminate
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// wait for final result
|
|
|
|
<-resCh
|
|
|
|
|
2015-02-15 13:44:54 +00:00
|
|
|
b.Logf("max duration for a dir: %v", max)
|
|
|
|
}
|
2015-02-15 11:57:09 +00:00
|
|
|
}
|
2015-03-08 19:57:21 +00:00
|
|
|
|
|
|
|
func TestPipelineWalkerMultiple(t *testing.T) {
|
2015-06-28 11:15:35 +00:00
|
|
|
if TestWalkerPath == "" {
|
2015-03-08 19:57:21 +00:00
|
|
|
t.Skipf("walkerpath not set, skipping TestPipelineWalker")
|
|
|
|
}
|
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
paths, err := filepath.Glob(filepath.Join(TestWalkerPath, "*"))
|
2015-03-08 19:57:21 +00:00
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
before, err := statPath(TestWalkerPath)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-08 19:57:21 +00:00
|
|
|
|
|
|
|
t.Logf("walking paths %v with %d dirs, %d files", paths,
|
|
|
|
before.dirs, before.files)
|
|
|
|
|
|
|
|
after := stats{}
|
|
|
|
m := sync.Mutex{}
|
|
|
|
|
|
|
|
worker := func(wg *sync.WaitGroup, done <-chan struct{}, jobs <-chan pipe.Job) {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case job, ok := <-jobs:
|
|
|
|
if !ok {
|
|
|
|
// channel is closed
|
|
|
|
return
|
|
|
|
}
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, job != nil, "job is nil")
|
2015-03-08 19:57:21 +00:00
|
|
|
|
|
|
|
switch j := job.(type) {
|
|
|
|
case pipe.Dir:
|
|
|
|
// wait for all content
|
|
|
|
for _, ch := range j.Entries {
|
|
|
|
<-ch
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
after.dirs++
|
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
j.Result() <- true
|
|
|
|
case pipe.Entry:
|
|
|
|
m.Lock()
|
|
|
|
after.files++
|
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
j.Result() <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-done:
|
|
|
|
// pipeline was cancelled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
done := make(chan struct{})
|
|
|
|
jobs := make(chan pipe.Job)
|
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
for i := 0; i < maxWorkers; i++ {
|
2015-03-08 19:57:21 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go worker(&wg, done, jobs)
|
|
|
|
}
|
|
|
|
|
|
|
|
resCh := make(chan pipe.Result, 1)
|
2015-11-06 18:41:57 +00:00
|
|
|
pipe.Walk(paths, acceptAll, done, jobs, resCh)
|
2015-03-08 19:57:21 +00:00
|
|
|
|
|
|
|
// wait for all workers to terminate
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// wait for top-level blob
|
|
|
|
<-resCh
|
|
|
|
|
|
|
|
t.Logf("walked %d paths with %d dirs, %d files", len(paths), after.dirs, after.files)
|
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, before == after, "stats do not match, expected %v, got %v", before, after)
|
2015-03-08 19:57:21 +00:00
|
|
|
}
|