143 lines
4.0 KiB
Go
143 lines
4.0 KiB
Go
package app
|
|
|
|
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 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[name] = ch
|
|
typing.lastHintAt[name] = time.Now()
|
|
typing.mu.Unlock()
|
|
go func() {
|
|
a.runRemoteTextQueue(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)
|
|
}
|
|
}
|