8
8
"os/exec"
9
9
"path/filepath"
10
10
"runtime"
11
- "slices"
12
11
"strings"
13
12
"testing"
14
13
"time"
@@ -31,11 +30,6 @@ type downloadResult struct {
31
30
// races quicker. 20 parallel downloads take about 120 milliseconds on M1 Pro.
32
31
const parallelDownloads = 20
33
32
34
- // When downloading in parallel usually all downloads completed with
35
- // StatusDownload, but some may be delayed and find the data file when they
36
- // start. Can be reproduced locally using 100 parallel downloads.
37
- var parallelStatus = []Status {StatusDownloaded , StatusUsedCache }
38
-
39
33
func TestDownloadRemote (t * testing.T ) {
40
34
ts := httptest .NewServer (http .FileServer (http .Dir ("testdata" )))
41
35
t .Cleanup (ts .Close )
@@ -103,15 +97,10 @@ func TestDownloadRemote(t *testing.T) {
103
97
results <- downloadResult {r , err }
104
98
}()
105
99
}
106
- // We must process all results before cleanup.
107
- for i := 0 ; i < parallelDownloads ; i ++ {
108
- result := <- results
109
- if result .err != nil {
110
- t .Errorf ("Download failed: %s" , result .err )
111
- } else if ! slices .Contains (parallelStatus , result .r .Status ) {
112
- t .Errorf ("Expected download status %s, got %s" , parallelStatus , result .r .Status )
113
- }
114
- }
100
+ // Only one thread should download, the rest should use the cache.
101
+ downloaded , cached := countResults (t , results )
102
+ assert .Equal (t , downloaded , 1 )
103
+ assert .Equal (t , cached , parallelDownloads - 1 )
115
104
})
116
105
})
117
106
t .Run ("caching-only mode" , func (t * testing.T ) {
@@ -146,15 +135,10 @@ func TestDownloadRemote(t *testing.T) {
146
135
results <- downloadResult {r , err }
147
136
}()
148
137
}
149
- // We must process all results before cleanup.
150
- for i := 0 ; i < parallelDownloads ; i ++ {
151
- result := <- results
152
- if result .err != nil {
153
- t .Errorf ("Download failed: %s" , result .err )
154
- } else if ! slices .Contains (parallelStatus , result .r .Status ) {
155
- t .Errorf ("Expected download status %s, got %s" , parallelStatus , result .r .Status )
156
- }
157
- }
138
+ // Only one thread should download, the rest should use the cache.
139
+ downloaded , cached := countResults (t , results )
140
+ assert .Equal (t , downloaded , 1 )
141
+ assert .Equal (t , cached , parallelDownloads - 1 )
158
142
})
159
143
})
160
144
t .Run ("cached" , func (t * testing.T ) {
@@ -188,6 +172,26 @@ func TestDownloadRemote(t *testing.T) {
188
172
})
189
173
}
190
174
175
+ func countResults (t * testing.T , results chan downloadResult ) (downloaded , cached int ) {
176
+ t .Helper ()
177
+ for i := 0 ; i < parallelDownloads ; i ++ {
178
+ result := <- results
179
+ if result .err != nil {
180
+ t .Errorf ("Download failed: %s" , result .err )
181
+ } else {
182
+ switch result .r .Status {
183
+ case StatusDownloaded :
184
+ downloaded ++
185
+ case StatusUsedCache :
186
+ cached ++
187
+ default :
188
+ t .Errorf ("Unexpected download status %q" , result .r .Status )
189
+ }
190
+ }
191
+ }
192
+ return downloaded , cached
193
+ }
194
+
191
195
func TestRedownloadRemote (t * testing.T ) {
192
196
remoteDir := t .TempDir ()
193
197
ts := httptest .NewServer (http .FileServer (http .Dir (remoteDir )))
0 commit comments