featurebase/sql3/test/helpers.go
Lory Cloutier 7031f7b968
Fb 2048 (#2363)
* Add test coverage for executionplanner.go
*ExecutionPlanner.mapper does not get tested in the case where its
context gets cancelled. In order to make testing this possible,
I've added a context argument to sql_test.MustQueryRow. If it's
nil, MustQueryRow creates a context for itself just like it always
has, but if a context is provided, it uses that.

* Adds test coverage for ExecutionPlanner.mapper in executionplanner.go
The case where the context gets cancelled mid-query is now covered.
The test is timing-dependent - the cancel call has to happen after
the query has been started but before it finishes, and in just the
right part of MustRunQuery, in order to actually produce a context
cancelled error, and not, say, a query cancelled error. May have to
adjust timing if the current delays don't work in CI testing.

* Addressed review notes
-reordered arguments for MustRunQuery
-moved MustRunQuery out of a goroutine, put the cancel in one
2023-04-03 12:04:41 -05:00

92 lines
2.8 KiB
Go

// Copyright 2021 Molecula Corp. All rights reserved.
package test
import (
"context"
"encoding/json"
"testing"
"time"
featurebase "github.com/featurebasedb/featurebase/v3"
fbcontext "github.com/featurebasedb/featurebase/v3/context"
"github.com/featurebasedb/featurebase/v3/dax"
plannertypes "github.com/featurebasedb/featurebase/v3/sql3/planner/types"
uuid "github.com/satori/go.uuid"
)
// MustQueryRows returns the row results as a slice of []interface{}, along with the columns, the query plan as a []byte or an error.
func MustQueryRows(tb testing.TB, c context.Context, svr *featurebase.Server, q string) ([][]interface{}, []*featurebase.WireQueryField, []byte, error) {
tb.Helper()
requestId, err := uuid.NewV4()
if err != nil {
return nil, nil, nil, err
}
// Originally MustQueryRows just created a context for itself do test with.
// However, for some tests, we may want access to the test's context so that,
// for example, we can cancel the context mid-test and make sure that gets
// handled correctly. Since we need to be able to cancel the context before
// the query finishes, if a context is set we also introduce a delay.
var ctx context.Context
delay := 0 * time.Millisecond
if c == nil {
ctx = fbcontext.WithRequestID(context.Background(), requestId.String())
} else {
ctx = fbcontext.WithRequestID(c, requestId.String())
delay = 100 * time.Millisecond
}
stmt, err := svr.CompileExecutionPlan(ctx, q)
if err != nil {
return nil, nil, nil, err
}
// get the plan so that code runs during testing
plan := stmt.Plan()
bplan, err := json.MarshalIndent(plan, "", " ")
if err != nil {
return nil, nil, nil, err
}
ocolumns := stmt.Schema()
rowIter, err := stmt.Iterator(ctx, nil)
if err != nil {
return nil, nil, nil, err
}
results := make([][]interface{}, 0)
// figuring out where to put the sleep took some trial and error.
// Too early and the context gets cancelled before the query can
// start running, too late and you end up with the query getting
// cancelled instead of the context.
time.Sleep(delay)
next, err := rowIter.Next(ctx)
if err != nil && err != plannertypes.ErrNoMoreRows {
return nil, nil, nil, err
}
for err != plannertypes.ErrNoMoreRows {
result := make([]interface{}, len(ocolumns))
for i := range result {
result[i] = next[i]
}
results = append(results, result)
next, err = rowIter.Next(ctx)
if err != nil && err != plannertypes.ErrNoMoreRows {
return nil, nil, nil, err
}
}
// temporarily transform to Columns()
cols := make([]*featurebase.WireQueryField, 0)
for _, oc := range ocolumns {
cols = append(cols, &featurebase.WireQueryField{
Name: dax.FieldName(oc.ColumnName),
Type: oc.Type.TypeDescription(),
BaseType: dax.BaseType(oc.Type.BaseTypeName()),
TypeInfo: oc.Type.TypeInfo(),
})
}
return results, cols, bplan, nil
}