Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1703e6d

Browse files
committed
fix test
1 parent b1cc536 commit 1703e6d

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

agent/reaper/reaper.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// to indicated that the process is a child as opposed to the reaper.
1414
// Since we are forkexec'ing we need to be able to differentiate between
1515
// the two to avoid fork bombing ourselves.
16-
const agentEnvMark = "CODER_AGENT"
16+
const agentEnvMark = "CODER_REAPER_AGENT"
1717

1818
// IsChild returns true if we're the forked process.
1919
func IsChild() bool {
@@ -29,6 +29,8 @@ func IsInitProcess() bool {
2929
// complications with spawning `exec.Commands` in the same process that
3030
// is reaping, we forkexec a child process. This prevents a race between
3131
// the reaper and an exec.Command waiting for its process to complete.
32+
// The provided 'pids' channel may be nil if the caller does not care about the
33+
// reaped children PIDs.
3234
func ForkReap(pids reap.PidCh) error {
3335
// Check if the process is the parent or the child.
3436
// If it's the child we want to skip attempting to reap.

agent/reaper/reaper_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package reaper_test
22

33
import (
4+
"fmt"
45
"os/exec"
56
"testing"
67
"time"
@@ -19,6 +20,9 @@ func TestReap(t *testing.T) {
1920
t.Skip("I'm a child!")
2021
}
2122

23+
// OK checks that's the reaper is successfully reaping
24+
// exited processes and passing the PIDs through the shared
25+
// channel.
2226
t.Run("OK", func(t *testing.T) {
2327
pids := make(reap.PidCh, 1)
2428
err := reaper.ForkReap(pids)
@@ -41,11 +45,14 @@ func TestReap(t *testing.T) {
4145
expectedPIDs := []int{cmd.Process.Pid, cmd2.Process.Pid}
4246

4347
deadline := time.NewTimer(time.Second * 5)
44-
select {
45-
case <-deadline.C:
46-
t.Fatalf("Timed out waiting for process")
47-
case pid := <-pids:
48-
require.Contains(t, expectedPIDs, pid)
48+
for i := 0; i < len(expectedPIDs); i++ {
49+
select {
50+
case <-deadline.C:
51+
t.Fatalf("Timed out waiting for process")
52+
case pid := <-pids:
53+
fmt.Println("pid: ", pid)
54+
require.Contains(t, expectedPIDs, pid)
55+
}
4956
}
5057
})
5158
}

0 commit comments

Comments
 (0)