Skip to content

Commit

Permalink
Embed lib.TestPreInitState directly in js/common.InitEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Mar 31, 2023
1 parent 988f2f6 commit ed32098
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
14 changes: 6 additions & 8 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ func (b *Bundle) Instantiate(ctx context.Context, vuID uint64) (*BundleInstance,
// already pre-validated in cmd.validateScenarioConfig(), just get them here.
exports := instance.exports()

jsOptions := exports.Get("options")
jsOptions := exports.Get(consts.Options)
var jsOptionsObj *goja.Object
if jsOptions == nil || goja.IsNull(jsOptions) || goja.IsUndefined(jsOptions) {
jsOptionsObj = vuImpl.runtime.NewObject()
err := exports.Set("options", jsOptionsObj)
err := exports.Set(consts.Options, jsOptionsObj)
if err != nil {
return nil, fmt.Errorf("couldn't set exported options with merged values: %w", err)
}
Expand Down Expand Up @@ -263,11 +263,9 @@ func (b *Bundle) instantiate(vuImpl *moduleVUImpl, vuID uint64) (moduleInstance,
}

initenv := &common.InitEnvironment{
Logger: b.preInitState.Logger,
FileSystems: b.filesystems,
CWD: b.pwd,
Registry: b.preInitState.Registry,
LookupEnv: b.preInitState.LookupEnv,
TestPreInitState: b.preInitState,
FileSystems: b.filesystems,
CWD: b.pwd,
}

modSys := newModuleSystem(b.moduleResolver, vuImpl)
Expand All @@ -293,7 +291,7 @@ func (b *Bundle) instantiate(vuImpl *moduleVUImpl, vuID uint64) (moduleInstance,
var instance moduleInstance
err = common.RunWithPanicCatching(b.preInitState.Logger, rt, func() error {
return vuImpl.eventLoop.Start(func() error {
//nolint:shadow,govet // here we shadow err on purpose
//nolint:govet // here we shadow err on purpose
mod, err := b.moduleResolver.resolve(b.pwd, b.sourceData.URL.String())
if err != nil {
return err // TODO wrap as this should never happen
Expand Down
12 changes: 4 additions & 8 deletions js/common/initenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ import (
"net/url"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"go.k6.io/k6/metrics"
"go.k6.io/k6/lib"
)

// InitEnvironment contains properties that can be accessed by Go code executed
// in the k6 init context. It can be accessed by calling common.GetInitEnv().
type InitEnvironment struct {
Logger logrus.FieldLogger
*lib.TestPreInitState
FileSystems map[string]afero.Fs
CWD *url.URL
Registry *metrics.Registry
LookupEnv func(key string) (val string, ok bool)
// TODO: add RuntimeOptions and other properties, goja sources, etc.
// ideally, we should leave this as the only data structure necessary for
// executing the init context for all JS modules
// TODO: get rid of this type altogether? we won't need it if we figure out
// how to handle .tar archive vs regular JS script differences in FileSystems
}

// GetAbsFilePath should be used to access the FileSystems, since afero has a
Expand Down
19 changes: 11 additions & 8 deletions js/modules/k6/html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modulestest"
"go.k6.io/k6/lib"
"go.k6.io/k6/metrics"
)

Expand Down Expand Up @@ -76,7 +77,9 @@ func getTestModuleInstance(t testing.TB) (*goja.Runtime, *ModuleInstance) {
mockVU := &modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{
Registry: metrics.NewRegistry(),
TestPreInitState: &lib.TestPreInitState{
Registry: metrics.NewRegistry(),
},
},
CtxField: ctx,
StateField: nil,
Expand Down Expand Up @@ -420,10 +423,10 @@ func TestParseHTML(t *testing.T) {
_, err := rt.RunString(`
const values = doc
.find("#select_multi option")
.map(function(idx, val) {
.map(function(idx, val) {
return val.text()
})
if (values.length !== 3) {
throw new Error('Expected 3 values, got ' + values.length)
}
Expand Down Expand Up @@ -477,25 +480,25 @@ func TestParseHTML(t *testing.T) {
})
return bucketObj
})
if (buckets.length !== 2) {
throw new Error('Expected 2 buckets, got ' + buckets.length)
}
if (buckets[0].name !== 'firstBucket') {
throw new Error('Expected bucket name to be "firstBucket", got ' + buckets[0].name)
}
if (buckets[0].creationDate !== 1654852823) {
throw new Error(
'Expected bucket creation date to be 1654852823, got ' + buckets[0].creationDate
)
}
if (buckets[1].name != 'secondBucket') {
throw new Error('Expected bucket name to be "secondBucket", got ' + buckets[1].name)
}
if (buckets[1].creationDate !== 1654852825) {
throw new Error(
'Expected bucket creation date to be 1654852825, got ' + buckets[1].creationDate
Expand Down
10 changes: 6 additions & 4 deletions js/modules/k6/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ func TestMetrics(t *testing.T) {
registry := metrics.NewRegistry()
mii := &modulestest.VU{
RuntimeField: test.rt,
InitEnvField: &common.InitEnvironment{Registry: registry},
CtxField: context.Background(),
InitEnvField: &common.InitEnvironment{
TestPreInitState: &lib.TestPreInitState{Registry: registry},
},
CtxField: context.Background(),
}
m, ok := New().NewModuleInstance(mii).(*ModuleInstance)
require.True(t, ok)
Expand Down Expand Up @@ -172,7 +174,7 @@ func TestMetricGetName(t *testing.T) {

mii := &modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{Registry: metrics.NewRegistry()},
InitEnvField: &common.InitEnvironment{TestPreInitState: &lib.TestPreInitState{Registry: metrics.NewRegistry()}},
CtxField: context.Background(),
}
m, ok := New().NewModuleInstance(mii).(*ModuleInstance)
Expand Down Expand Up @@ -200,7 +202,7 @@ func TestMetricDuplicates(t *testing.T) {

mii := &modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{Registry: metrics.NewRegistry()},
InitEnvField: &common.InitEnvironment{TestPreInitState: &lib.TestPreInitState{Registry: metrics.NewRegistry()}},
CtxField: context.Background(),
}
m, ok := New().NewModuleInstance(mii).(*ModuleInstance)
Expand Down
6 changes: 4 additions & 2 deletions js/modulestest/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ func NewRuntime(t testing.TB) *Runtime {
}
vu.RuntimeField.SetFieldNameMapper(common.FieldNameMapper{})
vu.InitEnvField = &common.InitEnvironment{
Logger: testutils.NewLogger(t),
Registry: metrics.NewRegistry(),
TestPreInitState: &lib.TestPreInitState{
Logger: testutils.NewLogger(t),
Registry: metrics.NewRegistry(),
},
}

eventloop := eventloop.New(vu)
Expand Down

0 comments on commit ed32098

Please sign in to comment.