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 f8c5273 commit 0f3f7bc
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 55 deletions.
8 changes: 4 additions & 4 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,19 @@ type moduleInstanceParamsImpl struct {
// we can technically put lib.State here as well as anything else
}

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

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

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

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

Expand Down
12 changes: 6 additions & 6 deletions js/modules/k6/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func New() *RootModule {
// a new instance for each VU.
func (*RootModule) NewModuleInstance(m modules.InstanceParams) modules.Instance {
mi := &ModuleInstance{InstanceParams: m}
rt := m.GetRuntime()
rt := m.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.GetContext()
ctx := mi.Context()
rt := common.GetRuntime(ctx)
vuState := mi.GetState()
vuState := mi.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.GetContext())
ss := lib.GetScenarioState(mi.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.GetContext()
ctx := mi.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.GetContext()
ctx := mi.Context()
vuState := lib.GetState(ctx)
if vuState == nil {
return nil, errors.New("getting VU information in the init context is not supported")
Expand Down
12 changes: 6 additions & 6 deletions js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ func setupTagsExecEnv(t *testing.T) execEnv {
ctx = lib.WithState(ctx, state)
m, ok := New().NewModuleInstance(
&modulestest.InstanceParams{
Runtime: rt,
InitEnv: &common.InitEnvironment{},
Ctx: ctx,
State: state,
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{},
CtxField: ctx,
StateField: state,
},
).(*ModuleInstance)
require.True(t, ok)
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestVUTags(t *testing.T) {
t.Parallel()

tenv := setupTagsExecEnv(t)
state := tenv.Module.GetState()
state := tenv.Module.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.GetState()
state := tenv.Module.State()
state.Options.Throw = null.BoolFrom(true)
require.NotNil(t, state)

Expand Down
8 changes: 4 additions & 4 deletions js/modules/k6/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ type Metric struct {
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.GetInitEnv()
initEnv := mi.InitEnv()
if initEnv == nil {
return nil, errors.New("metrics must be declared in the init context")
}
rt := mi.GetRuntime()
rt := mi.Runtime()
c, _ := goja.AssertFunction(rt.ToValue(func(name string, isTime ...bool) (*goja.Object, error) {
valueType := stats.Default
if len(isTime) > 0 && isTime[0] {
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.GetState()
state := m.core.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.GetContext(), state.Samples, sample)
stats.PushIfNotDone(m.core.Context(), state.Samples, sample)
return true, nil
}

Expand Down
24 changes: 12 additions & 12 deletions js/modules/k6/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ func TestMetrics(t *testing.T) {
test.rt = goja.New()
test.rt.SetFieldNameMapper(common.FieldNameMapper{})
mii := &modulestest.InstanceParams{
Runtime: test.rt,
InitEnv: &common.InitEnvironment{Registry: metrics.NewRegistry()},
Ctx: context.Background(),
RuntimeField: test.rt,
InitEnvField: &common.InitEnvironment{Registry: metrics.NewRegistry()},
CtxField: context.Background(),
}
m, ok := New().NewModuleInstance(mii).(*ModuleInstance)
require.True(t, ok)
Expand All @@ -146,12 +146,12 @@ func TestMetrics(t *testing.T) {
require.NoError(t, err)

t.Run("ExitInit", func(t *testing.T) {
mii.State = state
mii.InitEnv = nil
mii.StateField = state
mii.InitEnvField = nil
_, err := test.rt.RunString(fmt.Sprintf(`new metrics.%s("my_metric")`, fn))
assert.Contains(t, err.Error(), "metrics must be declared in the init context")
})
mii.State = state
mii.StateField = state
logger := logrus.New()
logger.Out = ioutil.Discard
test.hook = &testutils.SimpleLogrusHook{HookedLevels: logrus.AllLevels}
Expand Down Expand Up @@ -187,9 +187,9 @@ func TestMetricGetName(t *testing.T) {
rt.SetFieldNameMapper(common.FieldNameMapper{})

mii := &modulestest.InstanceParams{
Runtime: rt,
InitEnv: &common.InitEnvironment{Registry: metrics.NewRegistry()},
Ctx: context.Background(),
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{Registry: metrics.NewRegistry()},
CtxField: context.Background(),
}
m, ok := New().NewModuleInstance(mii).(*ModuleInstance)
require.True(t, ok)
Expand All @@ -215,9 +215,9 @@ func TestMetricDuplicates(t *testing.T) {
rt.SetFieldNameMapper(common.FieldNameMapper{})

mii := &modulestest.InstanceParams{
Runtime: rt,
InitEnv: &common.InitEnvironment{Registry: metrics.NewRegistry()},
Ctx: context.Background(),
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{Registry: metrics.NewRegistry()},
CtxField: context.Background(),
}
m, ok := New().NewModuleInstance(mii).(*ModuleInstance)
require.True(t, ok)
Expand Down
14 changes: 7 additions & 7 deletions js/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ func getInterfaceMethods() []string {
// 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 {
GetContext() context.Context
Context() context.Context

// GetInitEnv returns common.InitEnvironment instance if present
GetInitEnv() *common.InitEnvironment
// InitEnv returns common.InitEnvironment instance if present
InitEnv() *common.InitEnvironment

// GetState returns lib.State if any is present
GetState() *lib.State
// State returns lib.State if any is present
State() *lib.State

// GetRuntime returns the goja.Runtime for the current VU
GetRuntime() *goja.Runtime
// Runtime returns the goja.Runtime for the current VU
Runtime() *goja.Runtime
}

// Exports is representation of ESM exports of a module
Expand Down
32 changes: 16 additions & 16 deletions js/modulestest/modulestest.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ var _ modules.InstanceParams = &InstanceParams{}

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

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

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

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

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

0 comments on commit 0f3f7bc

Please sign in to comment.