@@ -6,10 +6,12 @@ import (
6
6
"fmt"
7
7
"os"
8
8
"os/exec"
9
+ "path/filepath"
9
10
"strconv"
10
11
"strings"
11
12
"syscall"
12
13
"testing"
14
+ "time"
13
15
14
16
"github.com/stretchr/testify/require"
15
17
"golang.org/x/sys/unix"
@@ -23,13 +25,14 @@ func TestCLI(t *testing.T) {
23
25
t .Parallel ()
24
26
25
27
ctx := testutil .Context (t , testutil .WaitMedium )
26
- cmd , dir := cmd (t , ctx )
28
+ cmd , path := cmd (ctx , t )
27
29
cmd .Env = append (cmd .Env , "CODER_PROC_NICE_SCORE=10" )
28
30
cmd .Env = append (cmd .Env , "CODER_PROC_OOM_SCORE=123" )
29
31
err := cmd .Start ()
30
32
require .NoError (t , err )
33
+ go cmd .Wait ()
31
34
32
- waitForSentinel (t , ctx , cmd , dir )
35
+ waitForSentinel (t , ctx , cmd , path )
33
36
requireOOMScore (t , cmd .Process .Pid , 123 )
34
37
requireNiceScore (t , cmd .Process .Pid , 10 )
35
38
})
@@ -52,27 +55,40 @@ func requireOOMScore(t *testing.T, pid int, expected int) {
52
55
require .Equal (t , strconv .Itoa (expected ), score )
53
56
}
54
57
55
- func waitForSentinel (t * testing.T , ctx context.Context , cmd * exec.Cmd , dir string ) {
58
+ func waitForSentinel (t * testing.T , ctx context.Context , cmd * exec.Cmd , path string ) {
56
59
t .Helper ()
57
60
58
- require .Eventually (t , func () bool {
59
- // Check if the process is still running.
61
+ ticker := time .NewTicker (testutil .IntervalFast )
62
+ defer ticker .Stop ()
63
+
64
+ // RequireEventually doesn't work well with require.NoError or similar require functions.
65
+ for {
60
66
err := cmd .Process .Signal (syscall .Signal (0 ))
61
67
require .NoError (t , err )
62
68
63
- _ , err = os .Stat (dir )
64
- return err == nil && ctx .Err () == nil
65
- }, testutil .WaitLong , testutil .IntervalFast )
69
+ _ , err = os .Stat (path )
70
+ if err == nil {
71
+ return
72
+ }
73
+
74
+ select {
75
+ case <- ticker .C :
76
+ case <- ctx .Done ():
77
+ return
78
+ }
79
+ }
66
80
}
67
81
68
- func cmd (t * testing. T , ctx context. Context , args ... string ) (* exec.Cmd , string ) {
69
- dir := ""
82
+ func cmd (ctx context. Context , t * testing. T , args ... string ) (* exec.Cmd , string ) {
83
+ file := ""
70
84
cmd := exec .Command (TestBin , args ... )
71
85
if len (args ) == 0 {
72
- dir = t .TempDir ()
86
+ dir := t .TempDir ()
87
+ file = filepath .Join (dir , uniqueFile (t ))
73
88
//nolint:gosec
74
- cmd = exec .CommandContext (ctx , TestBin , "sh" , "-c" , fmt .Sprintf ("touch %s && sleep 10m" , dir ))
89
+ cmd = exec .CommandContext (ctx , TestBin , "agent-exec" , " sh" , "-c" , fmt .Sprintf ("touch %s && sleep 10m" , file ))
75
90
}
91
+ cmd .Env = os .Environ ()
76
92
var buf bytes.Buffer
77
93
cmd .Stdout = & buf
78
94
cmd .Stderr = & buf
@@ -86,5 +102,9 @@ func cmd(t *testing.T, ctx context.Context, args ...string) (*exec.Cmd, string)
86
102
_ = cmd .Process .Kill ()
87
103
}
88
104
})
89
- return cmd , dir
105
+ return cmd , file
106
+ }
107
+
108
+ func uniqueFile (t * testing.T ) string {
109
+ return fmt .Sprintf ("%s-%d" , strings .ReplaceAll (t .Name (), "/" , "_" ), time .Now ().UnixNano ())
90
110
}
0 commit comments