TestRuntimeTimeoutVsInterruptHandler tests precedence between timeout and interrupt handler
(t *testing.T)
| 423 | |
| 424 | // TestRuntimeTimeoutVsInterruptHandler tests precedence between timeout and interrupt handler |
| 425 | func 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 |
| 476 | func TestRuntimeMultipleContexts(t *testing.T) { |
nothing calls this directly
no test coverage detected