Skip to content

Commit

Permalink
fixup! Don't require InstanceCore to be embeded
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Nov 11, 2021
1 parent 0f3f7bc commit 2cd7f24
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 49 deletions.
13 changes: 7 additions & 6 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,25 @@ func (i *InitContext) Require(arg string) goja.Value {
}
}

type moduleInstanceParamsImpl struct {
// TODO this likely should just be part of the initialized VU or at least to take stuff directly from it.
type moduleVUImpl struct {
ctxPtr *context.Context
// we can technically put lib.State here as well as anything else
}

func (m *moduleInstanceParamsImpl) Context() context.Context {
func (m *moduleVUImpl) Context() context.Context {
return *m.ctxPtr
}

func (m *moduleInstanceParamsImpl) InitEnv() *common.InitEnvironment {
func (m *moduleVUImpl) InitEnv() *common.InitEnvironment {
return common.GetInitEnv(*m.ctxPtr) // TODO thread it correctly instead
}

func (m *moduleInstanceParamsImpl) State() *lib.State {
func (m *moduleVUImpl) State() *lib.State {
return lib.GetState(*m.ctxPtr) // TODO thread it correctly instead
}

func (m *moduleInstanceParamsImpl) Runtime() *goja.Runtime {
func (m *moduleVUImpl) Runtime() *goja.Runtime {
return common.GetRuntime(*m.ctxPtr) // TODO thread it correctly instead
}

Expand Down Expand Up @@ -201,7 +202,7 @@ func (i *InitContext) requireModule(name string) (goja.Value, error) {
return nil, fmt.Errorf("unknown module: %s", name)
}
if m, ok := mod.(modules.Module); ok {
instance := m.NewModuleInstance(&moduleInstanceParamsImpl{ctxPtr: i.ctxPtr})
instance := m.NewModuleInstance(&moduleVUImpl{ctxPtr: i.ctxPtr})
return i.runtime.ToValue(toESModuleExports(instance.Exports())), nil
}
if perInstance, ok := mod.(modules.HasModuleInstancePerVU); ok {
Expand Down
18 changes: 9 additions & 9 deletions js/modules/k6/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type (

// ModuleInstance represents an instance of the execution module.
ModuleInstance struct {
modules.InstanceParams
vu modules.VU
obj *goja.Object
}
)
Expand All @@ -57,9 +57,9 @@ func New() *RootModule {

// NewModuleInstance implements the modules.Module interface to return
// a new instance for each VU.
func (*RootModule) NewModuleInstance(m modules.InstanceParams) modules.Instance {
mi := &ModuleInstance{InstanceParams: m}
rt := m.Runtime()
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
mi := &ModuleInstance{vu: vu}
rt := vu.Runtime()
o := rt.NewObject()
defProp := func(name string, newInfo func() (*goja.Object, error)) {
err := o.DefineAccessorProperty(name, rt.ToValue(func() goja.Value {
Expand Down Expand Up @@ -90,17 +90,17 @@ func (mi *ModuleInstance) Exports() modules.Exports {
// newScenarioInfo returns a goja.Object with property accessors to retrieve
// information about the scenario the current VU is running in.
func (mi *ModuleInstance) newScenarioInfo() (*goja.Object, error) {
ctx := mi.Context()
ctx := mi.vu.Context()
rt := common.GetRuntime(ctx)
vuState := mi.State()
vuState := mi.vu.State()
if vuState == nil {
return nil, errors.New("getting scenario information in the init context is not supported")
}
if rt == nil {
return nil, errors.New("goja runtime is nil in context")
}
getScenarioState := func() *lib.ScenarioState {
ss := lib.GetScenarioState(mi.Context())
ss := lib.GetScenarioState(mi.vu.Context())
if ss == nil {
common.Throw(rt, errors.New("getting scenario information in the init context is not supported"))
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func (mi *ModuleInstance) newScenarioInfo() (*goja.Object, error) {
// newInstanceInfo returns a goja.Object with property accessors to retrieve
// information about the local instance stats.
func (mi *ModuleInstance) newInstanceInfo() (*goja.Object, error) {
ctx := mi.Context()
ctx := mi.vu.Context()
es := lib.GetExecutionState(ctx)
if es == nil {
return nil, errors.New("getting instance information in the init context is not supported")
Expand Down Expand Up @@ -175,7 +175,7 @@ func (mi *ModuleInstance) newInstanceInfo() (*goja.Object, error) {
// newVUInfo returns a goja.Object with property accessors to retrieve
// information about the currently executing VU.
func (mi *ModuleInstance) newVUInfo() (*goja.Object, error) {
ctx := mi.Context()
ctx := mi.vu.Context()
vuState := lib.GetState(ctx)
if vuState == nil {
return nil, errors.New("getting VU information in the init context is not supported")
Expand Down
6 changes: 3 additions & 3 deletions js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func setupTagsExecEnv(t *testing.T) execEnv {
ctx := common.WithRuntime(context.Background(), rt)
ctx = lib.WithState(ctx, state)
m, ok := New().NewModuleInstance(
&modulestest.InstanceParams{
&modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{},
CtxField: ctx,
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestVUTags(t *testing.T) {
t.Parallel()

tenv := setupTagsExecEnv(t)
state := tenv.Module.State()
state := tenv.Module.vu.State()
state.Tags.Set("custom-tag", "mytag1")

encoded, err := tenv.Runtime.RunString(`JSON.stringify(exec.vu.tags)`)
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestVUTags(t *testing.T) {
t.Parallel()

tenv := setupTagsExecEnv(t)
state := tenv.Module.State()
state := tenv.Module.vu.State()
state.Options.Throw = null.BoolFrom(true)
require.NotNil(t, state)

Expand Down
18 changes: 9 additions & 9 deletions js/modules/k6/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ import (

type Metric struct {
metric *stats.Metric
core modules.InstanceParams
vu modules.VU
}

// ErrMetricsAddInInitContext is error returned when adding to metric is done in the init context
var ErrMetricsAddInInitContext = common.NewInitContextError("Adding to metrics in the init context is not supported")

func (mi *ModuleInstance) newMetric(call goja.ConstructorCall, t stats.MetricType) (*goja.Object, error) {
initEnv := mi.InitEnv()
initEnv := mi.vu.InitEnv()
if initEnv == nil {
return nil, errors.New("metrics must be declared in the init context")
}
rt := mi.Runtime()
rt := mi.vu.Runtime()
c, _ := goja.AssertFunction(rt.ToValue(func(name string, isTime ...bool) (*goja.Object, error) {
valueType := stats.Default
if len(isTime) > 0 && isTime[0] {
Expand All @@ -58,7 +58,7 @@ func (mi *ModuleInstance) newMetric(call goja.ConstructorCall, t stats.MetricTyp
if err != nil {
return nil, err
}
metric := &Metric{metric: m, core: mi.InstanceParams}
metric := &Metric{metric: m, vu: mi.vu}
o := rt.NewObject()
err = o.DefineDataProperty("name", rt.ToValue(name), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE)
if err != nil {
Expand Down Expand Up @@ -93,7 +93,7 @@ func limitValue(v string) string {
}

func (m Metric) add(v goja.Value, addTags ...map[string]string) (bool, error) {
state := m.core.State()
state := m.vu.State()
if state == nil {
return false, ErrMetricsAddInInitContext
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func (m Metric) add(v goja.Value, addTags ...map[string]string) (bool, error) {
}

sample := stats.Sample{Time: time.Now(), Metric: m.metric, Value: vfloat, Tags: stats.IntoSampleTags(&tags)}
stats.PushIfNotDone(m.core.Context(), state.Samples, sample)
stats.PushIfNotDone(m.vu.Context(), state.Samples, sample)
return true, nil
}

Expand All @@ -139,7 +139,7 @@ type (
RootModule struct{}
// ModuleInstance represents an instance of the metrics module
ModuleInstance struct {
modules.InstanceParams
vu modules.VU
}
)

Expand All @@ -149,8 +149,8 @@ var (
)

// NewModuleInstance implements modules.Module interface
func (*RootModule) NewModuleInstance(m modules.InstanceParams) modules.Instance {
return &ModuleInstance{InstanceParams: m}
func (*RootModule) NewModuleInstance(m modules.VU) modules.Instance {
return &ModuleInstance{vu: m}
}

// New returns a new RootModule.
Expand Down
6 changes: 3 additions & 3 deletions js/modules/k6/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestMetrics(t *testing.T) {
}
test.rt = goja.New()
test.rt.SetFieldNameMapper(common.FieldNameMapper{})
mii := &modulestest.InstanceParams{
mii := &modulestest.VU{
RuntimeField: test.rt,
InitEnvField: &common.InitEnvironment{Registry: metrics.NewRegistry()},
CtxField: context.Background(),
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestMetricGetName(t *testing.T) {
rt := goja.New()
rt.SetFieldNameMapper(common.FieldNameMapper{})

mii := &modulestest.InstanceParams{
mii := &modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{Registry: metrics.NewRegistry()},
CtxField: context.Background(),
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestMetricDuplicates(t *testing.T) {
rt := goja.New()
rt.SetFieldNameMapper(common.FieldNameMapper{})

mii := &modulestest.InstanceParams{
mii := &modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{Registry: metrics.NewRegistry()},
CtxField: context.Background(),
Expand Down
15 changes: 7 additions & 8 deletions js/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ type HasModuleInstancePerVU interface {
NewModuleInstancePerVU() interface{}
}

// Module is the interface js modules should implement in order to get access to the InstanceParams
// Module is the interface js modules should implement in order to get access to the VU
type Module interface {
// NewModuleInstance will get InstanceParams that should provide the module with *everything* it needs and return an
// Instance implementation (embedding the InstanceParams).
// This method will be called for *each* require/import and return an object for VUs.
NewModuleInstance(InstanceParams) Instance
// NewModuleInstance will get modules.VU that should provide the module with a way to interact with the VU
// This method will be called for *each* require/import and should return an unique instance for each call
NewModuleInstance(VU) Instance
}

// checks that modules implement HasModuleInstancePerVU
Expand Down Expand Up @@ -107,9 +106,9 @@ func getInterfaceMethods() []string {
return result
}

// InstanceParams is something that will be provided to module instance on initialization so that it can get access to
// everything it needs
type InstanceParams interface {
// VU gives access to the currently executing VU to a module Instance
type VU interface {
// Context return the context.Context about the current VU
Context() context.Context

// InitEnv returns common.InitEnvironment instance if present
Expand Down
22 changes: 11 additions & 11 deletions js/modulestest/modulestest.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ import (
"go.k6.io/k6/lib"
)

var _ modules.InstanceParams = &InstanceParams{}
var _ modules.VU = &VU{}

// InstanceParams is a modules.InstanceParams implementation meant to be used within tests
type InstanceParams struct {
// VU is a modules.VU implementation meant to be used within tests
type VU struct {
CtxField context.Context
InitEnvField *common.InitEnvironment
StateField *lib.State
RuntimeField *goja.Runtime
}

// Context returns internally set field to conform to modules.InstanceParams interface
func (m *InstanceParams) Context() context.Context {
// Context returns internally set field to conform to modules.VU interface
func (m *VU) Context() context.Context {
return m.CtxField
}

// InitEnv returns internally set field to conform to modules.InstanceParams interface
func (m *InstanceParams) InitEnv() *common.InitEnvironment {
// InitEnv returns internally set field to conform to modules.VU interface
func (m *VU) InitEnv() *common.InitEnvironment {
return m.InitEnvField
}

// State returns internally set field to conform to modules.InstanceParams interface
func (m *InstanceParams) State() *lib.State {
// State returns internally set field to conform to modules.VU interface
func (m *VU) State() *lib.State {
return m.StateField
}

// Runtime returns internally set field to conform to modules.InstanceParams interface
func (m *InstanceParams) Runtime() *goja.Runtime {
// Runtime returns internally set field to conform to modules.VU interface
func (m *VU) Runtime() *goja.Runtime {
return m.RuntimeField
}

0 comments on commit 2cd7f24

Please sign in to comment.