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

Function TestSyncEvalRejectedInAsyncEnv

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

Source from the content-addressed store, hash-verified

797}
798
799func TestSyncEvalRejectedInAsyncEnv(t *testing.T) {
800 // Env-level rejection: an environment that declares any async function rejects the synchronous
801 // entry points even for an expression that does not call the async function. Callers needing
802 // synchronous evaluation should build a separate, non-async environment.
803 prg := mustProgram(t, `x + 1`, // pure, synchronous, does not use rpc
804 cel.Variable("x", cel.IntType),
805 cel.Function("rpc",
806 cel.Overload("rpc_string", []*cel.Type{cel.StringType}, cel.StringType,
807 cel.AsyncBinding(func(ctx context.Context, args ...ref.Val) ref.Val { return args[0] }))),
808 )
809 if _, _, err := prg.Eval(map[string]any{"x": 1}); err == nil || !strings.Contains(err.Error(), "ConcurrentEval") {
810 t.Errorf("Eval() in async env = %v, want ConcurrentEval error", err)
811 }
812
813 // A separate non-async env evaluates the same expression synchronously.
814 syncPrg := mustProgram(t, `x + 1`, cel.Variable("x", cel.IntType))
815 out, _, err := syncPrg.Eval(map[string]any{"x": 1})
816 if err != nil {
817 t.Fatalf("Eval() in non-async env returned error: %v", err)
818 }
819 if out.Equal(types.Int(2)) != types.True {
820 t.Errorf("Eval() = %v, want 2", out)
821 }
822}
823
824func TestBypassAsyncProtection(t *testing.T) {
825 // An environment that declares an async function allows synchronous Eval and ContextEval

Callers

nothing calls this directly

Calls 10

VariableFunction · 0.92
FunctionFunction · 0.92
OverloadFunction · 0.92
AsyncBindingFunction · 0.92
IntTypeAlias · 0.92
mustProgramFunction · 0.85
EvalMethod · 0.65
ContainsMethod · 0.65
EqualMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected