MCPcopy Create free account
hub / github.com/buke/quickjs-go / TestRuntimeTimeoutVsInterruptHandler

Function TestRuntimeTimeoutVsInterruptHandler

runtime_test.go:425–473  ·  view source on GitHub ↗

TestRuntimeTimeoutVsInterruptHandler tests precedence between timeout and interrupt handler

(t *testing.T)

Source from the content-addressed store, hash-verified

423
424// TestRuntimeTimeoutVsInterruptHandler tests precedence between timeout and interrupt handler
425func TestRuntimeTimeoutVsInterruptHandler(t *testing.T) {
426 t.Run("TimeoutOverridesHandler", func(t *testing.T) {
427 rt := NewRuntime()
428 defer rt.Close()
429
430 ctx := rt.NewContext()
431 defer ctx.Close()
432
433 // Set handler first, then timeout (timeout should override)
434 rt.SetInterruptHandler(func() int { return 0 })
435 rt.SetExecuteTimeout(1)
436
437 start := time.Now()
438 result := ctx.Eval(`while(true){}`)
439 defer result.Free()
440 elapsed := time.Since(start)
441
442 require.True(t, result.IsException()) // Check for exceptions instead of error
443
444 // Use Context.Exception() instead of result.ToError()
445 err := ctx.Exception()
446 require.Contains(t, err.Error(), "interrupted")
447 require.Less(t, elapsed, 3*time.Second)
448 })
449
450 t.Run("HandlerOverridesTimeout", func(t *testing.T) {
451 rt := NewRuntime()
452 defer rt.Close()
453
454 ctx := rt.NewContext()
455 defer ctx.Close()
456
457 // Set timeout first, then handler (handler should override)
458 rt.SetExecuteTimeout(10)
459 rt.SetInterruptHandler(func() int { return 1 })
460
461 start := time.Now()
462 result := ctx.Eval(`while(true){}`)
463 defer result.Free()
464 elapsed := time.Since(start)
465
466 require.True(t, result.IsException()) // Check for exceptions instead of error
467
468 // Use Context.Exception() instead of result.ToError()
469 err := ctx.Exception()
470 require.Contains(t, err.Error(), "interrupted")
471 require.Less(t, elapsed, 3*time.Second)
472 })
473}
474
475// TestRuntimeMultipleContexts tests creating and using multiple contexts
476func TestRuntimeMultipleContexts(t *testing.T) {

Callers

nothing calls this directly

Calls 11

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
SetInterruptHandlerMethod · 0.95
SetExecuteTimeoutMethod · 0.95
EvalMethod · 0.95
ExceptionMethod · 0.95
NewRuntimeFunction · 0.85
IsExceptionMethod · 0.80
FreeMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected