|
|
|
// Copyright 2016 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
|
|
"github.com/prometheus/prometheus/storage"
|
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestQueryConcurrency(t *testing.T) {
|
|
|
|
concurrentQueries := 10
|
|
|
|
|
|
|
|
engine := NewEngine(nil, nil, concurrentQueries, 10*time.Second)
|
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
|
|
|
|
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
|
|
|
|
|
|
|
f := func(context.Context) error {
|
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < concurrentQueries; i++ {
|
|
|
|
q := engine.newTestQuery(f)
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
8 years ago
|
|
|
go q.Exec(ctx)
|
|
|
|
select {
|
|
|
|
case <-processing:
|
|
|
|
// Expected.
|
|
|
|
case <-time.After(20 * time.Millisecond):
|
|
|
|
t.Fatalf("Query within concurrency threshold not being executed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
q := engine.newTestQuery(f)
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
8 years ago
|
|
|
go q.Exec(ctx)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-processing:
|
|
|
|
t.Fatalf("Query above concurrency threshold being executed")
|
|
|
|
case <-time.After(20 * time.Millisecond):
|
|
|
|
// Expected.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate a running query.
|
|
|
|
block <- struct{}{}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-processing:
|
|
|
|
// Expected.
|
|
|
|
case <-time.After(20 * time.Millisecond):
|
|
|
|
t.Fatalf("Query within concurrency threshold not being executed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate remaining queries.
|
|
|
|
for i := 0; i < concurrentQueries; i++ {
|
|
|
|
block <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryTimeout(t *testing.T) {
|
|
|
|
engine := NewEngine(nil, nil, 20, 5*time.Millisecond)
|
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
|
|
|
|
|
|
|
query := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
})
|
|
|
|
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
8 years ago
|
|
|
res := query.Exec(ctx)
|
|
|
|
if res.Err == nil {
|
|
|
|
t.Fatalf("expected timeout error but got none")
|
|
|
|
}
|
|
|
|
if _, ok := res.Err.(ErrQueryTimeout); res.Err != nil && !ok {
|
|
|
|
t.Fatalf("expected timeout error but got: %s", res.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryCancel(t *testing.T) {
|
|
|
|
engine := NewEngine(nil, nil, 10, 10*time.Second)
|
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
|
|
|
|
|
|
|
// Cancel a running query before it completes.
|
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
|
|
|
|
|
|
|
query1 := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
})
|
|
|
|
|
|
|
|
var res *Result
|
|
|
|
|
|
|
|
go func() {
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
8 years ago
|
|
|
res = query1.Exec(ctx)
|
|
|
|
processing <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-processing
|
|
|
|
query1.Cancel()
|
|
|
|
block <- struct{}{}
|
|
|
|
<-processing
|
|
|
|
|
|
|
|
if res.Err == nil {
|
|
|
|
t.Fatalf("expected cancellation error for query1 but got none")
|
|
|
|
}
|
|
|
|
if ee := ErrQueryCanceled("test statement execution"); res.Err != ee {
|
|
|
|
t.Fatalf("expected error %q, got %q", ee, res.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Canceling a query before starting it must have no effect.
|
|
|
|
query2 := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
})
|
|
|
|
|
|
|
|
query2.Cancel()
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
8 years ago
|
|
|
res = query2.Exec(ctx)
|
|
|
|
if res.Err != nil {
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
8 years ago
|
|
|
t.Fatalf("unexpected error on executing query2: %s", res.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// errQuerier implements storage.Querier which always returns error.
|
|
|
|
type errQuerier struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *errQuerier) Select(*storage.SelectParams, ...*labels.Matcher) (storage.SeriesSet, error) {
|
|
|
|
return errSeriesSet{err: q.err}, q.err
|
|
|
|
}
|
|
|
|
func (*errQuerier) LabelValues(name string) ([]string, error) { return nil, nil }
|
|
|
|
func (*errQuerier) Close() error { return nil }
|
|
|
|
|
|
|
|
// errSeriesSet implements storage.SeriesSet which always returns error.
|
|
|
|
type errSeriesSet struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (errSeriesSet) Next() bool { return false }
|
|
|
|
func (errSeriesSet) At() storage.Series { return nil }
|
|
|
|
func (e errSeriesSet) Err() error { return e.err }
|
|
|
|
|
|
|
|
func TestQueryError(t *testing.T) {
|
|
|
|
engine := NewEngine(nil, nil, 10, 10*time.Second)
|
|
|
|
errStorage := ErrStorage(fmt.Errorf("storage error"))
|
|
|
|
queryable := storage.QueryableFunc(func(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
|
|
|
|
return &errQuerier{err: errStorage}, nil
|
|
|
|
})
|
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
|
|
|
|
|
|
|
vectorQuery, err := engine.NewInstantQuery(queryable, "foo", time.Unix(1, 0))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating query: %q", err)
|
|
|
|
}
|
|
|
|
res := vectorQuery.Exec(ctx)
|
|
|
|
if res.Err == nil {
|
|
|
|
t.Fatalf("expected error on failed select but got none")
|
|
|
|
}
|
|
|
|
if res.Err != errStorage {
|
|
|
|
t.Fatalf("expected error %q, got %q", errStorage, res.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
matrixQuery, err := engine.NewInstantQuery(queryable, "foo[1m]", time.Unix(1, 0))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating query: %q", err)
|
|
|
|
}
|
|
|
|
res = matrixQuery.Exec(ctx)
|
|
|
|
if res.Err == nil {
|
|
|
|
t.Fatalf("expected error on failed select but got none")
|
|
|
|
}
|
|
|
|
if res.Err != errStorage {
|
|
|
|
t.Fatalf("expected error %q, got %q", errStorage, res.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEngineShutdown(t *testing.T) {
|
|
|
|
engine := NewEngine(nil, nil, 10, 10*time.Second)
|
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
|
|
|
|
|
|
|
// Shutdown engine on first handler execution. Should handler execution ever become
|
|
|
|
// concurrent this test has to be adjusted accordingly.
|
|
|
|
f := func(ctx context.Context) error {
|
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
}
|
|
|
|
query1 := engine.newTestQuery(f)
|
|
|
|
|
|
|
|
// Stopping the engine must cancel the base context. While executing queries is
|
|
|
|
// still possible, their context is canceled from the beginning and execution should
|
|
|
|
// terminate immediately.
|
|
|
|
|
|
|
|
var res *Result
|
|
|
|
go func() {
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
8 years ago
|
|
|
res = query1.Exec(ctx)
|
|
|
|
processing <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-processing
|
|
|
|
cancelCtx()
|
|
|
|
block <- struct{}{}
|
|
|
|
<-processing
|
|
|
|
|
|
|
|
if res.Err == nil {
|
|
|
|
t.Fatalf("expected error on shutdown during query but got none")
|
|
|
|
}
|
|
|
|
if ee := ErrQueryCanceled("test statement execution"); res.Err != ee {
|
|
|
|
t.Fatalf("expected error %q, got %q", ee, res.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
query2 := engine.newTestQuery(func(context.Context) error {
|
|
|
|
t.Fatalf("reached query execution unexpectedly")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// The second query is started after the engine shut down. It must
|
|
|
|
// be canceled immediately.
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
8 years ago
|
|
|
res2 := query2.Exec(ctx)
|
|
|
|
if res2.Err == nil {
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
8 years ago
|
|
|
t.Fatalf("expected error on querying with canceled context but got none")
|
|
|
|
}
|
|
|
|
if _, ok := res2.Err.(ErrQueryCanceled); !ok {
|
|
|
|
t.Fatalf("expected cancellation error, got %q", res2.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEngineEvalStmtTimestamps(t *testing.T) {
|
|
|
|
test, err := NewTest(t, `
|
|
|
|
load 10s
|
|
|
|
metric 1 2
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating test: %q", err)
|
|
|
|
}
|
|
|
|
defer test.Close()
|
|
|
|
|
|
|
|
err = test.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error initializing test: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Query string
|
|
|
|
Result Value
|
|
|
|
Start time.Time
|
|
|
|
End time.Time
|
|
|
|
Interval time.Duration
|
|
|
|
ShouldError bool
|
|
|
|
}{
|
|
|
|
// Instant queries.
|
|
|
|
{
|
|
|
|
Query: "1",
|
|
|
|
Result: Scalar{V: 1, T: 1000},
|
|
|
|
Start: time.Unix(1, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
Result: Vector{
|
|
|
|
Sample{Point: Point{V: 1, T: 1000},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
Start: time.Unix(1, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric[20s]",
|
|
|
|
Result: Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 2, T: 10000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
Start: time.Unix(10, 0),
|
|
|
|
},
|
|
|
|
// Range queries.
|
|
|
|
{
|
|
|
|
Query: "1",
|
|
|
|
Result: Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 1000}, {V: 1, T: 2000}},
|
|
|
|
Metric: labels.FromStrings()},
|
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(2, 0),
|
|
|
|
Interval: time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
Result: Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 1000}, {V: 1, T: 2000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(2, 0),
|
|
|
|
Interval: time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
Result: Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 5000}, {V: 2, T: 10000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(10, 0),
|
|
|
|
Interval: 5 * time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: `count_values("wrong label!", metric)`,
|
|
|
|
ShouldError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
var err error
|
|
|
|
var qry Query
|
|
|
|
if c.Interval == 0 {
|
|
|
|
qry, err = test.QueryEngine().NewInstantQuery(test.Queryable(), c.Query, c.Start)
|
|
|
|
} else {
|
|
|
|
qry, err = test.QueryEngine().NewRangeQuery(test.Queryable(), c.Query, c.Start, c.End, c.Interval)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating query: %q", err)
|
|
|
|
}
|
|
|
|
res := qry.Exec(test.Context())
|
|
|
|
if c.ShouldError {
|
|
|
|
testutil.NotOk(t, res.Err, "expected error for the query %q", c.Query)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if res.Err != nil {
|
|
|
|
t.Fatalf("unexpected error running query: %q", res.Err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(res.Value, c.Result) {
|
|
|
|
t.Fatalf("unexpected result for query %q: got %q wanted %q", c.Query, res.Value.String(), c.Result.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecoverEvaluatorRuntime(t *testing.T) {
|
|
|
|
ev := &evaluator{logger: log.NewNopLogger()}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
defer ev.recover(&err)
|
|
|
|
|
|
|
|
// Cause a runtime panic.
|
|
|
|
var a []int
|
|
|
|
a[123] = 1
|
|
|
|
|
|
|
|
if err.Error() != "unexpected error" {
|
|
|
|
t.Fatalf("wrong error message: %q, expected %q", err, "unexpected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecoverEvaluatorError(t *testing.T) {
|
|
|
|
ev := &evaluator{logger: log.NewNopLogger()}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
e := fmt.Errorf("custom error")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err.Error() != e.Error() {
|
|
|
|
t.Fatalf("wrong error message: %q, expected %q", err, e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer ev.recover(&err)
|
|
|
|
|
|
|
|
panic(e)
|
|
|
|
}
|