From 0122c77bf14c9fed5f3fa395d9185b390471c785 Mon Sep 17 00:00:00 2001 From: Dov Reshef Date: Sat, 26 Aug 2017 19:44:51 +0300 Subject: [PATCH] use dep tool to manage dependencies & add the ability to kill cmd --- .gitignore | 1 + Gopkg.lock | 15 ++++++++++++ Gopkg.toml | 26 +++++++++++++++++++++ cmd.go | 20 ++++++++++++++-- cmd_test.go | 11 +++++---- vendor/github.com/go-test/deep/.travis.yml | 9 +++++++ vendor/github.com/go-test/deep/README.md | 2 +- vendor/github.com/go-test/deep/deep.go | 13 ++++++++++- vendor/github.com/go-test/deep/deep_test.go | 22 +++++++++++++++++ 9 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml diff --git a/.gitignore b/.gitignore index 53f12f0..3958e0b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.swp *.out +**/.idea \ No newline at end of file diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..5325a42 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,15 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + branch = "master" + name = "github.com/go-test/deep" + packages = ["."] + revision = "f49763a6ea0a91026be26f8213bebee726b4185f" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "bc476f93fa2a6cdf14f39d2e36e3141eec0a5fb13ef8fc74de3947e03ec23ab8" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..4c4fc26 --- /dev/null +++ b/Gopkg.toml @@ -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" diff --git a/cmd.go b/cmd.go index 6b898b5..2432d73 100644 --- a/cmd.go +++ b/cmd.go @@ -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() @@ -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 diff --git a/cmd_test.go b/cmd_test.go index 6e49e37..c70e9dd 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -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) { @@ -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) } @@ -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) } @@ -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) } @@ -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) } } diff --git a/vendor/github.com/go-test/deep/.travis.yml b/vendor/github.com/go-test/deep/.travis.yml index 72a70e7..9909ccf 100644 --- a/vendor/github.com/go-test/deep/.travis.yml +++ b/vendor/github.com/go-test/deep/.travis.yml @@ -1,4 +1,13 @@ language: go + go: - 1.6 - 1.7 + - 1.8 + +before_install: + - go get github.com/mattn/goveralls + - go get golang.org/x/tools/cover + +script: + - $HOME/gopath/bin/goveralls -service=travis-ci diff --git a/vendor/github.com/go-test/deep/README.md b/vendor/github.com/go-test/deep/README.md index 05df37b..3b78eac 100644 --- a/vendor/github.com/go-test/deep/README.md +++ b/vendor/github.com/go-test/deep/README.md @@ -1,6 +1,6 @@ # Deep Variable Equality for Humans -[![Go Report Card](https://goreportcard.com/badge/github.com/go-test/deep)](https://goreportcard.com/report/github.com/go-test/deep) [![Build Status](https://travis-ci.org/go-test/deep.svg?branch=master)](https://travis-ci.org/go-test/deep) [![GoDoc](https://godoc.org/github.com/go-test/deep?status.svg)](https://godoc.org/github.com/go-test/deep) +[![Go Report Card](https://goreportcard.com/badge/github.com/go-test/deep)](https://goreportcard.com/report/github.com/go-test/deep) [![Build Status](https://travis-ci.org/go-test/deep.svg?branch=master)](https://travis-ci.org/go-test/deep) [![Coverage Status](https://coveralls.io/repos/github/go-test/deep/badge.svg?branch=master)](https://coveralls.io/github/go-test/deep?branch=master) [![GoDoc](https://godoc.org/github.com/go-test/deep?status.svg)](https://godoc.org/github.com/go-test/deep) This package provides a single function: `deep.Equal`. It's like [reflect.DeepEqual](http://golang.org/pkg/reflect/#DeepEqual) but much friendlier to humans (or any sentient being) for two reason: diff --git a/vendor/github.com/go-test/deep/deep.go b/vendor/github.com/go-test/deep/deep.go index 5b98c0d..d0466ec 100644 --- a/vendor/github.com/go-test/deep/deep.go +++ b/vendor/github.com/go-test/deep/deep.go @@ -1,5 +1,5 @@ // Package deep provides function deep.Equal which is like reflect.DeepEqual but -// retunrs a list of differences. This is helpful when comparing complex types +// returns a list of differences. This is helpful when comparing complex types // like structures and maps. package deep @@ -63,6 +63,17 @@ func Equal(a, b interface{}) []string { buff: []string{}, floatFormat: fmt.Sprintf("%%.%df", FloatPrecision), } + if a == nil && b == nil { + return nil + } else if a == nil && b != nil { + c.saveDiff(b, "") + } else if a != nil && b == nil { + c.saveDiff(a, "") + } + if len(c.diff) > 0 { + return c.diff + } + c.equals(aVal, bVal, 0) if len(c.diff) > 0 { return c.diff // diffs diff --git a/vendor/github.com/go-test/deep/deep_test.go b/vendor/github.com/go-test/deep/deep_test.go index 57044db..87135a2 100644 --- a/vendor/github.com/go-test/deep/deep_test.go +++ b/vendor/github.com/go-test/deep/deep_test.go @@ -690,3 +690,25 @@ func TestError(t *testing.T) { t.Errorf("got '%s', expected 'Error: *errors.errorString != '", diff[0]) } } + +func TestNil(t *testing.T) { + type student struct { + name string + age int + } + + mark := student{"mark", 10} + var someNilThing interface{} = nil + diff := deep.Equal(someNilThing, mark) + if diff == nil { + t.Error("Nil value to comparision should not be equal") + } + diff = deep.Equal(mark, someNilThing) + if diff == nil { + t.Error("Nil value to comparision should not be equal") + } + diff = deep.Equal(someNilThing, someNilThing) + if diff != nil { + t.Error("Nil value to comparision should not be equal") + } +}