Add timeout error for influx

This adds a type to be returned when requests to InfluxDB were cancelled
or timed out.
pull/53/head
Tim Raymond 2016-09-15 15:08:25 -04:00
parent a8291c602c
commit d3da914994
3 changed files with 22 additions and 2 deletions

13
influx/errors.go Normal file
View File

@ -0,0 +1,13 @@
package influx
const (
ErrTimedOut string = "Timeout during request to InfluxDB"
)
// TimeoutError is returned when requests to InfluxDB are cancelled or time
// out.
type TimeoutError struct{}
func (te TimeoutError) Error() string {
return ErrTimedOut
}

View File

@ -43,6 +43,6 @@ func (c *Client) Query(ctx context.Context, query mrfusion.Query) (mrfusion.Resp
case resp := <-resps:
return resp, resp.err
case <-ctx.Done():
return nil, nil
return nil, TimeoutError{}
}
}

View File

@ -66,8 +66,10 @@ func Test_Influx_CancelsInFlightRequests(t *testing.T) {
series, _ := influx.NewClient(ts.URL)
ctx, cancel := context.WithCancel(context.Background())
errs := make(chan (error))
go func() {
_, _ = series.Query(ctx, "show databases")
_, err := series.Query(ctx, "show databases")
errs <- err
}()
cancel()
@ -75,4 +77,9 @@ func Test_Influx_CancelsInFlightRequests(t *testing.T) {
if started != true && finished != false {
t.Errorf("Expected cancellation during request processing. Started: %t. Finished: %t", started, finished)
}
err := <-errs
if _, ok := err.(influx.TimeoutError); !ok {
t.Error("Expected TimeoutError but wasn't. err was", err)
}
}