Skip to content

Commit

Permalink
Merge d357f49 into b866c10
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov authored Apr 24, 2024
2 parents b866c10 + d357f49 commit 9c2d7cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
19 changes: 16 additions & 3 deletions js/modules/k6/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,15 @@ func (mi *ModuleInstance) newScenarioInfo() (*goja.Object, error) {
return newInfoObj(rt, si)
}

//nolint:lll,gochecknoglobals
var instanceInfoInitContextErr = common.NewInitContextError("getting instance information in the init context is not supported")

// newInstanceInfo returns a goja.Object with property accessors to retrieve
// information about the local instance stats.
func (mi *ModuleInstance) newInstanceInfo() (*goja.Object, error) {
es := lib.GetExecutionState(mi.vu.Context())
if es == nil {
return nil, errors.New("getting instance information in the init context is not supported")
return nil, instanceInfoInitContextErr
}
rt := mi.vu.Runtime()

Expand All @@ -157,6 +160,9 @@ func (mi *ModuleInstance) newInstanceInfo() (*goja.Object, error) {
return newInfoObj(rt, ti)
}

//nolint:gochecknoglobals
var testInfoInitContextErr = common.NewInitContextError("getting test options in the init context is not supported")

// newTestInfo returns a goja.Object with property accessors to retrieve
// information and control execution of the overall test run.
func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) {
Expand All @@ -176,8 +182,12 @@ func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) {
}
},
"options": func() interface{} {
vuState := mi.vu.State()
if vuState == nil {
common.Throw(rt, testInfoInitContextErr)
}
if optionsObject == nil {
opts, err := optionsAsObject(rt, mi.vu.State().Options)
opts, err := optionsAsObject(rt, vuState.Options)
if err != nil {
common.Throw(rt, err)
}
Expand All @@ -190,12 +200,15 @@ func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) {
return newInfoObj(rt, ti)
}

//nolint:gochecknoglobals
var vuInfoInitContextErr = common.NewInitContextError("getting VU information in the init context is not supported")

// newVUInfo returns a goja.Object with property accessors to retrieve
// information about the currently executing VU.
func (mi *ModuleInstance) newVUInfo() (*goja.Object, error) {
vuState := mi.vu.State()
if vuState == nil {
return nil, errors.New("getting VU information in the init context is not supported")
return nil, vuInfoInitContextErr
}
rt := mi.vu.Runtime()

Expand Down
17 changes: 17 additions & 0 deletions js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,23 @@ func TestScenarioNoAvailableInInitContext(t *testing.T) {
}
}

func TestOptionsNoAvailableInInitContext(t *testing.T) {
t.Parallel()

rt := goja.New()
m, ok := New().NewModuleInstance(
&modulestest.VU{
RuntimeField: rt,
CtxField: context.Background(),
},
).(*ModuleInstance)
require.True(t, ok)
require.NoError(t, rt.Set("exec", m.Exports().Default))

_, err := rt.RunString("exec.test.options")
require.ErrorContains(t, err, "getting test options in the init context is not supported")
}

func TestVUDefaultDetails(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 9c2d7cf

Please sign in to comment.