Skip to content

Commit

Permalink
Fixes race condition in test server
Browse files Browse the repository at this point in the history
  • Loading branch information
cuducos committed Aug 17, 2024
1 parent 6253d41 commit c2d6cad
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http/httptest"
"os"
"path"
"sync/atomic"
"testing"
)

Expand Down Expand Up @@ -51,11 +52,11 @@ func httpTestServer(t *testing.T, cs []string) *httptest.Server {
if len(cs) == 0 {
panic("no content provided to the test server")
}
var c int
var c uint32
return httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
idx := c % len(cs)
c += 1
idx := int(atomic.LoadUint32(&c)) % len(cs)
atomic.AddUint32(&c, 1)
if r.Method == http.MethodHead {
f, s := loadFixture(t, cs[idx])
defer f.Close()
Expand Down

0 comments on commit c2d6cad

Please sign in to comment.