Skip to content

Commit

Permalink
Fix panic in k6/execution
Browse files Browse the repository at this point in the history
Fixing the panic that could happen if try to access the
`exec.vu.iterationInScenario` in `setup`. At that moment, the details are
not initialized.
  • Loading branch information
olegbespalov committed Feb 23, 2023
1 parent eb2a54d commit 26ec301
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions js/modules/k6/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ func (mi *ModuleInstance) newVUInfo() (*goja.Object, error) {
"idInTest": func() interface{} { return vuState.VUIDGlobal },
"iterationInInstance": func() interface{} { return vuState.Iteration },
"iterationInScenario": func() interface{} {
if vuState.GetScenarioVUIter == nil {
// hasn't been set yet, no iteration stats available
return 0
}

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

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

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

props := []string{"idInInstance", "idInTest", "iterationInInstance", "iterationInScenario"}

for _, code := range props {
prop := fmt.Sprintf("exec.vu.%s", code)
res, err := rt.RunString(prop)
require.NoError(t, err)
require.Equal(t, "0", res.String())
}
}

func TestTagsDynamicObjectGet(t *testing.T) {
t.Parallel()
rt := goja.New()
Expand Down

0 comments on commit 26ec301

Please sign in to comment.