aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/process/manager_test.go
diff options
context:
space:
mode:
author简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
committer简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
commitdd84b9d64fb98746a230cd24233ff50a562c39c9 (patch)
treeb583261ef00b3afe72ec4d6dacb31e57779a6faf /cli/internal/process/manager_test.go
parent0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff)
downloadHydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz
HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip
Diffstat (limited to 'cli/internal/process/manager_test.go')
-rw-r--r--cli/internal/process/manager_test.go94
1 files changed, 94 insertions, 0 deletions
diff --git a/cli/internal/process/manager_test.go b/cli/internal/process/manager_test.go
new file mode 100644
index 0000000..fb40ffa
--- /dev/null
+++ b/cli/internal/process/manager_test.go
@@ -0,0 +1,94 @@
+package process
+
+import (
+ "errors"
+ "os/exec"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/hashicorp/go-gatedio"
+ "github.com/hashicorp/go-hclog"
+)
+
+func newManager() *Manager {
+ return NewManager(hclog.Default())
+}
+
+func TestExec_simple(t *testing.T) {
+ mgr := newManager()
+
+ out := gatedio.NewByteBuffer()
+ cmd := exec.Command("env")
+ cmd.Stdout = out
+
+ err := mgr.Exec(cmd)
+ if err != nil {
+ t.Errorf("expected %q to be nil", err)
+ }
+
+ output := out.String()
+ if output == "" {
+ t.Error("expected output from running 'env', got empty string")
+ }
+}
+
+func TestClose(t *testing.T) {
+ mgr := newManager()
+
+ wg := sync.WaitGroup{}
+ tasks := 4
+ errors := make([]error, tasks)
+ start := time.Now()
+ for i := 0; i < tasks; i++ {
+ wg.Add(1)
+ go func(index int) {
+ cmd := exec.Command("sleep", "0.5")
+ err := mgr.Exec(cmd)
+ if err != nil {
+ errors[index] = err
+ }
+ wg.Done()
+ }(i)
+ }
+ // let processes kick off
+ time.Sleep(50 * time.Millisecond)
+ mgr.Close()
+ end := time.Now()
+ wg.Wait()
+ duration := end.Sub(start)
+ if duration >= 500*time.Millisecond {
+ t.Errorf("expected to close, total time was %q", duration)
+ }
+ for _, err := range errors {
+ if err != ErrClosing {
+ t.Errorf("expected manager closing error, found %q", err)
+ }
+ }
+}
+
+func TestClose_alreadyClosed(t *testing.T) {
+ mgr := newManager()
+ mgr.Close()
+
+ // repeated closing does not error
+ mgr.Close()
+
+ err := mgr.Exec(exec.Command("sleep", "1"))
+ if err != ErrClosing {
+ t.Errorf("expected manager closing error, found %q", err)
+ }
+}
+
+func TestExitCode(t *testing.T) {
+ mgr := newManager()
+
+ err := mgr.Exec(exec.Command("ls", "doesnotexist"))
+ exitErr := &ChildExit{}
+ if !errors.As(err, &exitErr) {
+ t.Errorf("expected a ChildExit err, got %q", err)
+ }
+ if exitErr.ExitCode == 0 {
+ t.Error("expected non-zero exit code , got 0")
+ }
+}