|
| 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