Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Commit

Permalink
Add test.abort() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed Aug 6, 2021
1 parent 3d5ac8d commit 39b69b3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
30 changes: 25 additions & 5 deletions pkg/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package execution
import (
"context"
"errors"
"fmt"
"sort"
"time"

Expand Down Expand Up @@ -172,34 +173,53 @@ func newScenarioInfo(getCtx func() context.Context) (*execInfo, error) {
return newExecInfo(rt, si), nil
}

// newTestInfo returns a goja.DynamicObject implementation to retrieve
// information about the overall test run (local instance).
func newTestInfo(getCtx func() context.Context) (*execInfo, error) {
ctx := getCtx()
func getES(ctx context.Context, rt *goja.Runtime) *lib.ExecutionState {
es := lib.GetExecutionState(ctx)
if es == nil {
return nil, errors.New("getting test information in the init context is not supported")
common.Throw(rt, errors.New("getting test information in the init context is not supported"))
}
return es
}

// newTestInfo returns a goja.DynamicObject implementation to retrieve
// information about the overall test run (local instance), and to allow
// stopping it with abort().
func newTestInfo(getCtx func() context.Context) (*execInfo, error) {
ctx := getCtx()
rt := common.GetRuntime(ctx)
if rt == nil {
return nil, errors.New("goja runtime is nil in context")
}

ti := map[string]func() interface{}{
// stop the test run
"abort": func() interface{} {
return func(msg goja.Value) {
reason := common.AbortTest
if msg != nil && !goja.IsUndefined(msg) {
reason = fmt.Sprintf("%s: %s", reason, msg.String())
}
rt.Interrupt(&common.InterruptError{Reason: reason})
}
},
"duration": func() interface{} {
es := getES(ctx, rt)
return float64(es.GetCurrentTestRunDuration()) / float64(time.Millisecond)
},
"iterationsCompleted": func() interface{} {
es := getES(ctx, rt)
return es.GetFullIterationCount()
},
"iterationsInterrupted": func() interface{} {
es := getES(ctx, rt)
return es.GetPartialIterationCount()
},
"vusActive": func() interface{} {
es := getES(ctx, rt)
return es.GetCurrentlyActiveVUsCount()
},
"vusMax": func() interface{} {
es := getES(ctx, rt)
return es.GetInitializedVUsCount()
},
}
Expand Down
39 changes: 38 additions & 1 deletion pkg/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ package execution
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"testing"
"time"

"github.com/dop251/goja"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.k6.io/k6/core/local"
"go.k6.io/k6/js"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/js/modulestest"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/loader"
Expand Down Expand Up @@ -354,7 +358,7 @@ func TestExecutionInfo(t *testing.T) {
}`},
{name: "test_err", script: `
var exec = require('k6/x/execution');
exec.test;
exec.test.duration;
`, expErr: "getting test information in the init context is not supported"},
}

Expand Down Expand Up @@ -402,3 +406,36 @@ func TestExecutionInfo(t *testing.T) {
})
}
}

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

rt := goja.New()
ctx := common.WithRuntime(context.Background(), rt)
ctx = lib.WithState(ctx, &lib.State{})
mii := &modulestest.InstanceCore{
Runtime: rt,
InitEnv: &common.InitEnvironment{},
Ctx: ctx,
}
m, ok := New().NewModuleInstance(mii).(*ModuleInstance)
require.True(t, ok)
require.NoError(t, rt.Set("exec", m.GetExports().Default))

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("default reason", func(t *testing.T) { //nolint: paralleltest
prove(t, "exec.test.abort()", common.AbortTest)
})
t.Run("custom reason", func(t *testing.T) { //nolint: paralleltest
prove(t, `exec.test.abort("mayday")`, fmt.Sprintf("%s: mayday", common.AbortTest))
})
}

0 comments on commit 39b69b3

Please sign in to comment.