Skip to content

Commit

Permalink
Set browser registry context from root module
Browse files Browse the repository at this point in the history
Modifies the browser registry constructor to accept a context that will
be used in order to build the browser context which will eventually be
inherited by the other browser components.
  • Loading branch information
ka3de committed Dec 11, 2023
1 parent 3659b8c commit 3e936af
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
3 changes: 2 additions & 1 deletion browser/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package browser

import (
"context"
"log"
"net/http"
_ "net/http/pprof" //nolint:gosec
Expand Down Expand Up @@ -67,7 +68,7 @@ func (m *RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
Browser: mapBrowserToGoja(moduleVU{
VU: vu,
pidRegistry: m.PidRegistry,
browserRegistry: newBrowserRegistry(vu, m.remoteRegistry, m.PidRegistry),
browserRegistry: newBrowserRegistry(context.Background(), vu, m.remoteRegistry, m.PidRegistry),
taskQueueRegistry: newTaskQueueRegistry(vu),
}),
Devices: common.GetDevices(),
Expand Down
12 changes: 8 additions & 4 deletions browser/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ type browserRegistry struct {

type browserBuildFunc func(ctx context.Context) (*common.Browser, error)

func newBrowserRegistry(vu k6modules.VU, remote *remoteRegistry, pids *pidRegistry) *browserRegistry {
func newBrowserRegistry(
ctx context.Context, vu k6modules.VU, remote *remoteRegistry, pids *pidRegistry,
) *browserRegistry {
bt := chromium.NewBrowserType(vu)
builder := func(ctx context.Context) (*common.Browser, error) {
var (
Expand Down Expand Up @@ -231,12 +233,14 @@ func newBrowserRegistry(vu k6modules.VU, remote *remoteRegistry, pids *pidRegist
}

go r.handleExitEvent(exitCh, unsubscribe)
go r.handleIterEvents(eventsCh, unsubscribe)
go r.handleIterEvents(ctx, eventsCh, unsubscribe)

return r
}

func (r *browserRegistry) handleIterEvents(eventsCh <-chan *k6event.Event, unsubscribeFn func()) {
func (r *browserRegistry) handleIterEvents( //nolint:funlen
ctx context.Context, eventsCh <-chan *k6event.Event, unsubscribeFn func(),
) {
var (
ok bool
data k6event.IterData
Expand Down Expand Up @@ -281,7 +285,7 @@ func (r *browserRegistry) handleIterEvents(eventsCh <-chan *k6event.Event, unsub

// Wrap the tracer into the browser context to make it accessible for the other
// components that inherit the context so these can use it to trace their actions.
tracerCtx := common.WithTracer(context.Background(), r.tr.tracer)
tracerCtx := common.WithTracer(ctx, r.tr.tracer)
tracedCtx := r.tr.startIterationTrace(tracerCtx, data)

b, err := r.buildFn(tracedCtx)
Expand Down
21 changes: 15 additions & 6 deletions browser/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,11 @@ func TestBrowserRegistry(t *testing.T) {
t.Run("init_and_close_browsers_on_iter_events", func(t *testing.T) {
t.Parallel()

vu := k6test.NewVU(t)
browserRegistry := newBrowserRegistry(vu, remoteRegistry, &pidRegistry{})
var (
ctx = context.Background()
vu = k6test.NewVU(t)
browserRegistry = newBrowserRegistry(ctx, vu, remoteRegistry, &pidRegistry{})
)

vu.ActivateVU()

Expand Down Expand Up @@ -242,8 +245,11 @@ func TestBrowserRegistry(t *testing.T) {
t.Run("close_browsers_on_exit_event", func(t *testing.T) {
t.Parallel()

vu := k6test.NewVU(t)
browserRegistry := newBrowserRegistry(vu, remoteRegistry, &pidRegistry{})
var (
ctx = context.Background()
vu = k6test.NewVU(t)
browserRegistry = newBrowserRegistry(ctx, vu, remoteRegistry, &pidRegistry{})
)

vu.ActivateVU()

Expand Down Expand Up @@ -274,8 +280,11 @@ func TestBrowserRegistry(t *testing.T) {
t.Run("unsubscribe_on_non_browser_vu", func(t *testing.T) {
t.Parallel()

vu := k6test.NewVU(t)
browserRegistry := newBrowserRegistry(vu, remoteRegistry, &pidRegistry{})
var (
ctx = context.Background()
vu = k6test.NewVU(t)
browserRegistry = newBrowserRegistry(ctx, vu, remoteRegistry, &pidRegistry{})
)

vu.ActivateVU()

Expand Down

0 comments on commit 3e936af

Please sign in to comment.