Skip to content
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.swp
*.out
**/.idea
15 changes: 15 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
branch = "master"
name = "github.com/go-test/deep"
20 changes: 18 additions & 2 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (c *Cmd) Start() <-chan Status {
// Stop is idempotent. An error should only be returned in the rare case that
// Stop is called immediately after the command ends but before Start can
// update its internal state.
func (c *Cmd) Stop() error {
func (c *Cmd) Stop(waitTime time.Duration) error {
c.Lock()
defer c.Unlock()

Expand All @@ -120,7 +120,23 @@ func (c *Cmd) Stop() error {
// Signal the process group (-pid), not just the process, so that the process
// and all its children are signaled. Else, child procs can keep running and
// keep the stdout/stderr fd open and cause cmd.Wait to hang.
return syscall.Kill(-c.status.PID, syscall.SIGTERM)
stoppedChannel := make(chan error)
timeoutChannel := time.After(waitTime)
// the process might ignore sigterm, so we make a best effort request for the process
// to terminate. If it does not terminate in waitTime, we'll kill it
go func() {
err := syscall.Kill(-c.status.PID, syscall.SIGTERM)
stoppedChannel <- err
}()
for {
select {
case err := <- stoppedChannel:
return err
case <-timeoutChannel:
return syscall.Kill(-c.status.PID, syscall.SIGKILL)
}
}
return nil
}

// Status returns the Status of the command at any time. It is safe to call
Expand Down
11 changes: 6 additions & 5 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"testing"
"time"

"github.com/go-cmd/cmd"

"github.com/go-test/deep"
"github.com/go-cmd/cmd"
)

func TestCmdOK(t *testing.T) {
Expand Down Expand Up @@ -87,7 +88,7 @@ func TestCmdStop(t *testing.T) {
time.Sleep(1 * time.Second)

// Kill the process
err := p.Stop()
err := p.Stop(500 * time.Millisecond)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -131,7 +132,7 @@ func TestCmdStop(t *testing.T) {
}

// Stop should be idempotent
err = p.Stop()
err = p.Stop(500 * time.Millisecond)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -162,7 +163,7 @@ func TestCmdNotStarted(t *testing.T) {
t.Error(diffs)
}

err := p.Stop()
err := p.Stop(500 * time.Millisecond)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -226,7 +227,7 @@ func TestCmdOutput(t *testing.T) {
}

// Kill the process
if err := p.Stop(); err != nil {
if err := p.Stop(500 * time.Millisecond); err != nil {
t.Error(err)
}
}
Expand Down
9 changes: 9 additions & 0 deletions vendor/github.com/go-test/deep/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/go-test/deep/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion vendor/github.com/go-test/deep/deep.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/go-test/deep/deep_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.