File: progress_test.go

package info (click to toggle)
restic 0.18.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,824 kB
  • sloc: sh: 3,704; makefile: 50; python: 34
file content (77 lines) | stat: -rw-r--r-- 1,795 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package backup

import (
	"sync"
	"testing"
	"time"

	"github.com/restic/restic/internal/archiver"
	"github.com/restic/restic/internal/restic"
)

type mockPrinter struct {
	sync.Mutex
	dirUnchanged, fileNew bool
	id                    restic.ID
}

func (p *mockPrinter) Update(_, _ Counter, _ uint, _ map[string]struct{}, _ time.Time, _ uint64) {
}
func (p *mockPrinter) Error(_ string, err error) error        { return err }
func (p *mockPrinter) ScannerError(_ string, err error) error { return err }

func (p *mockPrinter) CompleteItem(messageType string, _ string, _ archiver.ItemStats, _ time.Duration) {
	p.Lock()
	defer p.Unlock()

	switch messageType {
	case "dir unchanged":
		p.dirUnchanged = true
	case "file new":
		p.fileNew = true
	}
}

func (p *mockPrinter) ReportTotal(_ time.Time, _ archiver.ScanStats) {}
func (p *mockPrinter) Finish(id restic.ID, _ *archiver.Summary, _ bool) {
	p.Lock()
	defer p.Unlock()

	p.id = id
}

func (p *mockPrinter) Reset() {}

func (p *mockPrinter) P(_ string, _ ...interface{}) {}
func (p *mockPrinter) V(_ string, _ ...interface{}) {}

func TestProgress(t *testing.T) {
	t.Parallel()

	prnt := &mockPrinter{}
	prog := NewProgress(prnt, time.Millisecond)

	prog.StartFile("foo")
	prog.CompleteBlob(1024)

	// "dir unchanged"
	node := restic.Node{Type: restic.NodeTypeDir}
	prog.CompleteItem("foo", &node, &node, archiver.ItemStats{}, 0)
	// "file new"
	node.Type = restic.NodeTypeFile
	prog.CompleteItem("foo", nil, &node, archiver.ItemStats{}, 0)

	time.Sleep(10 * time.Millisecond)
	id := restic.NewRandomID()
	prog.Finish(id, nil, false)

	if !prnt.dirUnchanged {
		t.Error(`"dir unchanged" event not seen`)
	}
	if !prnt.fileNew {
		t.Error(`"file new" event not seen`)
	}
	if prnt.id != id {
		t.Errorf("id not stored (has %v)", prnt.id)
	}
}