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

Function TestAsyncObserverOnCancellation

interpreter/async_test.go:378–435  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

376}
377
378func TestAsyncObserverOnCancellation(t *testing.T) {
379 ctx, cancel := context.WithCancelCause(context.Background())
380 frame, closeFrame := newTestFrame(t, ctx)
381 defer closeFrame()
382
383 type finishedEvent struct {
384 callID int64
385 function string
386 overload string
387 res ref.Val
388 }
389 finishedChan := make(chan finishedEvent, 1)
390
391 obs := &recordingObserverWithCallback{
392 onFinished: func(callID int64, function, overload string, res ref.Val) {
393 finishedChan <- finishedEvent{callID, function, overload, res}
394 },
395 }
396 if err := frame.SetAsyncObserver(obs); err != nil {
397 t.Fatalf("SetAsyncObserver() failed: %v", err)
398 }
399
400 release := make(chan struct{})
401 defer close(release)
402 var live, maxLive atomic.Int32
403 impl := asyncControllable(release, &live, &maxLive)
404
405 res := frame.ComputeResult(1, "fn", "fn_int", impl, []ref.Val{types.Int(1)})
406 if unk, ok := res.(*types.Unknown); ok {
407 frame.DispatchPendingAsyncCalls(unk.IDs())
408 }
409 if !types.IsUnknown(res) {
410 t.Fatalf("ComputeResult() got %v, wanted Unknown", res)
411 }
412
413 cancelErr := errors.New("aborted by user request")
414 cancel(cancelErr)
415
416 select {
417 case event := <-finishedChan:
418 if event.callID != 1 {
419 t.Errorf("observer got callID = %d, wanted 1", event.callID)
420 }
421 if event.function != "fn" || event.overload != "fn_int" {
422 t.Errorf("observer got func/overload = %q/%q, wanted fn/fn_int", event.function, event.overload)
423 }
424 if !types.IsError(event.res) {
425 t.Errorf("observer result got %v, wanted error", event.res)
426 } else {
427 errVal := event.res.(*types.Err)
428 if !strings.Contains(errVal.Error(), "aborted by user request") {
429 t.Errorf("expected observer error to contain cause, got: %v", errVal)
430 }
431 }
432 case <-time.After(2 * time.Second):
433 t.Fatal("timed out waiting for observer completion notification on context cancel")
434 }
435}

Callers

nothing calls this directly

Calls 12

IntTypeAlias · 0.92
IsUnknownFunction · 0.92
IsErrorFunction · 0.92
newTestFrameFunction · 0.85
asyncControllableFunction · 0.85
SetAsyncObserverMethod · 0.80
ComputeResultMethod · 0.80
NewMethod · 0.80
IDsMethod · 0.65
ContainsMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected