Add files via upload

This commit is contained in:
公明
2026-06-26 23:11:52 +08:00
committed by GitHub
parent e537236bf3
commit c91806c0c4
5 changed files with 131 additions and 11 deletions
+32
View File
@@ -2,6 +2,8 @@ package security
import (
"context"
"os/exec"
"runtime"
"strings"
"testing"
"time"
@@ -147,3 +149,33 @@ func indexOf(slice []string, s string) int {
}
return -1
}
// TestCombinedOutputCancellable_ContextCancelKillsTree 验证 ctx 取消时能在数秒内结束(杀进程组,非挂死)。
func TestCombinedOutputCancellable_ContextCancelKillsTree(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("unix process group kill")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := exec.CommandContext(ctx, "sh", "-c", "sleep 300")
ConfigureShellCmdForAgentExecute(cmd)
done := make(chan error, 1)
go func() {
_, err := combinedOutputCancellable(ctx, cmd)
done <- err
}()
time.Sleep(150 * time.Millisecond)
cancel()
select {
case err := <-done:
if err == nil {
t.Fatal("expected context cancel error")
}
case <-time.After(5 * time.Second):
t.Fatal("combinedOutputCancellable did not return within 5s after context cancel")
}
}