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

Function TestRuntimeInterruptHandler

runtime_test.go:340–405  ·  view source on GitHub ↗

TestRuntimeInterruptHandler tests interrupt handler functionality and coverage

(t *testing.T)

Source from the content-addressed store, hash-verified

338
339// TestRuntimeInterruptHandler tests interrupt handler functionality and coverage
340func TestRuntimeInterruptHandler(t *testing.T) {
341 useStableOwnerHooksForLegacySubtests(t)
342
343 rt := NewRuntime()
344 defer rt.Close()
345
346 ctx := rt.NewContext()
347 defer ctx.Close()
348
349 t.Run("InterruptAfterDelay", func(t *testing.T) {
350 startTime := time.Now()
351 rt.SetInterruptHandler(func() int {
352 if time.Since(startTime) > time.Second {
353 return 1 // Interrupt after 1 second
354 }
355 return 0 // Continue
356 })
357
358 result := ctx.Eval(`while(true){}`)
359 defer result.Free()
360 require.True(t, result.IsException()) // Check for exceptions instead of error
361
362 // Use Context.Exception() instead of result.ToError()
363 err := ctx.Exception()
364 require.Contains(t, err.Error(), "interrupted")
365 })
366
367 t.Run("ClearBySettingNil", func(t *testing.T) {
368 // Set then clear by nil (covers else branch in SetInterruptHandler)
369 rt.SetInterruptHandler(func() int { return 1 })
370 rt.SetInterruptHandler(nil)
371
372 done := make(chan bool, 1)
373 go func() {
374 result := ctx.Eval(`let sum = 0; for(let i = 0; i < 100000; i++) sum += i; sum`)
375 defer result.Free()
376 done <- !result.IsException() // Check for exceptions instead of error
377 }()
378
379 select {
380 case success := <-done:
381 require.True(t, success)
382 case <-time.After(3 * time.Second):
383 t.Fatal("Code took too long")
384 }
385 })
386
387 t.Run("ClearExplicitly", func(t *testing.T) {
388 rt.SetInterruptHandler(func() int { return 1 })
389 rt.ClearInterruptHandler()
390
391 done := make(chan bool, 1)
392 go func() {
393 result := ctx.Eval(`let result = 42; result`)
394 defer result.Free()
395 done <- !result.IsException() // Check for exceptions instead of error
396 }()
397

Callers

nothing calls this directly

Calls 12

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

Tested by

no test coverage detected