Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use chan only to notify command is done #5244

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions pkg/app/launcher/cmd/launcher/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"os"
"os/exec"
"path/filepath"
"sync/atomic"
"syscall"
"time"

Expand All @@ -29,7 +30,8 @@

type command struct {
cmd *exec.Cmd
stoppedCh chan error
stoppedCh chan struct{}
result atomic.Pointer[error]
}

func (c *command) IsRunning() bool {
Expand All @@ -50,9 +52,16 @@
select {
case <-timer.C:
c.cmd.Process.Kill()
return <-c.stoppedCh
case err := <-c.stoppedCh:
return err
<-c.stoppedCh
if perr := c.result.Load(); perr != nil {
return *perr
}
return nil

Check warning on line 59 in pkg/app/launcher/cmd/launcher/binary.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/launcher/cmd/launcher/binary.go#L59

Added line #L59 was not covered by tests
case <-c.stoppedCh:
if perr := c.result.Load(); perr != nil {
return *perr
}
return nil

Check warning on line 64 in pkg/app/launcher/cmd/launcher/binary.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/launcher/cmd/launcher/binary.go#L64

Added line #L64 was not covered by tests
}
}

Expand All @@ -68,11 +77,12 @@

c := &command{
cmd: cmd,
stoppedCh: make(chan error, 1),
stoppedCh: make(chan struct{}),
result: atomic.Pointer[error]{},
}
go func() {
err := cmd.Wait()
c.stoppedCh <- err
c.result.Store(&err)
close(c.stoppedCh)
}()

Expand Down
32 changes: 32 additions & 0 deletions pkg/app/launcher/cmd/launcher/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package launcher

import (
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -49,3 +50,34 @@ func TestGracefulStopCommand(t *testing.T) {
})
}
}

func TestGracefulStopCommandResult(t *testing.T) {
testcases := []struct {
name string
exitCode int
assertion assert.ErrorAssertionFunc
}{
{
name: "successfully exit",
exitCode: 0,
assertion: assert.NoError,
},
{
name: "exit with an error",
exitCode: 1,
assertion: assert.Error,
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
cmd, err := runBinary("sh", []string{"-c", "exit " + strconv.Itoa(tc.exitCode)})
require.NoError(t, err)
require.NotNil(t, cmd)

time.Sleep(100 * time.Millisecond) // to avoid GracefulStop executed before the command exits
tc.assertion(t, cmd.GracefulStop(time.Second))
assert.False(t, cmd.IsRunning())
})
}
}
Loading