From c2d6cadabe470919c13f540a95e3b4e9586dfb80 Mon Sep 17 00:00:00 2001 From: Eduardo Cuducos <4732915+cuducos@users.noreply.github.com> Date: Sat, 17 Aug 2024 14:04:51 -0400 Subject: [PATCH] Fixes race condition in test server --- download/download_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/download/download_test.go b/download/download_test.go index 3c5b4d9..8a9354f 100644 --- a/download/download_test.go +++ b/download/download_test.go @@ -6,6 +6,7 @@ import ( "net/http/httptest" "os" "path" + "sync/atomic" "testing" ) @@ -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()