Add coverage for bad hostnames and cancellation

The case where users passed bad hostnames to create new influx clients
was untested as well as the cancellation logic.
pull/10616/head
Tim Raymond 2016-09-15 14:01:53 -04:00
parent f6ac18b692
commit 05dfabc06b
2 changed files with 39 additions and 3 deletions

View File

@ -45,6 +45,4 @@ func (c *Client) Query(ctx context.Context, query mrfusion.Query) (mrfusion.Resp
case <-ctx.Done():
return nil, nil
}
return nil, nil
}

View File

@ -4,13 +4,22 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/influxdata/mrfusion"
"github.com/influxdata/mrfusion/influx"
"golang.org/x/net/context"
)
func Test_MakesRequestsToQueryEndpoint(t *testing.T) {
func Test_Influx_RejectsBadHostnames(t *testing.T) {
t.Parallel()
_, err := influx.NewClient("wibble")
if err == nil {
t.Error("Expected an error for invalid influx host")
}
}
func Test_Influx_MakesRequestsToQueryEndpoint(t *testing.T) {
t.Parallel()
called := false
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
@ -38,3 +47,32 @@ func Test_MakesRequestsToQueryEndpoint(t *testing.T) {
t.Error("Expected http request to Influx but there was none")
}
}
func Test_Influx_CancelsInFlightRequests(t *testing.T) {
t.Parallel()
started := false
finished := false
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
started = true
time.Sleep(2 * time.Second)
finished = true
}))
defer func() {
ts.CloseClientConnections()
ts.Close()
}()
series, _ := influx.NewClient(ts.URL)
ctx, cancel := context.WithCancel(context.Background())
go func() {
_, _ = series.Query(ctx, "show databases")
}()
cancel()
if started != true && finished != false {
t.Errorf("Expected cancellation during request processing. Started: %t. Finished: %t", started, finished)
}
}