|
| 1 | +package process |
| 2 | + |
| 3 | +import ( |
| 4 | + "os/exec" |
| 5 | + "runtime" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func startTestProcess(t *testing.T) *exec.Cmd { |
| 11 | + t.Helper() |
| 12 | + var cmd *exec.Cmd |
| 13 | + if runtime.GOOS == "windows" { |
| 14 | + cmd = getTestProcessCmd("timeout", "/t", "30") |
| 15 | + } else { |
| 16 | + cmd = getTestProcessCmd("sleep", "30") |
| 17 | + } |
| 18 | + err := cmd.Start() |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("Failed to start test process: %v", err) |
| 21 | + } |
| 22 | + return cmd |
| 23 | +} |
| 24 | + |
| 25 | +func TestKill_InvalidPID(t *testing.T) { |
| 26 | + err := Kill(0) |
| 27 | + if err == nil { |
| 28 | + t.Error("Expected an error for invalid PID 0, but got nil") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestKill_GracefulExitSuccess(t *testing.T) { |
| 33 | + cmd := startTestProcess(t) |
| 34 | + pid := cmd.Process.Pid |
| 35 | + |
| 36 | + // Ensure the process is running before trying to kill it. |
| 37 | + if !isProcessRunning(pid) { |
| 38 | + t.Fatalf("Test process with PID %d should be running but it's not", pid) |
| 39 | + } |
| 40 | + |
| 41 | + err := Kill(pid) |
| 42 | + if err != nil { |
| 43 | + t.Errorf("Kill should have succeeded gracefully, but got error: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Give it a moment to die, then check. |
| 47 | + time.Sleep(1 * time.Second) |
| 48 | + |
| 49 | + if isProcessRunning(pid) { |
| 50 | + if ok := forceKill(pid); !ok { |
| 51 | + t.Errorf("Process %d should have been terminated gracefully, but it is still running.", pid) |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestKill_ForceKillAfterGracefulTimeout(t *testing.T) { |
| 57 | + cmd := startTestProcess(t) |
| 58 | + pid := cmd.Process.Pid |
| 59 | + |
| 60 | + // To test the timeout path, we'll let Kill do its thing. |
| 61 | + // The 5-second wait in Kill should expire, triggering forceKill. |
| 62 | + err := Kill(pid) |
| 63 | + if err != nil { |
| 64 | + t.Errorf("Kill should have eventually succeeded with force kill, but got error: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + // We wait slightly longer than the internal timeout of Kill() |
| 68 | + time.Sleep(6 * time.Second) |
| 69 | + |
| 70 | + if isProcessRunning(pid) { |
| 71 | + if err = cmd.Process.Kill(); err != nil { |
| 72 | + t.Errorf("Process %d should have been force-killed, but it is still running.", pid) |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestKill_NonExistentProcess(t *testing.T) { |
| 78 | + pid := 999999 |
| 79 | + for isProcessRunning(pid) { |
| 80 | + pid++ |
| 81 | + } |
| 82 | + |
| 83 | + err := Kill(pid) |
| 84 | + if err != nil { |
| 85 | + t.Errorf("Killing a non-existent process should not return an error, but got: %v", err) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestKill_ForceKillFailure(t *testing.T) { |
| 90 | + // This scenario is hard to test reliably without special permissions. |
| 91 | + // For example, trying to kill a critical system process (PID 1 on Linux). |
| 92 | + // Such a test would be flaky and dangerous. |
| 93 | + // Instead, we will test the code path where forceKill *reports* failure, |
| 94 | + // but the process is actually already gone. |
| 95 | + |
| 96 | + // We can't easily mock `forceKill` to return false, so we'll test the |
| 97 | + // logic by killing a process and then immediately trying to kill it again. |
| 98 | + // The second kill attempt might fail on the `forceKill` call, but since |
| 99 | + // `isProcessRunning` will be false, `Kill` should return nil. |
| 100 | + cmd := startTestProcess(t) |
| 101 | + pid := cmd.Process.Pid |
| 102 | + |
| 103 | + // Kill it once for real |
| 104 | + forceKill(pid) |
| 105 | + time.Sleep(100 * time.Millisecond) // Let it die |
| 106 | + |
| 107 | + if isProcessRunning(pid) { |
| 108 | + t.Skip("Could not kill process for the first time, skipping test.") |
| 109 | + } |
| 110 | + |
| 111 | + // Now call the main Kill function on the already-dead process. |
| 112 | + // Internally, it might try graceful, then force, which might fail, |
| 113 | + // but the final check should show the process is not running. |
| 114 | + err := Kill(pid) |
| 115 | + if err != nil { |
| 116 | + t.Errorf("Expected nil error when Kill fails to force-kill an already-dead process, but got: %v", err) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestIsProcessRunning_False(t *testing.T) { |
| 121 | + pid := 999999 |
| 122 | + for isProcessRunning(pid) { // ensure PID is not running |
| 123 | + pid++ |
| 124 | + } |
| 125 | + if isProcessRunning(pid) { |
| 126 | + t.Errorf("isProcessRunning for non-existent PID %d should be false", pid) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func getTestProcessCmd(name string, arg ...string) *exec.Cmd { |
| 131 | + return exec.Command(name, arg...) |
| 132 | +} |
0 commit comments