Files
Reims-Go/app_poll_test.go
T
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

86 lines
2.4 KiB
Go

package main
import (
"context"
"runtime"
"testing"
"time"
"github.com/Nwflower/reims-go/internal/driver/reims"
)
func TestPolledVMsReusesFreshPoll(t *testing.T) {
pid := 42
a := &App{pollWake: make(chan struct{}, 1)}
a.lastVMs = []reims.VMListItem{{Name: "m0", Dir: `D:\vms\m0`, PID: &pid, State: "running"}}
a.lastVMsAt = time.Now()
vm, ok := a.polledVM("m0")
if !ok || vm.State != "running" || vm.Dir != `D:\vms\m0` || *vm.PID != 42 {
t.Fatalf("polledVM = %+v, %v", vm, ok)
}
if _, ok := a.polledVM("missing"); ok {
t.Fatal("unknown VM should not be found")
}
vms, _ := a.polledVMs(context.Background())
vms[0].State = "stopped"
if a.lastVMs[0].State != "running" {
t.Fatal("polledVMs must return a copy, not the cached slice")
}
}
func TestWatchExitsTracksRunningPIDs(t *testing.T) {
gone := 999999 // no such process: WaitProcessExit reports it exited at once
stopped := reims.VMListItem{Name: "m1", State: "stopped"}
running := reims.VMListItem{Name: "m0", State: "running", PID: &gone}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
exits := make(chan struct{}, 4)
watched := map[int]bool{}
watchExits(ctx, []reims.VMListItem{running, stopped}, watched, func() { exits <- struct{}{} })
watchExits(ctx, []reims.VMListItem{running, stopped}, watched, func() { exits <- struct{}{} })
if len(watched) != 1 || !watched[gone] {
t.Fatalf("watched = %v, want only %d", watched, gone)
}
if runtime.GOOS == "windows" {
select {
case <-exits:
case <-time.After(3 * time.Second):
t.Fatal("onExit not called for an exited process")
}
select {
case <-exits:
t.Fatal("a PID already watched must not get a second waiter")
case <-time.After(300 * time.Millisecond):
}
}
running.State = "stopped"
watchExits(ctx, []reims.VMListItem{running, stopped}, watched, func() {})
if len(watched) != 0 {
t.Fatalf("watched = %v, want empty once the VM stopped", watched)
}
}
func TestWakePollMarksStaleAndWakesOnce(t *testing.T) {
a := &App{pollWake: make(chan struct{}, 1)}
a.lastVMsAt = time.Now()
a.wakePoll()
a.wakePoll() // a second wake while one is pending must not block
if !a.lastVMsAt.IsZero() {
t.Fatal("wakePoll should mark the polled list stale")
}
select {
case <-a.pollWake:
default:
t.Fatal("wakePoll should signal pollVMs")
}
select {
case <-a.pollWake:
t.Fatal("pending wakes should coalesce into one")
default:
}
}