Skip to content

Commit

Permalink
Made a helper function a method and added more documentation based on…
Browse files Browse the repository at this point in the history
… PR feedback.
  • Loading branch information
mitchell-as committed May 22, 2024
1 parent af40d39 commit fcd0667
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (tt *TermTest) expectExitCode(exitCode int, match bool, opts ...SetExpectOp
select {
case <-time.After(timeoutV):
return fmt.Errorf("after %s: %w", timeoutV, TimeoutError)
case state := <-ttExited(tt, false):
case state := <-tt.Exited(false): // do not wait for unread output since it's not read by this select{}
if state.Err != nil && (state.ProcessState == nil || state.ProcessState.ExitCode() == 0) {
return fmt.Errorf("cmd wait failed: %w", state.Err)
}
Expand Down
24 changes: 0 additions & 24 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ var neverGonnaHappen = time.Hour * 24 * 365 * 100
var lineSepPosix = "\n"
var lineSepWindows = "\r\n"

var processExitPollInterval = 10 * time.Millisecond
var processExitExtraWait = 500 * time.Millisecond

type cmdExit struct {
ProcessState *os.ProcessState
Err error
Expand All @@ -36,27 +33,6 @@ func waitForCmdExit(cmd *exec.Cmd) chan *cmdExit {
return exit
}

// ttExited returns a channel that sends the given termtest's command cmdExit info when available.
// This can be used within a select{} statement.
// If waitExtra is given, waits a little bit before sending cmdExit info. This allows any fellow
// switch cases to handle unprocessed stdout.
func ttExited(tt *TermTest, waitExtra bool) chan *cmdExit {
return waitChan(func() *cmdExit {
ticker := time.NewTicker(processExitPollInterval)
for {
select {
case <-ticker.C:
if tt.exited != nil {
if waitExtra {
time.Sleep(processExitExtraWait)
}
return tt.exited
}
}
}
})
}

func waitChan[T any](wait func() T) chan T {
done := make(chan T)
go func() {
Expand Down
2 changes: 1 addition & 1 deletion outputconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (e *outputConsumer) wait() error {
e.mutex.Lock()
e.opts.Logger.Println("Encountered timeout")
return fmt.Errorf("after %s: %w", e.opts.Timeout, TimeoutError)
case state := <-ttExited(e.tt, true):
case state := <-e.tt.Exited(true): // allow for output to be read first by first case in this select{}
e.mutex.Lock()
if state.Err != nil {
e.opts.Logger.Println("Encountered error waiting for process to exit: %s\n", state.Err.Error())
Expand Down
25 changes: 25 additions & 0 deletions termtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type SetOpt func(o *Opts) error
const DefaultCols = 140
const DefaultRows = 10

var processExitPollInterval = 10 * time.Millisecond
var processExitExtraWait = 500 * time.Millisecond

func NewOpts() *Opts {
return &Opts{
Logger: VoidLogger,
Expand Down Expand Up @@ -321,6 +324,28 @@ func (tt *TermTest) SendCtrlC() {
tt.Send(string([]byte{0x03})) // 0x03 is ASCII character for ^C
}

// Exited returns a channel that sends the given termtest's command cmdExit info when available.
// This can be used within a select{} statement.
// If waitExtra is given, waits a little bit before sending cmdExit info. This allows any fellow
// switch cases with output consumers to handle unprocessed stdout. If there are no such cases
// (e.g. ExpectExit(), where we want to catch an exit ASAP), waitExtra should be false.
func (tt *TermTest) Exited(waitExtra bool) chan *cmdExit {
return waitChan(func() *cmdExit {
ticker := time.NewTicker(processExitPollInterval)
for {
select {
case <-ticker.C:
if tt.exited != nil {
if waitExtra { // allow sibling output consumer cases to handle their output
time.Sleep(processExitExtraWait)
}
return tt.exited
}
}
}
})
}

func (tt *TermTest) errorHandler(rerr *error) {
err := *rerr
if err == nil {
Expand Down

0 comments on commit fcd0667

Please sign in to comment.