//go:build windows package main import ( "os" "path/filepath" "strings" "testing" "time" ) func TestNewLaunchClearsLogAndPreviousArchive(t *testing.T) { path := filepath.Join(t.TempDir(), "autopriority.log") for _, p := range []string{path, path + ".1"} { if err := os.WriteFile(p, []byte("PREVIOUS SESSION\n"), 0600); err != nil { t.Fatal(err) } } w, err := openRotatingLog(path, 256) if err != nil { t.Fatal(err) } defer w.Close() data, err := os.ReadFile(path) if err != nil || len(data) != 0 { t.Fatal("new session retained the previous log") } if _, err = os.Stat(path + ".1"); !os.IsNotExist(err) { t.Fatal("previous-session archive retained") } if err = w.Write("CURRENT SESSION\n"); err != nil { t.Fatal(err) } if err = w.Close(); err != nil { t.Fatal(err) } data, err = os.ReadFile(path) if err != nil || string(data) != "CURRENT SESSION\n" { t.Fatal("wrong current-session log") } } func TestDeniedProcessNotRetriedUntilNewInstance(t *testing.T) { f := newFake() p := f.add(10, 150, PriorityClassNormal) p.writable = false c := testController(f) defer c.Shutdown() if r := c.Step(); len(r.Errors) != 1 { t.Fatal("missing initial denial") } p.writable = true c.now = func() time.Time { return time.Unix(100000, 0) } if r := c.Step(); len(r.Errors) != 0 { t.Fatal("repeated denial") } assertPriority(t, p, PriorityClassNormal) p.info.Prio = PriorityClassBelowNormal p.info.RSS = 250 c.Step() assertPriority(t, p, PriorityClassBelowNormal) if p.writes != 0 { t.Fatal("blocked instance was written again") } p.alive = false replacement := f.add(10, 150, PriorityClassNormal) delete(f.procs, replacement.info.Key) replacement.info.Key.Created++ f.procs[replacement.info.Key] = replacement c.Step() assertPriority(t, replacement, PriorityClassHigh) c.Shutdown() assertPriority(t, replacement, PriorityClassNormal) assertClosed(t, f) } func TestErrorsAreOneSeparateTableWithoutRetryLines(t *testing.T) { f := newFake() a := f.add(4, 150, PriorityClassNormal) a.info.Name = "System" a.writable = false b := f.add(148, 150, PriorityClassNormal) b.info.Name = "Registry" b.writable = false c := testController(f) defer c.Shutdown() text := formatReport(c.Step(), time.Unix(0, 0)) if !strings.Contains(text, "Описание ошибки") || strings.Contains(text, "ОШИБКА:") || strings.Contains(text, "повтор") { t.Fatal("errors must be a table without retry lines", text) } for _, name := range []string{"System", "Registry"} { if strings.Count(text, name) != 1 { t.Fatal("duplicated or missing process", name, text) } for _, line := range strings.Split(text, "\n") { if strings.Contains(line, name) && !strings.HasPrefix(line, "|") { t.Fatal("process outside error table", line) } } } }