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

Function TestBindingsMemoryLimitStringConcat

ext/bindings_test.go:441–509  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

439}
440
441func TestBindingsMemoryLimitStringConcat(t *testing.T) {
442 // A 1MiB input string doubled per bind level. The doublings rapidly exceed the 1M unit
443 // memory limit, so evaluation must terminate while the bound values are still being
444 // constructed; unchecked, the doublings would allocate ~510MiB cumulative through a8.
445 env, err := cel.NewEnv(Bindings(), cel.Variable("s", cel.StringType))
446 if err != nil {
447 t.Fatalf("cel.NewEnv(Bindings()) failed: %v", err)
448 }
449 in := map[string]any{"s": strings.Repeat("a", 1<<20)}
450 compile := func(levels int) *cel.Ast {
451 ast, iss := env.Compile(doublingStringBindExpr(levels))
452 if iss.Err() != nil {
453 t.Fatalf("env.Compile() failed: %v", iss.Err())
454 }
455 return ast
456 }
457 measure := func(t *testing.T, prg cel.Program) (time.Duration, uint64, error) {
458 var before, after runtime.MemStats
459 runtime.GC()
460 runtime.ReadMemStats(&before)
461 start := time.Now()
462 _, _, err := prg.Eval(in)
463 elapsed := time.Since(start)
464 runtime.ReadMemStats(&after)
465 return elapsed, after.TotalAlloc - before.TotalAlloc, err
466 }
467
468 // Subtests share process-global MemStats and must not run in parallel.
469 t.Run("limit_trips_mid_amplification", func(t *testing.T) {
470 prg, err := env.Program(compile(8), cel.MemoryLimit(1_000_000))
471 if err != nil {
472 t.Fatalf("env.Program() failed: %v", err)
473 }
474 elapsed, allocated, err := measure(t, prg)
475 if err == nil || !strings.Contains(err.Error(), "memory limit exceeded") {
476 t.Fatalf("prg.Eval() got error %v, wanted error containing 'memory limit exceeded'", err)
477 }
478 t.Logf("limit tripped in %v after allocating %d bytes", elapsed, allocated)
479 // The enforcement point follows the materialization of the offending value, so a
480 // few doublings of intermediates is the expected floor; the bound proves the
481 // remaining exponential trajectory (~510MiB unchecked) was halted.
482 const maxAllocBytes = 64 << 20 // 64MiB
483 if allocated > maxAllocBytes {
484 t.Errorf("evaluation allocated %d bytes, wanted less than %d", allocated, maxAllocBytes)
485 }
486 if elapsed > 5*time.Second {
487 t.Errorf("evaluation took %v, wanted under 5s", elapsed)
488 }
489 })
490
491 t.Run("unchecked_go_layer_baseline", func(t *testing.T) {
492 // The same workload the tripped case performed (doubling through 16MiB) without
493 // memory tracking: quantifies the Go-layer time and allocation the limit raced
494 // against, and the tracking overhead paid by the tripped case.
495 prg, err := env.Program(compile(4))
496 if err != nil {
497 t.Fatalf("env.Program() failed: %v", err)
498 }

Callers

nothing calls this directly

Calls 12

CompileMethod · 0.95
ProgramMethod · 0.95
NewEnvFunction · 0.92
VariableFunction · 0.92
MemoryLimitFunction · 0.92
BindingsFunction · 0.85
doublingStringBindExprFunction · 0.85
ErrMethod · 0.80
EvalMethod · 0.65
ContainsMethod · 0.65
compileFunction · 0.50
ErrorMethod · 0.45

Tested by

no test coverage detected