Skip to content

Commit 966b441

Browse files
committed
test: fix flaky TestRunAttachTermination
This patch fixes the `TestRunAttachTermination` flaky runs. It seems like we weren't halting on the `waitFunc` so if the process was fast enough to setup the signal handler and execute `waitExitOrRemoved`. We now instead wait for the `killCh` channel to close inside the mocked `waitFunc`. Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
1 parent 2d74733 commit 966b441

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

cli/command/container/run_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ func TestRunAttachTermination(t *testing.T) {
147147
_ = p.Close()
148148
}()
149149

150-
var conn net.Conn
151150
killCh := make(chan struct{})
152151
attachCh := make(chan struct{})
153152
fakeCLI := test.NewFakeCli(&fakeClient{
@@ -156,13 +155,14 @@ func TestRunAttachTermination(t *testing.T) {
156155
ID: "id",
157156
}, nil
158157
},
159-
containerKillFunc: func(ctx context.Context, containerID, signal string) error {
160-
killCh <- struct{}{}
158+
containerKillFunc: func(ctx context.Context, containerID, sig string) error {
159+
if sig == "TERM" {
160+
close(killCh)
161+
}
161162
return nil
162163
},
163164
containerAttachFunc: func(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error) {
164165
server, client := net.Pipe()
165-
conn = server
166166
t.Cleanup(func() {
167167
_ = server.Close()
168168
})
@@ -172,7 +172,7 @@ func TestRunAttachTermination(t *testing.T) {
172172
waitFunc: func(_ string) (<-chan container.WaitResponse, <-chan error) {
173173
responseChan := make(chan container.WaitResponse, 1)
174174
errChan := make(chan error)
175-
175+
<-killCh
176176
responseChan <- container.WaitResponse{
177177
StatusCode: 130,
178178
}
@@ -201,9 +201,7 @@ func TestRunAttachTermination(t *testing.T) {
201201
case <-attachCh:
202202
}
203203

204-
assert.NilError(t, syscall.Kill(syscall.Getpid(), syscall.SIGINT))
205-
// end stream from "container" so that we'll detach
206-
conn.Close()
204+
assert.NilError(t, syscall.Kill(syscall.Getpid(), syscall.SIGTERM))
207205

208206
select {
209207
case <-killCh:

0 commit comments

Comments
 (0)