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

Function TestConcurrentEvalProgramThreadSafety

cel/program_async_test.go:604–639  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

602}
603
604func TestConcurrentEvalProgramThreadSafety(t *testing.T) {
605 prg := mustProgram(t, `async_func(x) + 1`,
606 cel.Variable("x", cel.IntType),
607 cel.Function("async_func",
608 cel.Overload("async_func_int", []*cel.Type{cel.IntType}, cel.IntType,
609 cel.AsyncBinding(func(ctx context.Context, args ...ref.Val) ref.Val {
610 time.Sleep(5 * time.Millisecond)
611 return args[0]
612 }),
613 ),
614 ),
615 )
616
617 const numGoroutines = 10
618 errCh := make(chan error, numGoroutines)
619 for i := range numGoroutines {
620 go func(val int64) {
621 res := awaitEval(context.Background(), t, prg, map[string]any{"x": val})
622 if res.Err != nil {
623 errCh <- res.Err
624 return
625 }
626 if res.Val.Equal(types.Int(val+1)) != types.True {
627 errCh <- errors.New("unexpected eval result")
628 return
629 }
630 errCh <- nil
631 }(int64(i * 10))
632 }
633
634 for range numGoroutines {
635 if err := <-errCh; err != nil {
636 t.Errorf("Concurrent thread safety evaluation failed: %v", err)
637 }
638 }
639}
640
641// TestConcurrentEvalDetailsAfterPublish is a regression test for a data race on the EvalDetails
642// returned by ConcurrentEval.

Callers

nothing calls this directly

Calls 9

VariableFunction · 0.92
FunctionFunction · 0.92
OverloadFunction · 0.92
AsyncBindingFunction · 0.92
IntTypeAlias · 0.92
mustProgramFunction · 0.85
awaitEvalFunction · 0.85
NewMethod · 0.80
EqualMethod · 0.65

Tested by

no test coverage detected