Skip to content

Commit aa4bada

Browse files
authored
test: correct iteration over pids in SUT (#2693)
<!-- Please read and fill out this form before submitting your PR. Please make sure you have reviewed our contributors guide before submitting your first PR. NOTE: PR titles should follow semantic commits: https://www.conventionalcommits.org/en/v1.0.0/ --> ## Overview NOTE: the fix was actually included in the e2e refactor, this PR is just adding some extra tests to cover it. I found a small bug in the `SystemUnderTest` type in which iteration was happening over indices instead of process IDs, so when Shutdown was called, it was not actually finding the process. The fix was ```diff - for pid := range pids { + for _, pid := range pids { p, err := os.FindProcess(pid) if err != nil { continue } if !yield(p) { break } } ``` in `iterProcessesByCmd` <!-- Please provide an explanation of the PR, including the appropriate context, background, goal, and rationale. If there is an issue with this information, please provide a tl;dr and link the issue. Ex: Closes #<issue number> -->
1 parent 686f465 commit aa4bada

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

test/e2e/sut_helper_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//go:build e2e
2+
3+
package e2e
4+
5+
import (
6+
"testing"
7+
"time"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// TestIterProcessesByCmd tests that iterProcessesByCmd correctly iterates over actual PIDs.
13+
func TestIterProcessesByCmd(t *testing.T) {
14+
sut := NewSystemUnderTest(t)
15+
16+
// simulate some processes being tracked
17+
testPIDs := []int{12345, 67890, 99999}
18+
cmdName := "testcmd"
19+
20+
// manually add PIDs to the tracking maps.
21+
for _, pid := range testPIDs {
22+
sut.pids[pid] = struct{}{}
23+
sut.cmdToPids[cmdName] = append(sut.cmdToPids[cmdName], pid)
24+
}
25+
26+
var iteratedPIDs []int
27+
for process := range sut.iterProcessesByCmd(cmdName) {
28+
iteratedPIDs = append(iteratedPIDs, process.Pid)
29+
}
30+
31+
// The iterator should return the actual PIDs
32+
require.ElementsMatch(t, testPIDs, iteratedPIDs,
33+
"iterProcessesByCmd should iterate over actual PIDs")
34+
}
35+
36+
// TestShutdownByCmd tests that ShutdownByCmd can properly find and signal processes.
37+
// This test verifies that the fix allows the shutdown mechanism to work correctly.
38+
func TestShutdownByCmd(t *testing.T) {
39+
sut := NewSystemUnderTest(t)
40+
41+
sut.ExecCmd("sleep")
42+
43+
require.Eventually(t, func() bool {
44+
return sut.HasProcess("sleep")
45+
}, time.Millisecond*100, time.Millisecond*10, "process should be tracked after starting")
46+
47+
sut.ShutdownByCmd("sleep")
48+
49+
require.Eventually(t, func() bool {
50+
return !sut.HasProcess("sleep")
51+
}, time.Millisecond*100, time.Millisecond*10, "process should be removed from tracking after shutdown")
52+
}

0 commit comments

Comments
 (0)