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

Function TestCostTrackingWithStateTracking

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

Source from the content-addressed store, hash-verified

1684}
1685
1686func TestCostTrackingWithStateTracking(t *testing.T) {
1687 // Cost tracking and state tracking install separate observers. Every observer has to see
1688 // every evaluation step, whichever combination of them is configured.
1689 env := testEnv(t, Variable("a", StringType))
1690 ast, iss := env.Compile(`a.startsWith("x") && a.contains("yz")`)
1691 if iss.Err() != nil {
1692 t.Fatalf("env.Compile() failed: %v", iss.Err())
1693 }
1694 baseline, _ := evalCostAndState(t, env, ast, CostTracking(nil))
1695 if baseline == 0 {
1696 t.Fatalf("cost tracking alone reported a cost of 0")
1697 }
1698 tests := []struct {
1699 name string
1700 opts []ProgramOption
1701 wantState bool
1702 wantEqCost bool
1703 }{
1704 {name: "cost", opts: []ProgramOption{CostTracking(nil)}, wantEqCost: true},
1705 {name: "cost and state", opts: []ProgramOption{CostTracking(nil), EvalOptions(OptTrackState)},
1706 wantState: true, wantEqCost: true},
1707 {name: "cost and exhaustive", opts: []ProgramOption{CostTracking(nil), EvalOptions(OptExhaustiveEval)},
1708 wantState: true, wantEqCost: true},
1709 }
1710 for _, tst := range tests {
1711 tc := tst
1712 t.Run(tc.name, func(t *testing.T) {
1713 cost, hasState := evalCostAndState(t, env, ast, tc.opts...)
1714 if tc.wantEqCost && cost != baseline {
1715 t.Errorf("actual cost got %d, wanted %d", cost, baseline)
1716 }
1717 if hasState != tc.wantState {
1718 t.Errorf("state tracked got %t, wanted %t", hasState, tc.wantState)
1719 }
1720 })
1721 }
1722}
1723
1724// evalCostAndState evaluates the ast and reports the tracked cost along with whether evaluation
1725// state was recorded.

Callers

nothing calls this directly

Calls 7

evalCostAndStateFunction · 0.85
CostTrackingFunction · 0.85
EvalOptionsFunction · 0.85
ErrMethod · 0.80
testEnvFunction · 0.70
VariableFunction · 0.70
CompileMethod · 0.65

Tested by

no test coverage detected