(t *testing.T)
| 814 | } |
| 815 | |
| 816 | func TestConcurrentEvalRecover(t *testing.T) { |
| 817 | env, err := cel.NewEnv( |
| 818 | cel.Function("panic", |
| 819 | cel.Overload("global_panic", []*cel.Type{}, cel.BoolType, |
| 820 | cel.FunctionBinding(func(args ...ref.Val) ref.Val { |
| 821 | panic("watch me recover") |
| 822 | }), |
| 823 | ), |
| 824 | ), |
| 825 | cel.Function("cancel_panic", |
| 826 | cel.Overload("global_cancel_panic", []*cel.Type{}, cel.BoolType, |
| 827 | cel.FunctionBinding(func(args ...ref.Val) ref.Val { |
| 828 | panic(interpreter.EvalCancelledError{Message: "eval cancelled", Cause: interpreter.ContextCancelled}) |
| 829 | }), |
| 830 | ), |
| 831 | ), |
| 832 | cel.Function("sleep_func", |
| 833 | cel.Overload("global_sleep_func", []*cel.Type{}, cel.BoolType, |
| 834 | cel.AsyncBinding(func(ctx context.Context, args ...ref.Val) ref.Val { |
| 835 | time.Sleep(1 * time.Second) |
| 836 | return types.True |
| 837 | }), |
| 838 | ), |
| 839 | ), |
| 840 | ) |
| 841 | if err != nil { |
| 842 | t.Fatalf("cel.NewEnv() failed: %v", err) |
| 843 | } |
| 844 | |
| 845 | tests := []struct { |
| 846 | name string |
| 847 | expr string |
| 848 | prgOpts []cel.ProgramOption |
| 849 | getCtx func() (context.Context, context.CancelFunc) |
| 850 | wantErr any |
| 851 | }{ |
| 852 | { |
| 853 | name: "panic", |
| 854 | expr: "panic()", |
| 855 | wantErr: "internal error: watch me recover", |
| 856 | }, |
| 857 | { |
| 858 | name: "panic_tracked_state", |
| 859 | expr: "panic()", |
| 860 | prgOpts: []cel.ProgramOption{cel.EvalOptions(cel.OptTrackState)}, |
| 861 | wantErr: "internal error: watch me recover", |
| 862 | }, |
| 863 | { |
| 864 | name: "eval_cancelled_error", |
| 865 | expr: "cancel_panic()", |
| 866 | wantErr: &interpreter.EvalCancelledError{}, |
| 867 | }, |
| 868 | { |
| 869 | name: "context_timeout", |
| 870 | expr: "sleep_func()", |
| 871 | getCtx: func() (context.Context, context.CancelFunc) { |
| 872 | return context.WithTimeout(context.Background(), 10*time.Millisecond) |
| 873 | }, |
nothing calls this directly
no test coverage detected