MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestBypassAsyncProtection

Function TestBypassAsyncProtection

cel/program_async_test.go:824–862  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

822}
823
824func TestBypassAsyncProtection(t *testing.T) {
825 // An environment that declares an async function allows synchronous Eval and ContextEval
826 // when BypassAsyncProtection is specified.
827 prg := mustProgram(t, `x + 1`,
828 cel.Variable("x", cel.IntType),
829 cel.Function("rpc",
830 cel.Overload("rpc_string", []*cel.Type{cel.StringType}, cel.StringType,
831 cel.AsyncBinding(func(ctx context.Context, args ...ref.Val) ref.Val { return args[0] }))),
832 cel.BypassAsyncProtection(),
833 )
834 out, _, err := prg.Eval(map[string]any{"x": 1})
835 if err != nil {
836 t.Fatalf("Eval() with BypassAsyncProtection failed: %v", err)
837 }
838 if out.Equal(types.Int(2)) != types.True {
839 t.Errorf("Eval() = %v, want 2", out)
840 }
841
842 out, _, err = prg.ContextEval(context.Background(), map[string]any{"x": 2})
843 if err != nil {
844 t.Fatalf("ContextEval() with BypassAsyncProtection failed: %v", err)
845 }
846 if out.Equal(types.Int(3)) != types.True {
847 t.Errorf("ContextEval() = %v, want 3", out)
848 }
849
850 // An expression that actually calls an asynchronous function still fails at runtime
851 // when evaluated synchronously, even with BypassAsyncProtection.
852 asyncPrg := mustProgram(t, `rpc("a")`,
853 cel.Function("rpc",
854 cel.Overload("rpc_string", []*cel.Type{cel.StringType}, cel.StringType,
855 cel.AsyncBinding(func(ctx context.Context, args ...ref.Val) ref.Val { return args[0] }))),
856 cel.BypassAsyncProtection(),
857 )
858 _, _, err = asyncPrg.Eval(cel.NoVars())
859 if err == nil || !strings.Contains(err.Error(), "asynchronous function calls require concurrent evaluation") {
860 t.Errorf("Eval() on actual async call with BypassAsyncProtection = %v, want error mentioning concurrent evaluation", err)
861 }
862}
863
864func TestConcurrentEvalDrainReady(t *testing.T) {
865 cases := []struct {

Callers

nothing calls this directly

Calls 13

VariableFunction · 0.92
FunctionFunction · 0.92
OverloadFunction · 0.92
AsyncBindingFunction · 0.92
BypassAsyncProtectionFunction · 0.92
IntTypeAlias · 0.92
NoVarsFunction · 0.92
mustProgramFunction · 0.85
EvalMethod · 0.65
EqualMethod · 0.65
ContextEvalMethod · 0.65
ContainsMethod · 0.65

Tested by

no test coverage detected