use atomics in the Chrome test HTTP handlers

This fixes a race condition in these tests.
This commit is contained in:
Marten Seemann
2017-08-23 10:03:44 +07:00
parent 7d446253c4
commit 5751b599ee

View File

@@ -35,9 +35,9 @@ const (
)
var (
nFilesUploaded int32
testEndpointCalled bool
doneCalled bool
nFilesUploaded int32 // should be used atomically
testEndpointCalled utils.AtomicBool
doneCalled utils.AtomicBool
)
func TestChrome(t *testing.T) {
@@ -54,7 +54,7 @@ func init() {
response = strings.Replace(response, "NUM", r.URL.Query().Get("num"), -1)
_, err := io.WriteString(w, response)
Expect(err).NotTo(HaveOccurred())
testEndpointCalled = true
testEndpointCalled.Set(true)
})
// Requires the len & num GET parameters, e.g. /downloadtest?len=100&num=1
@@ -65,7 +65,7 @@ func init() {
response = strings.Replace(response, "NUM", r.URL.Query().Get("num"), -1)
_, err := io.WriteString(w, response)
Expect(err).NotTo(HaveOccurred())
testEndpointCalled = true
testEndpointCalled.Set(true)
})
http.HandleFunc("/uploadhandler", func(w http.ResponseWriter, r *http.Request) {
@@ -84,7 +84,7 @@ func init() {
})
http.HandleFunc("/done", func(w http.ResponseWriter, r *http.Request) {
doneCalled = true
doneCalled.Set(true)
})
}
@@ -93,9 +93,9 @@ var _ = JustBeforeEach(testserver.StartQuicServer)
var _ = AfterEach(func() {
testserver.StopQuicServer()
nFilesUploaded = 0
doneCalled = false
testEndpointCalled = false
atomic.StoreInt32(&nFilesUploaded, 0)
doneCalled.Set(false)
testEndpointCalled.Set(false)
})
func getChromePath() string {
@@ -149,11 +149,11 @@ func chromeTestImpl(version protocol.VersionNumber, url string, blockUntilDone f
const pollDuration = 10 * time.Second
for i := 0; i < int(pollDuration/pollInterval); i++ {
time.Sleep(pollInterval)
if testEndpointCalled {
if testEndpointCalled.Get() {
break
}
}
if !testEndpointCalled {
if !testEndpointCalled.Get() {
return false
}
blockUntilDone()
@@ -161,7 +161,7 @@ func chromeTestImpl(version protocol.VersionNumber, url string, blockUntilDone f
}
func waitForDone() {
Eventually(func() bool { return doneCalled }, 60).Should(BeTrue())
Eventually(func() bool { return doneCalled.Get() }, 60).Should(BeTrue())
}
func waitForNUploaded(expected int) func() {