Skip to content

Commit

Permalink
Update k6.abortTest() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić authored and oleiade committed Jan 10, 2022
1 parent e7314bb commit 8ee99f1
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 53 deletions.
2 changes: 1 addition & 1 deletion cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestAbortTest(t *testing.T) { //nolint: tparallel
t.Run("Check that teardown is called", func(t *testing.T) { //nolint: paralleltest
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
msg := "Calling teardown function after abortTest()"
msg := "Calling teardown function after test.abort()"
var buf bytes.Buffer
logger := logrus.New()
logger.SetOutput(&buf)
Expand Down
4 changes: 2 additions & 2 deletions cmd/testdata/abort.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { abortTest } from 'k6';
import exec from 'k6/execution';

export default function () {
abortTest();
exec.test.abort();
}
8 changes: 4 additions & 4 deletions cmd/testdata/teardown.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { abortTest } from 'k6';
import exec from 'k6/execution';

export default function () {
abortTest();
exec.test.abort();
}

export function teardown() {
console.log('Calling teardown function after abortTest()');
}
console.log('Calling teardown function after test.abort()');
}
12 changes: 0 additions & 12 deletions js/modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package k6

import (
"context"
"errors"
"math/rand"
"sync/atomic"
Expand Down Expand Up @@ -152,17 +151,6 @@ func (mi *K6) Group(name string, fn goja.Callable) (goja.Value, error) {
return ret, err
}

// AbortTest exposes abortTest function in the k6 module. When called it will
// interrupt the active goja runtime passed with ctx.
func (*K6) AbortTest(ctx context.Context, msg goja.Value) {
rt := common.GetRuntime(ctx)
reason := common.AbortTest
if !goja.IsUndefined(msg) {
reason = msg.String()
}
rt.Interrupt(&common.InterruptError{Reason: reason})
}

// Check will emit check metrics for the provided checks.
//nolint:cyclop
func (mi *K6) Check(arg0, checks goja.Value, extras ...goja.Value) (bool, error) {
Expand Down
32 changes: 0 additions & 32 deletions js/modules/k6/k6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,35 +474,3 @@ func TestCheckTags(t *testing.T) {
}, sample.Tags.CloneTags())
}
}

func TestAbortTest(t *testing.T) { //nolint: tparallel
t.Parallel()

rt := goja.New()
baseCtx := common.WithRuntime(context.Background(), rt)

ctx := new(context.Context)
*ctx = baseCtx
err := rt.Set("k6", common.Bind(rt, New(), ctx))
require.Nil(t, err)
prove := func(t *testing.T, script, reason string) {
_, err := rt.RunString(script)
require.NotNil(t, err)
var x *goja.InterruptedError
assert.ErrorAs(t, err, &x)
v, ok := x.Value().(*common.InterruptError)
require.True(t, ok)
require.Equal(t, v.Reason, reason)
}
t.Run("Without state", func(t *testing.T) { //nolint: paralleltest
prove(t, "k6.abortTest()", common.AbortTest)
})
t.Run("With state and default reason", func(t *testing.T) { //nolint: paralleltest
*ctx = lib.WithState(baseCtx, &lib.State{})
prove(t, "k6.abortTest()", common.AbortTest)
})
t.Run("With state and custom reason", func(t *testing.T) { //nolint: paralleltest
*ctx = lib.WithState(baseCtx, &lib.State{})
prove(t, `k6.abortTest("mayday")`, "mayday")
})
}
4 changes: 2 additions & 2 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1507,8 +1507,8 @@ func TestInitContextForbidden(t *testing.T) {
},
{
"abortTest",
`var abortTest = require("k6").abortTest;
abortTest();
`var test = require("k6/execution").test;
test.abort();
exports.default = function() { console.log("p"); }`,
common.AbortTest,
},
Expand Down

0 comments on commit 8ee99f1

Please sign in to comment.