Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Attach k6 VU to context
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed Mar 10, 2022
1 parent 0f468ea commit e5076d5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
7 changes: 3 additions & 4 deletions common/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/chromedp/cdproto/target"
"github.com/dop251/goja"
"github.com/gorilla/websocket"
k6common "go.k6.io/k6/js/common"

"github.com/grafana/xk6-browser/api"
)
Expand Down Expand Up @@ -456,10 +455,10 @@ func (b *Browser) On(event string) *goja.Promise {
k6Throw(b.ctx, "unknown browser event: %q, must be %q", event, EventBrowserDisconnected)
}

rt := k6common.GetRuntime(b.ctx) //nolint: staticcheck // SA1019 (deprecated)
rcb := GetRegisterCallback(b.ctx)
vu := GetVU(b.ctx)
rt := vu.Runtime()
cb := vu.RegisterCallback()
p, resolve, reject := rt.NewPromise()
cb := rcb()

go func() {
select {
Expand Down
30 changes: 17 additions & 13 deletions common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@

package common

import "context"
import (
"context"

k6modules "go.k6.io/k6/js/modules"
)

type ctxKey int

const (
ctxKeyLaunchOptions ctxKey = iota
ctxKeyPid
ctxKeyHooks
ctxKeyCallback
ctxKeyVU
)

func WithHooks(ctx context.Context, hooks *Hooks) context.Context {
Expand Down Expand Up @@ -66,19 +70,19 @@ func GetProcessID(ctx context.Context) int {
return v // it will return zero on error
}

// WithRegisterCallback adds the function to register an event loop callback to ctx.
func WithRegisterCallback(ctx context.Context, rcb RegisterCallbackFn) context.Context {
return context.WithValue(ctx, ctxKeyCallback, rcb)
// WithVU returns a new context based on ctx with the k6 VU instance attached.
func WithVU(ctx context.Context, vu k6modules.VU) context.Context {
return context.WithValue(ctx, ctxKeyVU, vu)
}

// GetRegisterCallback returns the function to register an event loop callback from ctx.
func GetRegisterCallback(ctx context.Context) RegisterCallbackFn {
v := ctx.Value(ctxKeyCallback)
if v == nil {
return nil
}
if rcb, ok := v.(RegisterCallbackFn); ok {
return rcb
// GetVU returns the attached k6 VU instance from ctx, which can be used to
// retrieve the goja runtime and other k6 objects relevant to the currently
// executing VU.
// See https://github.com/grafana/k6/blob/v0.38.0/js/initcontext.go#L168-L186
func GetVU(ctx context.Context) k6modules.VU {
v := ctx.Value(ctxKeyVU)
if vu, ok := v.(k6modules.VU); ok {
return vu
}
return nil
}
Expand Down
3 changes: 0 additions & 3 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ import (
"github.com/grafana/xk6-browser/api"
)

// RegisterCallbackFn represents the function used to register an event loop callback.
type RegisterCallbackFn func() func(func() error)

// ColorScheme represents a browser color scheme.
type ColorScheme string

Expand Down
16 changes: 7 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ type (

// JSModule is the entrypoint into the browser JS module.
JSModule struct {
vu k6modules.VU
registerCallback common.RegisterCallbackFn
Devices map[string]common.Device
Version string
vu k6modules.VU
Devices map[string]common.Device
Version string
}

// ModuleInstance represents an instance of the JS module.
Expand All @@ -68,10 +67,9 @@ func New() *RootModule {
func (*RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
return &ModuleInstance{
mod: &JSModule{
vu: vu,
registerCallback: vu.RegisterCallback,
Devices: common.GetDevices(),
Version: version,
vu: vu,
Devices: common.GetDevices(),
Version: version,
},
}
}
Expand All @@ -93,7 +91,7 @@ func (m *JSModule) Launch(browserName string, opts goja.Value) api.Browser {
<-ctx.Done()
}()*/

ctx := common.WithRegisterCallback(m.vu.Context(), m.registerCallback)
ctx := common.WithVU(m.vu.Context(), m.vu)

if browserName == "chromium" {
bt := chromium.NewBrowserType(ctx)
Expand Down

0 comments on commit e5076d5

Please sign in to comment.