Skip to content

Commit 80d9376

Browse files
committed
Cancel ExpectTimeout on remaining SubProcesses when a good result has been received
1 parent 5577cf0 commit 80d9376

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

test/gexpect.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package test
2424

2525
import (
2626
"bytes"
27+
"context"
2728
"fmt"
2829
"io"
2930
"os"
@@ -153,7 +154,8 @@ func (sp *SubProcess) Wait() error {
153154
// ExpectTimeout waits for the output of the process to match the given expression, or until a timeout occurs.
154155
// If a match on the given expression is found, the process output is discard until the end of the match and
155156
// nil is returned, otherwise a timeout error is returned.
156-
func (sp *SubProcess) ExpectTimeout(timeout time.Duration, re *regexp.Regexp, id string) error {
157+
// If the given context is cancelled, nil is returned.
158+
func (sp *SubProcess) ExpectTimeout(ctx context.Context, timeout time.Duration, re *regexp.Regexp, id string) error {
157159
found := make(chan struct{})
158160

159161
sp.mutex.Lock()
@@ -163,6 +165,8 @@ func (sp *SubProcess) ExpectTimeout(timeout time.Duration, re *regexp.Regexp, id
163165
sp.matchExpressions()
164166

165167
select {
168+
case <-ctx.Done():
169+
return nil
166170
case <-time.After(timeout):
167171
// Return timeout error
168172
var output []byte

test/passthrough_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package test
2424

2525
import (
26+
"context"
2627
"os"
2728
"regexp"
2829
"testing"
@@ -39,7 +40,8 @@ func TestPassthroughConflict(t *testing.T) {
3940
defer child.Close()
4041

4142
expr := regexp.MustCompile("is essential to the starters behavior and cannot be overwritten")
42-
if err := child.ExpectTimeout(time.Second*15, expr, "starter-passthrough"); err != nil {
43+
ctx := context.Background()
44+
if err := child.ExpectTimeout(ctx, time.Second*15, expr, "starter-passthrough"); err != nil {
4345
t.Errorf("Expected errors message, got %#v", err)
4446
}
4547
}

test/util.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,14 @@ type waitUntilReadyResult struct {
144144
// WaitUntilStarterReady waits until all given starter processes have reached the "Your cluster is ready state"
145145
func WaitUntilStarterReady(t *testing.T, what string, requiredGoodResults int, starters ...*SubProcess) bool {
146146
results := make(chan waitUntilReadyResult, len(starters))
147+
ctx, cancel := context.WithCancel(context.Background())
148+
defer cancel()
147149
for index, starter := range starters {
148150
starter := starter // Used in nested function
149151
id := fmt.Sprintf("starter-%d", index+1)
150152
go func() {
151153
started := time.Now()
152-
if err := starter.ExpectTimeout(time.Minute*2, regexp.MustCompile(fmt.Sprintf("Your %s can now be accessed with a browser at", what)), id); err != nil {
154+
if err := starter.ExpectTimeout(ctx, time.Minute*2, regexp.MustCompile(fmt.Sprintf("Your %s can now be accessed with a browser at", what)), id); err != nil {
153155
timeSpan := time.Since(started)
154156
results <- waitUntilReadyResult{
155157
Ready: false,

0 commit comments

Comments
 (0)