Skip to content

Commit

Permalink
Merge 058efe5 into 9c65657
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- authored Feb 9, 2023
2 parents 9c65657 + 058efe5 commit d6540cd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
1 change: 1 addition & 0 deletions errext/abort_reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
AbortedByScriptError
AbortedByScriptAbort
AbortedByTimeout
AbortedByOutput
)

// HasAbortReason is a wrapper around an error with an attached abort reason.
Expand Down
13 changes: 9 additions & 4 deletions output/cloud/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"go.k6.io/k6/cloudapi"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/output"

"go.k6.io/k6/lib"
Expand Down Expand Up @@ -59,7 +60,7 @@ type Output struct {
aggregationDone *sync.WaitGroup
stopOutput chan struct{}
outputDone *sync.WaitGroup
engineStopFunc func(error)
testStopFunc func(error)
}

// Verify that Output implements the wanted interfaces
Expand Down Expand Up @@ -309,7 +310,7 @@ func (out *Output) getRunStatus(testErr error) cloudapi.RunStatus {
return cloudapi.RunStatusAbortedScriptError
case errext.AbortedByScriptAbort:
return cloudapi.RunStatusAbortedUser // TODO: have a better value than this?
case errext.AbortedByTimeout:
case errext.AbortedByTimeout, errext.AbortedByOutput:
return cloudapi.RunStatusAbortedLimit
case errext.AbortedByThresholdsAfterTestEnd:
// The test run finished normally, it wasn't prematurely aborted by
Expand Down Expand Up @@ -349,7 +350,7 @@ func (out *Output) SetThresholds(scriptThresholds map[string]metrics.Thresholds)

// SetTestRunStopCallback receives the function that stops the engine on error
func (out *Output) SetTestRunStopCallback(stopFunc func(error)) {
out.engineStopFunc = stopFunc
out.testStopFunc = stopFunc
}

// AddMetricSamples receives a set of metric samples. This method is never
Expand Down Expand Up @@ -652,8 +653,12 @@ func (out *Output) pushMetrics() {
if err != nil {
if out.shouldStopSendingMetrics(err) {
out.logger.WithError(err).Warn("Stopped sending metrics to cloud due to an error")
serr := errext.WithAbortReasonIfNone(
errext.WithExitCodeIfNone(err, exitcodes.ExternalAbort),
errext.AbortedByOutput,
)
if out.config.StopOnError.Bool {
out.engineStopFunc(err)
out.testStopFunc(serr)
}
close(out.stopSendingMetrics)
break
Expand Down
12 changes: 6 additions & 6 deletions output/cloud/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,13 @@ func testCloudOutputStopSendingMetric(t *testing.T, stopOnError bool) {
},
ScriptPath: &url.URL{Path: "/script.js"},
})
var expectedEngineStopFuncCalled int64
var expectedTestStopFuncCalled int64
if stopOnError {
expectedEngineStopFuncCalled = 1
expectedTestStopFuncCalled = 1
}
var engineStopFuncCalled int64
out.engineStopFunc = func(error) {
atomic.AddInt64(&engineStopFuncCalled, 1)
var TestStopFuncCalled int64
out.testStopFunc = func(error) {
atomic.AddInt64(&TestStopFuncCalled, 1)
}
require.NoError(t, err)
now := time.Now()
Expand Down Expand Up @@ -513,7 +513,7 @@ func testCloudOutputStopSendingMetric(t *testing.T, stopOnError bool) {
t.Fatal("sending metrics wasn't stopped")
}
require.Equal(t, max, count)
require.Equal(t, expectedEngineStopFuncCalled, engineStopFuncCalled)
require.Equal(t, expectedTestStopFuncCalled, TestStopFuncCalled)

nBufferSamples := len(out.bufferSamples)
nBufferHTTPTrails := len(out.bufferHTTPTrails)
Expand Down
8 changes: 4 additions & 4 deletions output/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ type WithThresholds interface {
SetThresholds(map[string]metrics.Thresholds)
}

// WithTestRunStop is an output that can stop the Engine mid-test, interrupting
// the whole test run execution if some internal condition occurs, completely
// independently from the thresholds. It requires a callback function which
// expects an error and triggers the Engine to stop.
// WithTestRunStop is an output that can stop the test run mid-way through,
// interrupting the whole test run execution if some internal condition occurs,
// completely independently from the thresholds. It requires a callback function
// which expects an error and triggers the Engine to stop.
type WithTestRunStop interface {
Output
SetTestRunStopCallback(func(error))
Expand Down

0 comments on commit d6540cd

Please sign in to comment.