2015-03-30 10:13:36 -07:00
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
2015-08-19 06:28:53 -07:00
|
|
|
"fmt"
|
2015-03-30 10:13:36 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2015-04-29 02:08:56 -07:00
|
|
|
var noop = testStmt(func(context.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-04-30 15:49:19 -07:00
|
|
|
func TestQueryConcurreny(t *testing.T) {
|
2015-06-15 03:49:11 -07:00
|
|
|
engine := NewEngine(nil, nil)
|
2015-04-30 15:49:19 -07:00
|
|
|
defer engine.Stop()
|
|
|
|
|
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
2015-08-10 05:21:24 -07:00
|
|
|
|
|
|
|
f := func(context.Context) error {
|
2015-04-30 15:49:19 -07:00
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return nil
|
2015-08-10 05:21:24 -07:00
|
|
|
}
|
2015-04-30 15:49:19 -07:00
|
|
|
|
2015-06-15 03:49:11 -07:00
|
|
|
for i := 0; i < DefaultEngineOptions.MaxConcurrentQueries; i++ {
|
2015-08-10 05:21:24 -07:00
|
|
|
q := engine.newTestQuery(f)
|
2015-04-30 15:49:19 -07:00
|
|
|
go q.Exec()
|
|
|
|
select {
|
|
|
|
case <-processing:
|
|
|
|
// Expected.
|
|
|
|
case <-time.After(5 * time.Millisecond):
|
|
|
|
t.Fatalf("Query within concurrency threshold not being executed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 05:21:24 -07:00
|
|
|
q := engine.newTestQuery(f)
|
2015-04-30 15:49:19 -07:00
|
|
|
go q.Exec()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-processing:
|
|
|
|
t.Fatalf("Query above concurrency threhosld being executed")
|
|
|
|
case <-time.After(5 * time.Millisecond):
|
|
|
|
// Expected.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate a running query.
|
|
|
|
block <- struct{}{}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-processing:
|
|
|
|
// Expected.
|
|
|
|
case <-time.After(5 * time.Millisecond):
|
|
|
|
t.Fatalf("Query within concurrency threshold not being executed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate remaining queries.
|
2015-06-15 03:49:11 -07:00
|
|
|
for i := 0; i < DefaultEngineOptions.MaxConcurrentQueries; i++ {
|
2015-04-30 15:49:19 -07:00
|
|
|
block <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 10:13:36 -07:00
|
|
|
func TestQueryTimeout(t *testing.T) {
|
2015-06-15 03:49:11 -07:00
|
|
|
engine := NewEngine(nil, &EngineOptions{
|
|
|
|
Timeout: 5 * time.Millisecond,
|
|
|
|
MaxConcurrentQueries: 20,
|
|
|
|
})
|
2015-03-30 10:13:36 -07:00
|
|
|
defer engine.Stop()
|
|
|
|
|
2015-08-10 05:21:24 -07:00
|
|
|
query := engine.newTestQuery(func(ctx context.Context) error {
|
2015-09-14 03:28:27 -07:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2015-08-10 05:21:24 -07:00
|
|
|
return contextDone(ctx, "test statement execution")
|
2015-03-30 10:13:36 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
res := query.Exec()
|
|
|
|
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) {
|
2015-06-15 03:49:11 -07:00
|
|
|
engine := NewEngine(nil, nil)
|
2015-03-30 10:13:36 -07:00
|
|
|
defer engine.Stop()
|
|
|
|
|
2015-08-10 05:21:24 -07:00
|
|
|
// Cancel a running query before it completes.
|
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
2015-03-30 10:13:36 -07:00
|
|
|
|
2015-08-10 05:21:24 -07:00
|
|
|
query1 := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
})
|
2015-04-29 02:08:56 -07:00
|
|
|
|
2015-03-30 10:13:36 -07:00
|
|
|
var res *Result
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
res = query1.Exec()
|
2015-08-10 05:21:24 -07:00
|
|
|
processing <- struct{}{}
|
2015-03-30 10:13:36 -07:00
|
|
|
}()
|
2015-08-10 05:21:24 -07:00
|
|
|
|
|
|
|
<-processing
|
2015-03-30 10:13:36 -07:00
|
|
|
query1.Cancel()
|
2015-08-10 05:21:24 -07:00
|
|
|
block <- struct{}{}
|
|
|
|
<-processing
|
2015-03-30 10:13:36 -07:00
|
|
|
|
|
|
|
if res.Err == nil {
|
|
|
|
t.Fatalf("expected cancellation error for query1 but got none")
|
|
|
|
}
|
2015-08-10 05:21:24 -07:00
|
|
|
if ee := ErrQueryCanceled("test statement execution"); res.Err != ee {
|
2015-08-25 17:04:01 -07:00
|
|
|
t.Fatalf("expected error %q, got %q", ee, res.Err)
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
|
2015-08-10 05:21:24 -07:00
|
|
|
// Canceling a query before starting it must have no effect.
|
|
|
|
query2 := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
})
|
|
|
|
|
2015-03-30 10:13:36 -07:00
|
|
|
query2.Cancel()
|
|
|
|
res = query2.Exec()
|
|
|
|
if res.Err != nil {
|
|
|
|
t.Fatalf("unexpeceted error on executing query2: %s", res.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEngineShutdown(t *testing.T) {
|
2015-06-15 03:49:11 -07:00
|
|
|
engine := NewEngine(nil, nil)
|
2015-03-30 10:13:36 -07:00
|
|
|
|
2015-08-10 05:21:24 -07:00
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
|
|
|
|
2015-03-30 10:13:36 -07:00
|
|
|
// Shutdown engine on first handler execution. Should handler execution ever become
|
|
|
|
// concurrent this test has to be adjusted accordingly.
|
2015-08-10 05:21:24 -07:00
|
|
|
f := func(ctx context.Context) error {
|
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
}
|
|
|
|
query1 := engine.newTestQuery(f)
|
2015-03-30 10:13:36 -07:00
|
|
|
|
2015-04-29 02:08:56 -07:00
|
|
|
// Stopping the engine must cancel the base context. While executing queries is
|
|
|
|
// still possible, their context is canceled from the beginning and execution should
|
2015-03-30 10:13:36 -07:00
|
|
|
// terminate immediately.
|
|
|
|
|
2015-08-10 05:21:24 -07:00
|
|
|
var res *Result
|
|
|
|
go func() {
|
|
|
|
res = query1.Exec()
|
|
|
|
processing <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-processing
|
|
|
|
engine.Stop()
|
|
|
|
block <- struct{}{}
|
|
|
|
<-processing
|
|
|
|
|
2015-03-30 10:13:36 -07:00
|
|
|
if res.Err == nil {
|
|
|
|
t.Fatalf("expected error on shutdown during query but got none")
|
|
|
|
}
|
2015-08-10 05:21:24 -07:00
|
|
|
if ee := ErrQueryCanceled("test statement execution"); res.Err != ee {
|
|
|
|
t.Fatalf("expected error %q, got %q", ee, res.Err)
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
|
2015-08-10 05:21:24 -07:00
|
|
|
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.
|
2015-03-30 10:13:36 -07:00
|
|
|
res2 := query2.Exec()
|
|
|
|
if res2.Err == nil {
|
|
|
|
t.Fatalf("expected error on querying shutdown engine but got none")
|
|
|
|
}
|
2015-08-10 05:21:24 -07:00
|
|
|
if _, ok := res2.Err.(ErrQueryCanceled); !ok {
|
|
|
|
t.Fatalf("expected cancelation error, got %q", res2.Err)
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 06:28:53 -07:00
|
|
|
|
|
|
|
func TestRecoverEvaluatorRuntime(t *testing.T) {
|
|
|
|
var ev *evaluator
|
|
|
|
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) {
|
|
|
|
var ev *evaluator
|
|
|
|
var err error
|
|
|
|
|
|
|
|
e := fmt.Errorf("custom error")
|
|
|
|
|
2015-08-25 17:04:01 -07:00
|
|
|
defer func() {
|
|
|
|
if err.Error() != e.Error() {
|
|
|
|
t.Fatalf("wrong error message: %q, expected %q", err, e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer ev.recover(&err)
|
|
|
|
|
|
|
|
panic(e)
|
2015-08-19 06:28:53 -07:00
|
|
|
}
|