mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-08 08:10:30 +00:00
test: avoid redundant fixture work
Reuse immutable fixtures, combine Git queries and avoid large data copies to reduce test overhead while retaining behavior checks. Assisted-by: Codex:GPT-6
This commit is contained in:
28 files changed
+261
-291
No files matched your search
@@ -5,7 +5,6 @@ package process
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/sys/windows"
|
||||
@@ -25,6 +24,23 @@ func processAlive(pid int) bool {
|
||||
return code == stillActive
|
||||
}
|
||||
|
||||
func newProcessExitWaiter(t *testing.T, pids ...int) func() {
|
||||
t.Helper()
|
||||
handles := make([]windows.Handle, 0, len(pids))
|
||||
for _, pid := range pids {
|
||||
handle, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid))
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, windows.CloseHandle(handle)) })
|
||||
handles = append(handles, handle)
|
||||
}
|
||||
return func() {
|
||||
t.Helper()
|
||||
result, err := windows.WaitForMultipleObjects(handles, true, 20000)
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, windows.WAIT_OBJECT_0, result, "processes should exit: %v", pids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKillerKillsTree(t *testing.T) {
|
||||
cmd, ready := newTestProcess(t, "parent", false)
|
||||
require.NoError(t, cmd.Start())
|
||||
@@ -39,9 +55,7 @@ func TestKillerKillsTree(t *testing.T) {
|
||||
childPID := ready()
|
||||
require.True(t, processAlive(childPID), "child process should start")
|
||||
|
||||
waitForExit := newProcessExitWaiter(t, cmd.Process.Pid, childPID)
|
||||
require.NoError(t, killer.Kill())
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
return !processAlive(cmd.Process.Pid) && !processAlive(childPID)
|
||||
}, 20*time.Second, 200*time.Millisecond, "parent and child should both be terminated")
|
||||
waitForExit()
|
||||
}
|
||||
Reference in new issue
Block a user