Files
autoPriority/app_test.go
T

85 lines
2.4 KiB
Go

//go:build windows
package main
import (
"context"
"errors"
"flag"
"io"
"syscall"
"testing"
"time"
)
func TestOptionsValidation(t *testing.T) {
for _, args := range [][]string{{"-mem=0"}, {"-mem=1.5G"}, {"-game-mem=128M"}, {"-interval=bad"}, {"extra"}, {"-unknown"}} {
if _, err := readOptions(args, io.Discard); err == nil {
t.Fatalf("accepted %v", args)
}
}
o, err := readOptions([]string{"-interval=1ms", "-game-mem=0"}, io.Discard)
if err != nil || o.mem != 512<<20 || o.gameMem != 0 || o.interval != 10*time.Second {
t.Fatal(o, err)
}
if _, err = readOptions([]string{"-h"}, io.Discard); !errors.Is(err, flag.ErrHelp) {
t.Fatal(err)
}
}
func TestRunControllerCancellationRestores(t *testing.T) {
f := newFake()
p := f.add(10, 150, PriorityClassBelowNormal)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
calls := 0
err := runController(ctx, options{mem: 100, gameMem: 200, interval: time.Minute}, f, func(scanReport) error {
calls++
if calls == 1 {
assertPriority(t, p, PriorityClassHigh)
cancel()
}
return nil
})
if err != nil {
t.Fatal(err)
}
assertPriority(t, p, PriorityClassBelowNormal)
assertClosed(t, f)
if calls != 2 {
t.Fatal("shutdown report missing")
}
}
func TestRunControllerLogFailureStillRestores(t *testing.T) {
f := newFake()
p := f.add(10, 150, PriorityClassNormal)
sentinel := errors.New("disk failure")
err := runController(context.Background(), options{mem: 100, interval: time.Minute}, f, func(scanReport) error { return sentinel })
if !errors.Is(err, sentinel) {
t.Fatal(err)
}
assertPriority(t, p, PriorityClassNormal)
assertClosed(t, f)
}
func TestCancelledBeforeStartupDoesNotScan(t *testing.T) {
f := newFake()
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := runController(ctx, options{mem: 100, interval: time.Minute}, f, func(scanReport) error { return nil }); err != nil {
t.Fatal(err)
}
if f.snapshots != 0 {
t.Fatal("cancelled startup still scanned processes")
}
}
func TestWaitResultContract(t *testing.T) {
for _, item := range []struct {
code uintptr
alive, bad bool
}{{0, false, false}, {0x102, true, false}, {0xffffffff, false, true}, {0x80, false, true}} {
alive, err := decodeWaitResult(item.code, syscall.Errno(6))
if alive != item.alive || (err != nil) != item.bad {
t.Fatalf("bad wait decoding for %#x", item.code)
}
}
}