Skip to content

Commit c1b4d72

Browse files
committed
Make parallel downloads test more strict
We can assert now that only one thread downloaded the file, and all other threads used the cache. Signed-off-by: Nir Soffer <[email protected]>
1 parent 76d5c86 commit c1b4d72

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

pkg/downloader/downloader_test.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os/exec"
99
"path/filepath"
1010
"runtime"
11-
"slices"
1211
"strings"
1312
"testing"
1413
"time"
@@ -31,11 +30,6 @@ type downloadResult struct {
3130
// races quicker. 20 parallel downloads take about 120 milliseconds on M1 Pro.
3231
const parallelDownloads = 20
3332

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-
3933
func TestDownloadRemote(t *testing.T) {
4034
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
4135
t.Cleanup(ts.Close)
@@ -103,15 +97,10 @@ func TestDownloadRemote(t *testing.T) {
10397
results <- downloadResult{r, err}
10498
}()
10599
}
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)
115104
})
116105
})
117106
t.Run("caching-only mode", func(t *testing.T) {
@@ -146,15 +135,10 @@ func TestDownloadRemote(t *testing.T) {
146135
results <- downloadResult{r, err}
147136
}()
148137
}
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)
158142
})
159143
})
160144
t.Run("cached", func(t *testing.T) {
@@ -188,6 +172,26 @@ func TestDownloadRemote(t *testing.T) {
188172
})
189173
}
190174

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+
191195
func TestRedownloadRemote(t *testing.T) {
192196
remoteDir := t.TempDir()
193197
ts := httptest.NewServer(http.FileServer(http.Dir(remoteDir)))

0 commit comments

Comments
 (0)