Skip to content

Commit

Permalink
try manually canceling specCtx
Browse files Browse the repository at this point in the history
Instead of defer'ing the specCancel, try manually calling the specCancel
on exit points in each spec execution.

Issue #37

Signed-off-by: Jay Pipes <[email protected]>
  • Loading branch information
jaypipes committed Jun 26, 2024
1 parent 6acae43 commit 3090f72
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions scenario/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,35 +84,36 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error {
// we mutate the single supplied top-level context, then only the
// first deadline/timeout will be used.
specCtx, specCancel := context.WithCancel(ctx)
defer specCancel()

to := getTimeout(ctx, scDefaults, plugin, spec)
if to != nil {
var cancel context.CancelFunc
specCtx, cancel = context.WithTimeout(specCtx, to.Duration())
defer cancel()
specCtx, specCancel = context.WithTimeout(specCtx, to.Duration())
}

var res *api.Result
ch := make(chan runSpecRes, 1)

go s.runSpec(specCtx, ch, rt, to, idx, spec)
go s.runSpec(specCtx, ch, rt, idx, spec)

select {
case <-specCtx.Done():
t.Fatalf("assertion failed: timeout exceeded (%s)", to.After)
specCancel()
break
case runres := <-ch:
res = runres.r
rterr = runres.err
}
if rterr != nil {
specCancel()
break
}

if wait != nil && wait.After != "" {
debug.Println(ctx, "wait: %s after", wait.After)
time.Sleep(wait.AfterDuration())
}

// Results can have arbitrary run data stored in them and we
// save this prior run data in the top-level context (and pass
// that context to the next Run invocation).
Expand All @@ -122,6 +123,7 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error {
for _, fail := range res.Failures() {
t.Fatal(fail)
}
specCancel()
}
})
return rterr
Expand All @@ -137,7 +139,6 @@ func (s *Scenario) runSpec(
ctx context.Context,
ch chan runSpecRes,
retry *api.Retry,
timeout *api.Timeout,
idx int,
spec api.Evaluable,
) {
Expand Down

0 comments on commit 3090f72

Please sign in to comment.