Skip to content

Commit

Permalink
Add test for VU panics
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Oct 30, 2020
1 parent e35b653 commit f4ec572
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ func (u *VU) runFn(
for _, s := range gojaStack {
s.Write(b)
}
u.state.Logger.Log(logrus.ErrorLevel, r, "\n", string(debug.Stack()), "\nGoja stack:\n", b.String())
u.state.Logger.Log(logrus.ErrorLevel, "panic: ", r, "\n", string(debug.Stack()), "\nGoja stack:\n", b.String())
}
}()

Expand Down
66 changes: 66 additions & 0 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ import (
"net/http"
"net/url"
"os"
"strings"
"sync"
"testing"
"time"

"github.com/sirupsen/logrus"
logtest "github.com/sirupsen/logrus/hooks/test"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -1735,3 +1737,67 @@ func TestSystemTags(t *testing.T) {
})
}
}

func TestVUPanic(t *testing.T) {
r1, err := getSimpleRunner(t, "/script.js", `
var group = require("k6").group;
exports.default = function() {
group("panic here", function() {
if (__ITER == 0) {
panic("here we panic");
}
console.log("here we don't");
})
}`,
)
require.NoError(t, err)

r2, err := NewFromArchive(testutils.NewLogger(t), r1.MakeArchive(), lib.RuntimeOptions{})
if !assert.NoError(t, err) {
return
}

runners := map[string]*Runner{"Source": r1, "Archive": r2}
for name, r := range runners {
r := r
t.Run(name, func(t *testing.T) {
initVU, err := r.NewVU(1234, make(chan stats.SampleContainer, 100))
if !assert.NoError(t, err) {
return
}

logger := logrus.New()
logger.SetLevel(logrus.InfoLevel)
logger.Out = ioutil.Discard
hook := testutils.SimpleLogrusHook{
HookedLevels: []logrus.Level{logrus.InfoLevel, logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel},
}
logger.AddHook(&hook)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
vu.(*ActiveVU).Runtime.Set("panic", func(str string) { panic(str) })
vu.(*ActiveVU).state.Logger = logger

vu.(*ActiveVU).Console.logger = logger.WithField("source", "console")
err = vu.RunOnce()
require.Error(t, err)
assert.Contains(t, err.Error(), "a panic occurred in VU code but was caught, here we panic")
entries := hook.Drain()
require.Len(t, entries, 1)
assert.Equal(t, logrus.ErrorLevel, entries[0].Level)
require.Contains(t, entries[0].Message, "panic: here we panic")
require.Contains(t, entries[0].Message, "Goja stack:\nfile:///script.js:3:4(12)")
require.True(t, strings.HasSuffix(entries[0].Message, "Goja stack:\nfile:///script.js:3:4(12)"))

err = vu.RunOnce()
assert.NoError(t, err)

entries = hook.Drain()
require.Len(t, entries, 1)
assert.Equal(t, logrus.InfoLevel, entries[0].Level)
require.Contains(t, entries[0].Message, "here we don't")
})
}
}

0 comments on commit f4ec572

Please sign in to comment.