-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
progress_test.go
149 lines (140 loc) · 4.08 KB
/
progress_test.go
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
package chunk
import (
"os"
"path/filepath"
"sync"
"testing"
)
func TestProgress_FromScratch(t *testing.T) {
tmp := t.TempDir()
name := filepath.Join(tmp, "chunk.zip")
p, err := newProgress(name, tmp, "https://test.etc/chunk.zip", 5, 3, false)
if err != nil {
t.Errorf("expected no error creating the progress, got %s", err)
}
if err := p.done(1, 3); err != nil {
t.Errorf("expected no error marking chunk as done, got %s", err)
}
for i := 0; i < 3; i++ {
got, err := p.shouldDownload(i)
if err != nil {
t.Errorf("expected no error checking if chunk %d should be downloaded, got %s", i, err)
}
if i == 1 {
if got {
t.Errorf("expected chunk %d to be downloaded", i)
}
} else {
if !got {
t.Errorf("expected chunk %d not to be downloaded", i)
}
}
}
if err := p.close(); err != nil {
t.Errorf("expected no errors saving the progress file, got %s", err)
}
if _, err := os.ReadFile(p.path); err != nil {
t.Errorf("expected no errors reading the progress file, got %s", err)
}
}
func TestProgress_ParallelComplete(t *testing.T) {
tmp := t.TempDir()
name := filepath.Join(tmp, "chunk.zip")
p, err := newProgress(name, tmp, "https://test.etc/chunk.zip", 5, 2048, false)
if err != nil {
t.Errorf("expected no error creating the progress, got %s", err)
}
var wg sync.WaitGroup
errs := make(chan error)
for i := 0; i < 2048; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
errs <- p.done(i, 2048)
}(i)
}
for i := 0; i < 2048; i++ {
err := <-errs
if err != nil {
t.Errorf("expected no error marking chunk as done, got %s", err)
}
}
close(errs)
if err := p.close(); err != nil {
t.Errorf("expected no errors removing the progress file, got %s", err)
}
if _, err := os.ReadFile(p.path); !os.IsNotExist(err) {
t.Errorf("expected progress file not to exist, rediong it returned error %s", err)
}
}
func TestProgress_FromFile(t *testing.T) {
tmp := t.TempDir()
name := filepath.Join(tmp, "chunk.zip")
old, err := newProgress(name, tmp, "https://test.etc/chunk.zip", 5, 3, false)
if err != nil {
t.Errorf("expected no error creating the old progress, got %s", err)
}
old.done(1, 3)
old.close()
p, err := newProgress(name, tmp, "https://test.etc/chunk.zip", 5, 3, false)
if err != nil {
t.Errorf("expected no error creating the progress, got %s", err)
}
for i := 0; i < 3; i++ {
got, err := p.shouldDownload(i)
if err != nil {
t.Errorf("expected no error checking if chunk %d should be downloaded, got %s", i, err)
}
if i == 1 {
if got {
t.Errorf("expected chunk %d to be downloaded", i)
}
} else {
if !got {
t.Errorf("expected chunk %d not to be downloaded", i)
}
}
}
if err := p.close(); err != nil {
t.Errorf("expected no errors saving the progress file, got %s", err)
}
if _, err := os.ReadFile(p.path); err != nil {
t.Errorf("expected no errors reading the progress file, got %s", err)
}
}
func TestProgress_FromFileWithInvalidChunkSize(t *testing.T) {
tmp := t.TempDir()
name := filepath.Join(tmp, "chunk.zip")
old, err := newProgress(name, tmp, "https://test.etc/chunk.zip", 5, 3, false)
if err != nil {
t.Errorf("expected no error creating the old progress, got %s", err)
}
old.done(1, 3)
old.close()
if _, err := newProgress(name, tmp, "https://test.etc/chunk.zip", 10, 3, false); err == nil {
t.Error("expected error creating the progress with different chunk size")
}
}
func TestProgress_FromFileWithRestart(t *testing.T) {
tmp := t.TempDir()
name := filepath.Join(tmp, "chunk.zip")
old, err := newProgress(name, tmp, "https://test.etc/chunk.zip", 5, 3, false)
if err != nil {
t.Errorf("expected no error creating the old progress, got %s", err)
}
old.done(1, 3)
old.close()
p, err := newProgress(name, tmp, "https://test.etc/chunk.zip", 10, 3, true)
if err != nil {
t.Errorf("expected no error creating the progress, got %s", err)
}
for i := 0; i < 3; i++ {
got, err := p.shouldDownload(i)
if err != nil {
t.Errorf("expected no error checking if chunk %d should be downloaded, got %s", i, err)
}
if !got {
t.Errorf("expected chunk %d not to be downloaded", i)
}
}
}