Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ func TestContextEval(t *testing.T) {
if iss.Err() != nil {
t.Fatalf("env.Compile(expr) failed: %v", iss.Err())
}
prg, err := env.Program(ast, EvalOptions(OptOptimize))
prg, err := env.Program(ast, EvalOptions(OptOptimize|OptTrackState), InterruptCheckFrequency(100))
if err != nil {
t.Fatalf("env.Program() failed: %v", err)
}
Expand All @@ -999,23 +999,23 @@ func TestContextEval(t *testing.T) {
for i := int64(0); i < 1000; i++ {
items[i] = i
}
out, _, err := ContextEval(ctx, prg, map[string]interface{}{"items": items})
out, _, err := prg.ContextEval(ctx, map[string]interface{}{"items": items})
if err != nil {
t.Fatalf("ContextEval() failed: %v", err)
t.Fatalf("prg.ContextEval() failed: %v", err)
}
if out != types.Int(975) {
t.Errorf("ContextEval() got %v, wanted 75", out)
t.Errorf("prg.ContextEval() got %v, wanted 75", out)
}

evalCtx, cancel := context.WithTimeout(ctx, 10*time.Microsecond)
evalCtx, cancel := context.WithTimeout(ctx, time.Microsecond)
defer cancel()

out, _, err = ContextEval(evalCtx, prg, map[string]interface{}{"items": items})
out, _, err = prg.ContextEval(evalCtx, map[string]interface{}{"items": items})
if err == nil {
t.Errorf("Got result %v, wanted timeout error", out)
}
if err != nil && err.Error() != "operation cancelled" {
t.Errorf("Got %v, wanted operation cancelled error", err)
if err != nil && err.Error() != "operation interrupted" {
t.Errorf("Got %v, wanted operation interrupted error", err)
}
}

Expand All @@ -1032,7 +1032,7 @@ func BenchmarkContextEval(b *testing.B) {
if iss.Err() != nil {
b.Fatalf("env.Compile(expr) failed: %v", iss.Err())
}
prg, err := env.Program(ast, EvalOptions(OptOptimize))
prg, err := env.Program(ast, EvalOptions(OptOptimize), InterruptCheckFrequency(200))
if err != nil {
b.Fatalf("env.Program() failed: %v", err)
}
Expand All @@ -1043,12 +1043,12 @@ func BenchmarkContextEval(b *testing.B) {
items[i] = i
}
for i := 0; i < b.N; i++ {
out, _, err := ContextEval(ctx, prg, map[string]interface{}{"items": items})
out, _, err := prg.ContextEval(ctx, map[string]interface{}{"items": items})
if err != nil {
b.Fatalf("ContextEval() failed: %v", err)
b.Fatalf("prg.ContextEval() failed: %v", err)
}
if out != types.Int(75) {
b.Errorf("ContextEval() got %v, wanted 75", out)
b.Errorf("prg.ContextEval() got %v, wanted 75", out)
}
}
}
Expand Down Expand Up @@ -1540,7 +1540,7 @@ func TestResidualAst_AttributeQualifiers(t *testing.T) {
},
"y": []int{123},
}, AttributePattern("u"))
out, det, err := prg.Eval(vars)
out, det, err := prg.ContextEval(context.TODO(), vars)
if !types.IsUnknown(out) {
t.Fatalf("got %v, expected unknown", out)
}
Expand Down
11 changes: 10 additions & 1 deletion cel/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ const (
// member graph.
//
// By itself, OptPartialEval does not change evaluation behavior unless the input to the
// Program Eval is an PartialVars.
// Program Eval() call is created via PartialVars().
OptPartialEval EvalOption = 1 << iota
)

Expand All @@ -387,6 +387,15 @@ func EvalOptions(opts ...EvalOption) ProgramOption {
}
}

// InterruptCheckFrequency configures the number of iterations within a comprehension to evaluate
// before checking whether the function evaluation has been interrupted.
func InterruptCheckFrequency(checkFrequency uint) ProgramOption {
return func(p *prog) (*prog, error) {
p.interruptCheckFrequency = checkFrequency
return p, nil
}
}

func fieldToCELType(field protoreflect.FieldDescriptor) (*exprpb.Type, error) {
if field.Kind() == protoreflect.MessageKind {
msgName := (string)(field.Message().FullName())
Expand Down
Loading