Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate context-based utils #2384

Merged
merged 2 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ type ContextKey int

const ctxKeyEngine = ContextKey(1)

// WithEngine sets the k6 running Engine in the under the hood context.
//
// Deprecated: Use directly the Engine as dependency.
func WithEngine(ctx context.Context, engine *core.Engine) context.Context {
return context.WithValue(ctx, ctxKeyEngine, engine)
}

// GetEngine returns the k6 running Engine fetching it from the context.
//
// Deprecated: Use directly the Engine as dependency.
func GetEngine(ctx context.Context) *core.Engine {
return ctx.Value(ctxKeyEngine).(*core.Engine)
}
3 changes: 3 additions & 0 deletions js/common/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func BindToGlobal(rt *goja.Runtime, data map[string]interface{}) func() {
}

// Bind the provided value v to the provided runtime
//
// Deprecated: JS modules can implement the modules.VU interface for getting the context,
// goja runtime and the VU State, so the goja.Runtime.Set method can be used for data binding.
func Bind(rt *goja.Runtime, v interface{}, ctxPtr *context.Context) map[string]interface{} {
na-- marked this conversation as resolved.
Show resolved Hide resolved
exports := make(map[string]interface{})

Expand Down
11 changes: 11 additions & 0 deletions js/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (
"github.com/dop251/goja"
)

// TODO: https://github.com/grafana/k6/issues/2385
// Rid all the context-based utils functions

type ctxKey int

const (
Expand All @@ -34,11 +37,15 @@ const (
)

// WithRuntime attaches the given goja runtime to the context.
//
// Deprecated: Implement the modules.VU interface for sharing the Runtime.
func WithRuntime(ctx context.Context, rt *goja.Runtime) context.Context {
return context.WithValue(ctx, ctxKeyRuntime, rt)
}

// GetRuntime retrieves the attached goja runtime from the given context.
//
// Deprecated: Use modules.VU for get the Runtime.
func GetRuntime(ctx context.Context) *goja.Runtime {
v := ctx.Value(ctxKeyRuntime)
if v == nil {
Expand All @@ -48,11 +55,15 @@ func GetRuntime(ctx context.Context) *goja.Runtime {
}

// WithInitEnv attaches the given init environment to the context.
//
// Deprecated: Implement the modules.VU interface for sharing the init environment.
func WithInitEnv(ctx context.Context, initEnv *InitEnvironment) context.Context {
return context.WithValue(ctx, ctxKeyInitEnv, initEnv)
}

// GetInitEnv retrieves the attached init environment struct from the given context.
//
// Deprecated: Use modules.VU for get the init environment.
func GetInitEnv(ctx context.Context) *InitEnvironment {
v := ctx.Value(ctxKeyInitEnv)
if v == nil {
Expand Down
13 changes: 10 additions & 3 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"

"github.com/dop251/goja"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -201,6 +202,9 @@ func toESModuleExports(exp modules.Exports) interface{} {
return result
}

// TODO: https://github.com/grafana/k6/issues/2385
var onceBindWarning sync.Once //nolint: gochecknoglobals

func (i *InitContext) requireModule(name string) (goja.Value, error) {
mod, ok := i.modules[name]
if !ok {
Expand All @@ -210,9 +214,12 @@ func (i *InitContext) requireModule(name string) (goja.Value, error) {
instance := m.NewModuleInstance(i.moduleVUImpl)
return i.moduleVUImpl.runtime.ToValue(toESModuleExports(instance.Exports())), nil
}
if perInstance, ok := mod.(modules.HasModuleInstancePerVU); ok {
mod = perInstance.NewModuleInstancePerVU()
}

onceBindWarning.Do(func() {
i.logger.Warnf(`Module '%s' is using deprecated APIs that will be removed in k6 v0.38.0,`+
` for more details on how to update it see`+
` https://k6.io/docs/extensions/guides/create-an-extension/#advanced-javascript-extension`, name)
})

return i.moduleVUImpl.runtime.ToValue(common.Bind(i.moduleVUImpl.runtime, mod, i.moduleVUImpl.ctxPtr)), nil
}
Expand Down
7 changes: 0 additions & 7 deletions js/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ func Register(name string, mod interface{}) {
modules[name] = mod
}

// HasModuleInstancePerVU should be implemented by all native Golang modules that
// would require per-VU state. k6 will call their NewModuleInstancePerVU() methods
// every time a VU imports the module and use its result as the returned object.
type HasModuleInstancePerVU interface {
NewModuleInstancePerVU() interface{}
}

// Module is the interface js modules should implement in order to get access to the VU
type Module interface {
// NewModuleInstance will get modules.VU that should provide the module with a way to interact with the VU
Expand Down
11 changes: 10 additions & 1 deletion lib/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

package lib

import "context"
import (
"context"
)

type ctxKey int

Expand All @@ -30,12 +32,19 @@ const (
ctxKeyScenario
)

// TODO: https://github.com/grafana/k6/issues/2385
// Rid the State's context-based utils functions

// WithState embeds a State in ctx.
//
// Deprecated: Implement the modules.VU interface for sharing the State.
func WithState(ctx context.Context, state *State) context.Context {
return context.WithValue(ctx, ctxKeyState, state)
}

// GetState returns a State from ctx.
//
// Deprecated: Use modules.VU for get the State.
func GetState(ctx context.Context) *State {
v := ctx.Value(ctxKeyState)
if v == nil {
Expand Down