-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogress_test.go
More file actions
165 lines (151 loc) · 3.64 KB
/
Copy pathprogress_test.go
File metadata and controls
165 lines (151 loc) · 3.64 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"time"
)
type eventCapture struct {
mu sync.Mutex
events []string
progress []ScanProgress
done *ScanSummary
}
func (c *eventCapture) capture(event string, data interface{}) {
c.mu.Lock()
defer c.mu.Unlock()
c.events = append(c.events, event)
switch event {
case "scan:progress":
c.progress = append(c.progress, data.(ScanProgress))
case "scan:done":
s := data.(ScanSummary)
c.done = &s
}
}
func (c *eventCapture) snapshot() ([]string, []ScanProgress, *ScanSummary) {
c.mu.Lock()
defer c.mu.Unlock()
return c.events, c.progress, c.done
}
// buildBigTree creates a tree of `dirs` directories each holding one 3-byte file.
func buildBigTree(t *testing.T, dirs int) string {
t.Helper()
root := t.TempDir()
for i := 0; i < dirs; i++ {
p := filepath.Join(root, fmt.Sprintf("d%03d", i%100), fmt.Sprintf("s%03d", i/100))
if err := os.MkdirAll(p, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(p, "f.bin"), []byte{1, 2, 3}, 0o644); err != nil {
t.Fatal(err)
}
}
return root
}
func TestScanEmitsProgressEvents(t *testing.T) {
root := buildBigTree(t, 260)
cap := &eventCapture{}
a := &App{}
a.scans.emit = cap.capture
if err := a.StartScan(root, 10); err != nil {
t.Fatal(err)
}
waitScan(a, t)
events, progress, done := cap.snapshot()
if !contains(events, "scan:progress") || done == nil {
t.Fatalf("missing progress or done: events=%v done=%v", events, done)
}
if done.OK != true || done.Cancelled {
t.Fatalf("unexpected done: %+v", done)
}
if done.Bytes != 260*3 {
t.Fatalf("done.Bytes = %d, want %d", done.Bytes, 260*3)
}
// Phase sequence: enumerate first, then scan events, monotonic DirsDone.
sawScan := false
var lastDone int
var enumTotal int
for _, p := range progress {
if p.Phase == "enumerate" {
if sawScan {
t.Fatal("enumerate progress emitted after scan phase")
}
if p.DirsTotal < 260 {
t.Fatalf("enumerate DirsTotal = %d, want >= 260", p.DirsTotal)
}
enumTotal = p.DirsTotal
continue
}
if p.Phase == "scan" {
sawScan = true
if p.DirsTotal != enumTotal {
t.Fatalf("scan DirsTotal %d != enumerate %d", p.DirsTotal, enumTotal)
}
if p.DirsDone < lastDone {
t.Fatal("DirsDone not monotonic")
}
lastDone = p.DirsDone
}
}
if !sawScan {
t.Fatal("no scan-phase progress events")
}
if lastDone != enumTotal {
t.Fatalf("final DirsDone = %d, want %d", lastDone, enumTotal)
}
}
func TestCancelScan(t *testing.T) {
root := buildBigTree(t, 3000)
cap := &eventCapture{}
a := &App{}
a.scans.emit = cap.capture
if err := a.StartScan(root, 10); err != nil {
t.Fatal(err)
}
// Wait until the scan-phase progress begins (enumerate done, scan started),
// then cancel so we interrupt an in-flight scan.
deadline := time.Now().Add(30 * time.Second)
for {
_, progress, _ := cap.snapshot()
started := false
for _, p := range progress {
if p.Phase == "scan" && p.DirsDone > 0 {
started = true
break
}
}
if started {
break
}
if !a.IsScanning() {
t.Fatal("scan finished before we could cancel")
}
if time.Now().After(deadline) {
t.Fatal("scan-phase progress never arrived")
}
time.Sleep(5 * time.Millisecond)
}
a.CancelScan()
waitScan(a, t)
_, _, done := cap.snapshot()
if done == nil {
t.Fatal("no done event after cancel")
}
if !done.Cancelled {
t.Fatalf("expected cancelled scan, got %+v", done)
}
if done.Bytes >= 3000*3 {
t.Fatalf("cancelled scan reported full byte count: %+v", done)
}
}
func contains(list []string, s string) bool {
for _, v := range list {
if v == s {
return true
}
}
return false
}