Skip to content

Commit

Permalink
fix: fix a bug when stopOnFailure continues an extra cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
amad committed Feb 27, 2020
1 parent 6cea575 commit 73207c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
25 changes: 14 additions & 11 deletions runner/runner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runner

import (
"context"
"errors"
"fmt"
"io"
Expand All @@ -14,17 +15,19 @@ import (

// NewRunner creates and returns a new Runner.
func NewRunner(workers int, timeout time.Duration, stopOnFailure bool, stdout io.StringWriter, stderr io.StringWriter) *Runner {
closeChan := make(chan struct{}, workers)
reports := []core.TestResult{}

ctx, cancelFunc := context.WithCancel(context.Background())

return &Runner{
workers: workers,
timeout: timeout,
stopOnFailure: stopOnFailure,
closeChan: closeChan,
stdout: stdout,
stderr: stderr,
reports: reports,
ctx: ctx,
cancelFunc: cancelFunc,
}
}

Expand All @@ -33,9 +36,10 @@ type Runner struct {
workers int
timeout time.Duration
stopOnFailure bool
closeChan chan struct{}
stdout, stderr io.StringWriter
reports []core.TestResult
ctx context.Context
cancelFunc context.CancelFunc
}

// Run smoke test on a testsuite and provides results.
Expand All @@ -59,21 +63,21 @@ func (r *Runner) Run(requester core.Requester, testsuite *core.Testsuite) (bool,
go r.reportWriter(&wg, reportsChan)

for i, tc := range testsuite.Tests {
poolChan <- struct{}{}

if r.isClosing() {
break
}

poolChan <- struct{}{}
wg.Add(2) // delta=2 to sync worker and reportWriter.

go r.worker(&wg, requester, i+1, tc, poolChan, reportsChan)
}

wg.Wait()

close(r.closeChan)
close(reportsChan)
close(poolChan)
defer r.cancelFunc()

r.printfOut("\nElapsed: %.2fs", time.Since(start).Seconds())

Expand Down Expand Up @@ -110,16 +114,15 @@ func (r *Runner) worker(wg *sync.WaitGroup, requester core.Requester, idx int, t
// Stop pauses off the runner.
// used for signal handling or when stop on failure is enabled.
func (r *Runner) Stop() {
r.closeChan <- struct{}{}
r.cancelFunc()
}

func (r *Runner) isClosing() bool {
select {
case <-r.closeChan:
if err := r.ctx.Err(); err != nil {
return true
default:
return false
}

return false
}

func (r *Runner) shouldStopOnFailure() {
Expand Down
23 changes: 2 additions & 21 deletions runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package runner
import (
"bytes"
"errors"
"reflect"
"testing"
"time"

Expand All @@ -16,25 +15,7 @@ func TestNewRunner(t *testing.T) {
var workers = 5
var buffer *bytes.Buffer

r := NewRunner(workers, time.Second, false, buffer, buffer)

if cap(r.closeChan) != workers {
t.Fatalf("Expected close channel capacity to be equal to number of workers %d but got %d", workers, cap(r.closeChan))
}

expected := &Runner{
workers,
time.Second,
false,
r.closeChan,
buffer,
buffer,
[]core.TestResult{},
}

if !reflect.DeepEqual(expected, r) {
t.Fatalf("response %+v %+v", expected, r)
}
NewRunner(workers, time.Second, false, buffer, buffer)
}

func newTestRunner(workers int, timeout int, stopOnFailure bool) *Runner {
Expand Down Expand Up @@ -146,7 +127,7 @@ func TestRunner(t *testing.T) {
"stop on failure",
&core.Testsuite{Tests: []core.TestCase{{Name: "fail"}, {}, {}}},
true,
1,
0,
1,
},
}
Expand Down

0 comments on commit 73207c2

Please sign in to comment.