Files
Reims-Go/internal/host/procwait_windows_test.go
NwflowerandClaude Opus 5.5 12fa25d089 fix(shell): notice a VM that stops by itself at once
When macOS shut down or QEMU quit on its own, reims-go had issued no
stop, so it only saw the VM stopped at its next steady poll, up to 5 s
later. Now pollVMs waits on each running QEMU process handle and wakes
the poll the moment the process exits; a PID is only waited on while it
names qemu-system-x86_64.exe, so a reused PID is not mistaken for the VM.

Measured with macos-tahoe and QMP quit: the window got the state change
51 ms after QEMU exited. Off Windows the steady poll remains.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 00:14:37 -07:00

78 lines
2.1 KiB
Go

//go:build windows
package host
import (
"context"
"errors"
"os/exec"
"testing"
"time"
)
func startPing(t *testing.T, pings string) *exec.Cmd {
t.Helper()
cmd := exec.Command("ping", "-n", pings, "127.0.0.1")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = cmd.Process.Kill(); _ = cmd.Wait() })
return cmd
}
func TestWaitProcessExitReturnsWhenTheProcessEnds(t *testing.T) {
cmd := startPing(t, "30")
pid := uint32(cmd.Process.Pid)
done := make(chan error, 1)
go func() { done <- WaitProcessExit(context.Background(), pid, "ping.exe") }()
time.Sleep(300 * time.Millisecond)
killedAt := time.Now()
if err := cmd.Process.Kill(); err != nil {
t.Fatal(err)
}
select {
case err := <-done:
if err != nil {
t.Fatalf("WaitProcessExit = %v", err)
}
// The exit is signalled, not found by the next wait slice.
if d := time.Since(killedAt); d > 500*time.Millisecond {
t.Fatalf("noticed the exit after %v", d)
}
case <-time.After(5 * time.Second):
t.Fatal("WaitProcessExit did not return after the process was killed")
}
}
func TestWaitProcessExitStopsWithContext(t *testing.T) {
cmd := startPing(t, "30")
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
err := WaitProcessExit(ctx, uint32(cmd.Process.Pid), "")
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("err = %v, want deadline exceeded", err)
}
}
func TestWaitProcessExitChecksTheImage(t *testing.T) {
cmd := startPing(t, "30")
err := WaitProcessExit(context.Background(), uint32(cmd.Process.Pid), "qemu-system-x86_64.exe")
if !errors.Is(err, ErrOtherProcess) {
t.Fatalf("err = %v, want ErrOtherProcess", err)
}
}
func TestWaitProcessExitOnAGoneProcess(t *testing.T) {
cmd := exec.Command("cmd", "/c", "exit")
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
// Its PID is free (or, rarely, reused by something that is not ping).
if err := WaitProcessExit(ctx, uint32(cmd.Process.Pid), "ping.exe"); err != nil && !errors.Is(err, ErrOtherProcess) {
t.Fatalf("err = %v", err)
}
}