The poll loop was one 290-line function that did everything that follows the VM list. Its steps are now separate and recording a listing has a test. The install guide part moved next to the rest of the guide code. Running process IDs and capabilities are read through one method each. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
182 lines
5.8 KiB
Go
182 lines
5.8 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"runtime"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/macos/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 TestPrunePinnedPIDs(t *testing.T) {
|
|
runningPID, stoppedPID := 42, 43
|
|
pinned := map[int]bool{runningPID: true, stoppedPID: true, 44: true}
|
|
prunePinnedPIDs(pinned, []reims.VMListItem{
|
|
{Name: "running", State: "running", PID: &runningPID},
|
|
{Name: "stopped", State: "stopped", PID: &stoppedPID},
|
|
})
|
|
if len(pinned) != 1 || !pinned[runningPID] {
|
|
t.Fatalf("pinned = %v, want only running PID %d", pinned, runningPID)
|
|
}
|
|
}
|
|
|
|
func TestReleaseVMTypingDrainsAndRemovesQueue(t *testing.T) {
|
|
const name = "typing-release-test"
|
|
done := make(chan struct{})
|
|
a := &App{}
|
|
typing.mu.Lock()
|
|
ch := make(chan remoteTextItem)
|
|
typing.remoteQueues[vmKey(name)] = ch
|
|
typing.lastHintAt[vmKey(name)] = time.Now()
|
|
typing.mu.Unlock()
|
|
go func() {
|
|
a.runRemoteTextQueue(vmKey(name), name, ch)
|
|
close(done)
|
|
}()
|
|
releaseVMTyping(name)
|
|
select {
|
|
case <-done:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("remote typing worker did not exit")
|
|
}
|
|
typing.mu.Lock()
|
|
defer typing.mu.Unlock()
|
|
if len(typing.remoteQueues) != 0 || len(typing.lastHintAt) != 0 || len(typing.closing) != 0 {
|
|
t.Fatal("typing state remains after VM release")
|
|
}
|
|
}
|
|
|
|
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:
|
|
}
|
|
}
|
|
|
|
func TestCachedVMPID(t *testing.T) {
|
|
runningPID, stoppedPID := 4784, 9244
|
|
a := &App{
|
|
lastVMs: []reims.VMListItem{
|
|
{Name: "running-vm", State: "running", PID: &runningPID},
|
|
{Name: "stopped-vm", State: "stopped", PID: &stoppedPID},
|
|
},
|
|
}
|
|
if got := a.cachedVMPID("running-vm"); got != runningPID {
|
|
t.Fatalf("cachedVMPID(running-vm) = %d, want %d", got, runningPID)
|
|
}
|
|
if got := a.cachedVMPID("stopped-vm"); got != 0 {
|
|
t.Fatalf("cachedVMPID(stopped-vm) = %d, want 0", got)
|
|
}
|
|
if got := a.cachedVMPID("missing-vm"); got != 0 {
|
|
t.Fatalf("cachedVMPID(missing-vm) = %d, want 0", got)
|
|
}
|
|
}
|
|
|
|
func TestRecordVMListReportsWhatChanged(t *testing.T) {
|
|
mac := driver.BuiltinReimsMacOSDriverID
|
|
item := func(name, state string) driver.VMListItem {
|
|
return driver.VMListItem{DriverID: mac, Name: name, State: state}
|
|
}
|
|
key := func(name string) string { return vmKeyForDriver(mac, name) }
|
|
a := &App{
|
|
lastVMs: []driver.VMListItem{item("gone", "running"), item("crashed", "running"), item("rebooting", "running"), item("kept", "running")},
|
|
lastVMListErr: errors.New("reims-ctl failed"),
|
|
transitions: map[string]string{key("rebooting"): "starting", key("new"): "starting"},
|
|
pendingChanges: map[string][]PendingChange{},
|
|
diskPaths: map[string]cachedDiskPath{key("old-disk"): {}, key("kept"): {}},
|
|
}
|
|
change := a.recordVMList([]driver.VMListItem{item("crashed", "stopped"), item("rebooting", "stopped"), item("kept", "running"), item("new", "running")})
|
|
|
|
if !change.recovered || a.lastVMListErr != nil {
|
|
t.Error("a good listing after a failed one must count as recovered")
|
|
}
|
|
if len(change.removed) != 2 {
|
|
t.Errorf("removed = %v, want gone and old-disk", change.removed)
|
|
}
|
|
if _, ok := a.diskPaths[key("old-disk")]; ok || len(a.diskPaths) != 1 {
|
|
t.Errorf("diskPaths = %v, want only kept", a.diskPaths)
|
|
}
|
|
// "rebooting" is down only because it is being started again.
|
|
if !slices.Equal(change.stoppedBridges, []string{"crashed"}) {
|
|
t.Errorf("stoppedBridges = %v, want crashed", change.stoppedBridges)
|
|
}
|
|
if _, ok := a.transitions[key("new")]; ok {
|
|
t.Error("a running VM must not stay in the starting transition")
|
|
}
|
|
if a.transitions[key("rebooting")] != "starting" {
|
|
t.Error("an unfinished transition must be kept")
|
|
}
|
|
}
|