Skip to content

Commit fcd0667

Browse files
committed
Made a helper function a method and added more documentation based on PR feedback.
1 parent af40d39 commit fcd0667

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

expect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (tt *TermTest) expectExitCode(exitCode int, match bool, opts ...SetExpectOp
180180
select {
181181
case <-time.After(timeoutV):
182182
return fmt.Errorf("after %s: %w", timeoutV, TimeoutError)
183-
case state := <-ttExited(tt, false):
183+
case state := <-tt.Exited(false): // do not wait for unread output since it's not read by this select{}
184184
if state.Err != nil && (state.ProcessState == nil || state.ProcessState.ExitCode() == 0) {
185185
return fmt.Errorf("cmd wait failed: %w", state.Err)
186186
}

helpers.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ var neverGonnaHappen = time.Hour * 24 * 365 * 100
1818
var lineSepPosix = "\n"
1919
var lineSepWindows = "\r\n"
2020

21-
var processExitPollInterval = 10 * time.Millisecond
22-
var processExitExtraWait = 500 * time.Millisecond
23-
2421
type cmdExit struct {
2522
ProcessState *os.ProcessState
2623
Err error
@@ -36,27 +33,6 @@ func waitForCmdExit(cmd *exec.Cmd) chan *cmdExit {
3633
return exit
3734
}
3835

39-
// ttExited returns a channel that sends the given termtest's command cmdExit info when available.
40-
// This can be used within a select{} statement.
41-
// If waitExtra is given, waits a little bit before sending cmdExit info. This allows any fellow
42-
// switch cases to handle unprocessed stdout.
43-
func ttExited(tt *TermTest, waitExtra bool) chan *cmdExit {
44-
return waitChan(func() *cmdExit {
45-
ticker := time.NewTicker(processExitPollInterval)
46-
for {
47-
select {
48-
case <-ticker.C:
49-
if tt.exited != nil {
50-
if waitExtra {
51-
time.Sleep(processExitExtraWait)
52-
}
53-
return tt.exited
54-
}
55-
}
56-
}
57-
})
58-
}
59-
6036
func waitChan[T any](wait func() T) chan T {
6137
done := make(chan T)
6238
go func() {

outputconsumer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (e *outputConsumer) wait() error {
103103
e.mutex.Lock()
104104
e.opts.Logger.Println("Encountered timeout")
105105
return fmt.Errorf("after %s: %w", e.opts.Timeout, TimeoutError)
106-
case state := <-ttExited(e.tt, true):
106+
case state := <-e.tt.Exited(true): // allow for output to be read first by first case in this select{}
107107
e.mutex.Lock()
108108
if state.Err != nil {
109109
e.opts.Logger.Println("Encountered error waiting for process to exit: %s\n", state.Err.Error())

termtest.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type SetOpt func(o *Opts) error
5151
const DefaultCols = 140
5252
const DefaultRows = 10
5353

54+
var processExitPollInterval = 10 * time.Millisecond
55+
var processExitExtraWait = 500 * time.Millisecond
56+
5457
func NewOpts() *Opts {
5558
return &Opts{
5659
Logger: VoidLogger,
@@ -321,6 +324,28 @@ func (tt *TermTest) SendCtrlC() {
321324
tt.Send(string([]byte{0x03})) // 0x03 is ASCII character for ^C
322325
}
323326

327+
// Exited returns a channel that sends the given termtest's command cmdExit info when available.
328+
// This can be used within a select{} statement.
329+
// If waitExtra is given, waits a little bit before sending cmdExit info. This allows any fellow
330+
// switch cases with output consumers to handle unprocessed stdout. If there are no such cases
331+
// (e.g. ExpectExit(), where we want to catch an exit ASAP), waitExtra should be false.
332+
func (tt *TermTest) Exited(waitExtra bool) chan *cmdExit {
333+
return waitChan(func() *cmdExit {
334+
ticker := time.NewTicker(processExitPollInterval)
335+
for {
336+
select {
337+
case <-ticker.C:
338+
if tt.exited != nil {
339+
if waitExtra { // allow sibling output consumer cases to handle their output
340+
time.Sleep(processExitExtraWait)
341+
}
342+
return tt.exited
343+
}
344+
}
345+
}
346+
})
347+
}
348+
324349
func (tt *TermTest) errorHandler(rerr *error) {
325350
err := *rerr
326351
if err == nil {

0 commit comments

Comments
 (0)