Update test to use channels to prevent race.

Signed-off-by: Nathaniel Cook <nvcook42@gmail.com>
pull/10616/head
Chris Goller 2016-09-22 14:15:46 -07:00 committed by Nathaniel Cook
parent 0f9cb6aef7
commit c680cf8596
2 changed files with 25 additions and 7 deletions

View File

@ -54,6 +54,7 @@ test: jstest gotest
gotest: gotest:
go test ./... go test ./...
go test -race ./...
jstest: jstest:
cd ui && npm test cd ui && npm test

View File

@ -46,12 +46,12 @@ func Test_Influx_MakesRequestsToQueryEndpoint(t *testing.T) {
func Test_Influx_CancelsInFlightRequests(t *testing.T) { func Test_Influx_CancelsInFlightRequests(t *testing.T) {
t.Parallel() t.Parallel()
started := false started := make(chan bool, 1)
finished := false finished := make(chan bool, 1)
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
started = true started <- true
time.Sleep(2 * time.Second) time.Sleep(20 * time.Millisecond)
finished = true finished <- true
})) }))
defer func() { defer func() {
ts.CloseClientConnections() ts.CloseClientConnections()
@ -71,10 +71,27 @@ func Test_Influx_CancelsInFlightRequests(t *testing.T) {
errs <- err errs <- err
}() }()
timer := time.NewTimer(10 * time.Second)
defer timer.Stop()
select {
case s := <-started:
if !s {
t.Errorf("Expected cancellation during request processing. Started: %t", s)
}
case <-timer.C:
t.Fatalf("Expected server to finish")
}
cancel() cancel()
if started != true && finished != false { select {
t.Errorf("Expected cancellation during request processing. Started: %t. Finished: %t", started, finished) case f := <-finished:
if !f {
t.Errorf("Expected cancellation during request processing. Finished: %t", f)
}
case <-timer.C:
t.Fatalf("Expected server to finish")
} }
err := <-errs err := <-errs