MCPcopy Index your code
hub / github.com/MiniCodeMonkey/chief / TestLoop_WatchdogKillsHungProcess

Function TestLoop_WatchdogKillsHungProcess

internal/loop/loop_test.go:465–525  ·  view source on GitHub ↗

TestLoop_WatchdogKillsHungProcess tests that a hung process is killed after timeout.

(t *testing.T)

Source from the content-addressed store, hash-verified

463
464// TestLoop_WatchdogKillsHungProcess tests that a hung process is killed after timeout.
465func TestLoop_WatchdogKillsHungProcess(t *testing.T) {
466 l := NewLoop("/test/prd.json", "test", 5, testProvider)
467 l.iteration = 1
468
469 // Use a very short timeout for testing
470 timeout := 100 * time.Millisecond
471
472 // Collect events
473 var events []Event
474 done := make(chan bool)
475 go func() {
476 for event := range l.Events() {
477 events = append(events, event)
478 }
479 done <- true
480 }()
481
482 // Create a pipe that never sends data (simulates hung process)
483 r, w, _ := os.Pipe()
484
485 // Initialize lastOutputTime
486 l.mu.Lock()
487 l.lastOutputTime = time.Now()
488 l.mu.Unlock()
489
490 // Start watchdog with a short check interval
491 watchdogDone := make(chan struct{})
492 var fired atomic.Bool
493 go l.runWatchdog(timeout, watchdogDone, &fired)
494
495 // processOutput will block until pipe is closed (by watchdog killing would close it,
496 // but in this test we close it manually after watchdog fires)
497 go func() {
498 // Wait for watchdog to fire
499 time.Sleep(500 * time.Millisecond)
500 w.Close()
501 }()
502
503 l.processOutput(r)
504 close(watchdogDone)
505 close(l.events)
506 <-done
507
508 if !fired.Load() {
509 t.Error("Expected watchdog to fire for hung process")
510 }
511
512 // Check that we got a WatchdogTimeout event
513 hasWatchdog := false
514 for _, e := range events {
515 if e.Type == EventWatchdogTimeout {
516 hasWatchdog = true
517 if e.Text == "" {
518 t.Error("Expected watchdog event to have descriptive text")
519 }
520 }
521 }
522 if !hasWatchdog {

Callers

nothing calls this directly

Calls 5

EventsMethod · 0.95
runWatchdogMethod · 0.95
processOutputMethod · 0.95
NewLoopFunction · 0.85
LoadMethod · 0.80

Tested by

no test coverage detected