Skip to content

Commit

Permalink
Merge pull request #143 from grafana/feature/exit-on-abort
Browse files Browse the repository at this point in the history
fix: better handling of the test aborted situation
  • Loading branch information
szkiba authored Jan 16, 2024
2 parents 952bc2b + 57bff3c commit 2b5ffeb
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 15 deletions.
6 changes: 3 additions & 3 deletions dashboard/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package dashboard
type eventListener interface {
onEvent(event string, data interface{})
onStart() error
onStop() error
onStop(reason error) error
}

type eventSource struct {
Expand All @@ -36,9 +36,9 @@ func (src *eventSource) fireStart() error {
return nil
}

func (src *eventSource) fireStop() error {
func (src *eventSource) fireStop(reason error) error {
for _, e := range src.listeners {
if err := e.onStop(); err != nil {
if err := e.onStop(reason); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions dashboard/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (*errorEventListener) onStart() error {
return assert.AnError
}

func (*errorEventListener) onStop() error {
func (*errorEventListener) onStop(_ error) error {
return assert.AnError
}

Expand All @@ -32,5 +32,5 @@ func Test_eventSource_error(t *testing.T) {
src.addEventListener(new(errorEventListener))

assert.Error(t, src.fireStart())
assert.Error(t, src.fireStop())
assert.Error(t, src.fireStop(nil))
}
9 changes: 8 additions & 1 deletion dashboard/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,14 @@ func (ext *extension) Start() error {
}

// Stop flushes any remaining metrics and stops the extension.
// k6 core will call WithStopWithTestError instead of this one.
func (ext *extension) Stop() error {
return ext.StopWithTestError(nil)
}

// WithStopWithTestError allows output to receive the error value that the test finished with.
// Flushes any remaining metrics and stops the extension.
func (ext *extension) StopWithTestError(testRunErr error) error {
ext.noFlush.Store(true)

ext.flusher.Stop()
Expand All @@ -161,7 +168,7 @@ func (ext *extension) Stop() error {

ext.updateAndSend(nil, newMeter(ext.period, now, ext.options.Tags), stopEvent, now)

err := ext.fireStop()
err := ext.fireStop(testRunErr)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion dashboard/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (rec *recorder) onStart() error {
return nil
}

func (rec *recorder) onStop() error {
func (rec *recorder) onStop(_ error) error {
return rec.writer.Close()
}

Expand Down
8 changes: 4 additions & 4 deletions dashboard/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Test_recorder_onStart(t *testing.T) {

assert.True(t, exists(th.proc.fs, "foo"))

assert.NoError(t, rec.onStop())
assert.NoError(t, rec.onStop(nil))
}

func Test_recorder_onStart_error(t *testing.T) {
Expand All @@ -66,7 +66,7 @@ func Test_recorder_onEvent_config(t *testing.T) {

rec.onEvent("config", data)

assert.NoError(t, rec.onStop())
assert.NoError(t, rec.onStop(nil))

file, err := th.proc.fs.Open("out")

Expand All @@ -92,7 +92,7 @@ func Test_recorder_onEvent(t *testing.T) {

rec.onEvent("dummy", data)

assert.NoError(t, rec.onStop())
assert.NoError(t, rec.onStop(nil))

file, err := th.proc.fs.Open("out.gz")

Expand Down Expand Up @@ -137,5 +137,5 @@ func Test_recorder_onEvent_error(t *testing.T) {
assert.NotNil(t, entry)
assert.Equal(t, logrus.WarnLevel, entry.Level)

assert.NoError(t, rec.onStop())
assert.NoError(t, rec.onStop(nil))
}
2 changes: 1 addition & 1 deletion dashboard/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (rep *replayer) run() error {
rep.fireEvent(input.Name, input.Data)
}

return rep.fireStop()
return rep.fireStop(nil)
}

type replayerEnvelope struct {
Expand Down
2 changes: 1 addition & 1 deletion dashboard/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (rep *reporter) onStart() error {
return nil
}

func (rep *reporter) onStop() error {
func (rep *reporter) onStop(_ error) error {
if len(rep.output) == 0 {
return nil
}
Expand Down
10 changes: 8 additions & 2 deletions dashboard/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ package dashboard

import (
"encoding/json"
"errors"
"net/http"
"strconv"
"sync"
"sync/atomic"

"github.com/r3labs/sse/v2"
"github.com/sirupsen/logrus"
"go.k6.io/k6/errext"
)

type eventEmitter struct {
Expand Down Expand Up @@ -43,8 +45,12 @@ func (emitter *eventEmitter) onStart() error {
return nil
}

func (emitter *eventEmitter) onStop() error {
emitter.wait.Wait()
func (emitter *eventEmitter) onStop(reason error) error {
var err errext.HasAbortReason

if !errors.As(reason, &err) {
emitter.wait.Wait()
}

return nil
}
Expand Down
3 changes: 3 additions & 0 deletions releases/v0.7.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
xk6-dashboard `v0.7.1` is here 🎉! This release includes:

- fix: [#142](https://github.com/grafana/xk6-dashboard/issues/142) k6 does not stop in case of an abortion

0 comments on commit 2b5ffeb

Please sign in to comment.