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

Function TestLateBoundFunctionCost

cel/cel_test.go:6310–6370  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

6308}
6309
6310func TestLateBoundFunctionCost(t *testing.T) {
6311 env, err := NewEnv(
6312 Variable("x", IntType),
6313 Function("rpc",
6314 Overload("rpc_int", []*Type{IntType}, IntType, LateFunctionBinding()),
6315 ),
6316 )
6317 if err != nil {
6318 t.Fatalf("NewEnv() failed: %v", err)
6319 }
6320 ast, iss := env.Compile("rpc(x)")
6321 if iss.Err() != nil {
6322 t.Fatalf("Compile() failed: %v", iss.Err())
6323 }
6324
6325 // Without a published estimate a late-bound call is charged the standard default for a
6326 // function call, since nothing about the implementation is known at estimation time.
6327 est, err := env.EstimateCost(ast, testCostEstimator{hints: map[string]uint64{}})
6328 if err != nil {
6329 t.Fatalf("EstimateCost() failed: %v", err)
6330 }
6331 if est.Max != 2 { // 1 for the call, 1 for resolving 'x'
6332 t.Errorf("EstimateCost() got max %d, wanted 2", est.Max)
6333 }
6334
6335 // An estimate may be attached to the overload by id, which is how a caller declares the cost
6336 // of the implementations it intends to bind.
6337 est, err = env.EstimateCost(ast, testCostEstimator{hints: map[string]uint64{}},
6338 cost.OverloadCostEstimate("rpc_int", func(estimator cost.Estimator, target *cost.AstNode, args []cost.AstNode) *cost.CallEstimate {
6339 return &cost.CallEstimate{CostEstimate: cost.FixedCostEstimate(42)}
6340 }))
6341 if err != nil {
6342 t.Fatalf("EstimateCost() failed: %v", err)
6343 }
6344 if est.Max != 43 { // 42 for the call, 1 for resolving 'x'
6345 t.Errorf("EstimateCost() got max %d, wanted 43", est.Max)
6346 }
6347
6348 // Runtime cost tracking can likewise be attached by overload id.
6349 prg, err := env.Program(ast, CostTracking(nil),
6350 CostTrackerOptions(cost.OverloadTracker("rpc_int", func(args []ref.Val, result ref.Val) *uint64 {
6351 trackedCost := uint64(99)
6352 return &trackedCost
6353 })))
6354 if err != nil {
6355 t.Fatalf("Program() failed: %v", err)
6356 }
6357 act, err := FunctionVars(map[string]any{"x": 1}, map[string]LateBoundFunction{
6358 "rpc": func(overloadID string, args ...ref.Val) ref.Val { return args[0] },
6359 })
6360 if err != nil {
6361 t.Fatalf("FunctionVars() failed: %v", err)
6362 }
6363 _, det, err := prg.Eval(act)
6364 if err != nil {
6365 t.Fatalf("Eval() failed: %v", err)
6366 }
6367 if *det.ActualCost() != 100 { // 99 for the call, 1 for resolving 'x'

Callers

nothing calls this directly

Calls 15

CompileMethod · 0.95
EstimateCostMethod · 0.95
ProgramMethod · 0.95
OverloadCostEstimateFunction · 0.92
FixedCostEstimateFunction · 0.92
OverloadTrackerFunction · 0.92
CostTrackingFunction · 0.85
CostTrackerOptionsFunction · 0.85
FunctionVarsFunction · 0.85
ErrMethod · 0.80
NewEnvFunction · 0.70
VariableFunction · 0.70

Tested by

no test coverage detected