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
|
package model
import (
"errors"
"sync"
"testing"
)
func TestScanProgress_Start(t *testing.T) {
var p ScanProgress
p.Start(3)
cp := p.Copy()
if !cp.Running {
t.Error("expected Running to be true")
}
if cp.SetsTotal != 3 {
t.Errorf("SetsTotal = %d, want 3", cp.SetsTotal)
}
if cp.SetsDone != 0 {
t.Errorf("SetsDone = %d, want 0", cp.SetsDone)
}
if cp.FilesTotal != 0 {
t.Errorf("FilesTotal = %d, want 0", cp.FilesTotal)
}
if cp.FilesDone != 0 {
t.Errorf("FilesDone = %d, want 0", cp.FilesDone)
}
if cp.LastError != "" {
t.Errorf("LastError = %q, want empty", cp.LastError)
}
}
func TestScanProgress_SetCurrentSet(t *testing.T) {
var p ScanProgress
p.Start(1)
p.SetCurrentSet("movies")
cp := p.Copy()
if cp.CurrentSet != "movies" {
t.Errorf("CurrentSet = %q, want movies", cp.CurrentSet)
}
}
func TestScanProgress_SetFilesTotal(t *testing.T) {
var p ScanProgress
p.Start(1)
p.SetFilesTotal(42)
cp := p.Copy()
if cp.FilesTotal != 42 {
t.Errorf("FilesTotal = %d, want 42", cp.FilesTotal)
}
}
func TestScanProgress_AddFilesTotal(t *testing.T) {
var p ScanProgress
p.Start(2)
p.AddFilesTotal(10)
p.AddFilesTotal(15)
cp := p.Copy()
if cp.FilesTotal != 25 {
t.Errorf("FilesTotal = %d, want 25", cp.FilesTotal)
}
}
func TestScanProgress_IncrementFile(t *testing.T) {
var p ScanProgress
p.Start(1)
p.IncrementFile()
cp := p.Copy()
if cp.FilesDone != 1 {
t.Errorf("FilesDone = %d, want 1", cp.FilesDone)
}
}
func TestScanProgress_IncrementSet(t *testing.T) {
var p ScanProgress
p.Start(2)
p.IncrementSet()
cp := p.Copy()
if cp.SetsDone != 1 {
t.Errorf("SetsDone = %d, want 1", cp.SetsDone)
}
}
func TestScanProgress_Done(t *testing.T) {
var p ScanProgress
p.Start(1)
p.Done(nil)
cp := p.Copy()
if cp.Running {
t.Error("expected Running to be false")
}
if cp.CurrentSet != "" {
t.Errorf("CurrentSet = %q, want empty", cp.CurrentSet)
}
if cp.LastError != "" {
t.Errorf("LastError = %q, want empty", cp.LastError)
}
}
func TestScanProgress_Done_WithError(t *testing.T) {
var p ScanProgress
p.Start(1)
p.Done(errors.New("scan failed"))
cp := p.Copy()
if cp.LastError != "scan failed" {
t.Errorf("LastError = %q, want \"scan failed\"", cp.LastError)
}
}
func TestScanProgress_Copy_Isolation(t *testing.T) {
var p ScanProgress
p.Start(1)
cp1 := p.Copy()
p.IncrementSet()
cp2 := p.Copy()
if cp1.SetsDone != 0 {
t.Errorf("cp1.SetsDone = %d, want 0", cp1.SetsDone)
}
if cp2.SetsDone != 1 {
t.Errorf("cp2.SetsDone = %d, want 1", cp2.SetsDone)
}
}
func TestScanProgress_ConcurrentAccess(t *testing.T) {
var p ScanProgress
p.Start(2)
p.SetFilesTotal(100)
start := make(chan struct{})
done := make(chan struct{})
var copies sync.WaitGroup
go func() {
<-start
for i := 0; i < 50; i++ {
p.IncrementFile()
}
close(done)
}()
for i := 0; i < 50; i++ {
copies.Add(1)
go func() {
defer copies.Done()
<-start
_ = p.Copy()
}()
}
close(start)
copies.Wait()
<-done
cp := p.Copy()
if cp.FilesDone != 50 {
t.Errorf("FilesDone = %d, want 50", cp.FilesDone)
}
}
|