Skip to content

Commit

Permalink
Disable async function as group callback
Browse files Browse the repository at this point in the history
The general inconsistencies between group as an idea and
asynchronous code makes it not great fit and such we will try to
discourage users to mix them.

Updates #2728
  • Loading branch information
mstoykov committed Jan 25, 2023
1 parent 1d99b0b commit fb87f90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
17 changes: 14 additions & 3 deletions js/modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var (
ErrCheckInInitContext = common.NewInitContextError("Using check() in the init context is not supported")
)

const asyncFunctionNotSupportedMsg = "Async Function not supported: Come up with a message here"

type (
// RootModule is the global module instance that will create module
// instances for each VU.
Expand Down Expand Up @@ -85,16 +87,24 @@ func (mi *K6) RandomSeed(seed int64) {
}

// Group wraps a function call and executes it within the provided group name.
func (mi *K6) Group(name string, fn goja.Callable) (goja.Value, error) {
func (mi *K6) Group(name string, val goja.Value) (goja.Value, error) {
state := mi.vu.State()
if state == nil {
return nil, ErrGroupInInitContext
}

if fn == nil {
if val == nil || goja.IsNull(val) {
return nil, errors.New("group() requires a callback as a second argument")
}

fn, ok := goja.AssertFunction(val)
if !ok {
return nil, errors.New("group() requires a callback as a second argument")
}
rt := mi.vu.Runtime()
o := val.ToObject(rt)
if o.ClassName() == "AsyncFunction" {
return goja.Undefined(), errors.New(asyncFunctionNotSupportedMsg)
}
g, err := state.Group.Group(name)
if err != nil {
return goja.Undefined(), err
Expand Down Expand Up @@ -138,6 +148,7 @@ func (mi *K6) Group(name string, fn goja.Callable) (goja.Value, error) {
}

// Check will emit check metrics for the provided checks.
//
//nolint:cyclop
func (mi *K6) Check(arg0, checks goja.Value, extras ...goja.Value) (bool, error) {
state := mi.vu.State()
Expand Down
14 changes: 14 additions & 0 deletions js/modules/k6/k6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ func TestGroup(t *testing.T) {
_, err := rt.RunString(`k6.group("::", function() { throw new Error("nooo") })`)
assert.Contains(t, err.Error(), "group and check names may not contain '::'")
})

t.Run("async function", func(t *testing.T) {
t.Parallel()
rt, _, _ := setupGroupTest()
_, err := rt.RunString(`k6.group("something", async function() { })`)
assert.ErrorContains(t, err, asyncFunctionNotSupportedMsg)
})

t.Run("async lambda", func(t *testing.T) {
t.Parallel()
rt, _, _ := setupGroupTest()
_, err := rt.RunString(`k6.group("something", async () => { })`)
assert.ErrorContains(t, err, asyncFunctionNotSupportedMsg)
})
}

func checkTestRuntime(t testing.TB) (*goja.Runtime, chan metrics.SampleContainer, *metrics.BuiltinMetrics) {
Expand Down

0 comments on commit fb87f90

Please sign in to comment.