(t *testing.T)
| 822 | } |
| 823 | |
| 824 | func 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 | |
| 864 | func TestConcurrentEvalDrainReady(t *testing.T) { |
| 865 | cases := []struct { |
nothing calls this directly
no test coverage detected