Skip to content

Commit

Permalink
Refactor to work with taskqueueRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur22 committed Oct 18, 2023
1 parent fb900e1 commit d422aba
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 35 deletions.
29 changes: 3 additions & 26 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

"github.com/dop251/goja"
"github.com/mstoykov/k6-taskqueue-lib/taskqueue"

"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6error"
Expand Down Expand Up @@ -434,13 +433,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping {
})
},
"close": func(opts goja.Value) error {
vu.tqMu.Lock()
tq := vu.tq[p.GetTargetID()]
if tq != nil {
tq.Close()
delete(vu.tq, p.GetTargetID())
}
vu.tqMu.Unlock()
vu.taskQueueRegistry.close(p.GetTargetID())

return p.Close(opts) //nolint:wrapcheck
},
Expand Down Expand Up @@ -514,28 +507,20 @@ func mapPage(vu moduleVU, p *common.Page) mapping {
},
"mouse": rt.ToValue(p.GetMouse()).ToObject(rt),
"on": func(event string, handler goja.Callable) error {
vu.tqMu.Lock()
tq := vu.tq[p.GetTargetID()]
if tq == nil {
tq = taskqueue.New(vu.RegisterCallback)
vu.tq[p.GetTargetID()] = tq
}
vu.tqMu.Unlock()
tq := vu.taskQueueRegistry.get(p.GetTargetID())

mapMsgAndHandleEvent := func(m *common.ConsoleMessage) error {
mapping := mapConsoleMessage(vu, m)
_, err := handler(goja.Undefined(), vu.Runtime().ToValue(mapping))
return err
}
runInTaskQueue := func(m *common.ConsoleMessage) {
vu.tqMu.RLock()
tq.Queue(func() error {
if err := mapMsgAndHandleEvent(m); err != nil {
return fmt.Errorf("executing page.on handler: %w", err)
}
return nil
})
vu.tqMu.RUnlock()
}

return p.On(event, runInTaskQueue) //nolint:wrapcheck
Expand Down Expand Up @@ -686,29 +671,21 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
var runInTaskQueue func(p *common.Page) (bool, error)
if parsedOpts.PredicateFn != nil {
runInTaskQueue = func(p *common.Page) (bool, error) {
vu.tqMu.Lock()
tq := vu.tq[p.GetTargetID()]
if tq == nil {
tq = taskqueue.New(vu.RegisterCallback)
vu.tq[p.GetTargetID()] = tq
}
vu.tqMu.Unlock()
tq := vu.taskQueueRegistry.get(p.GetTargetID())

var rtn bool
var err error
// The function on the taskqueue runs in its own goroutine
// so we need to use a channel to wait for it to complete
// before returning the result to the caller.
c := make(chan bool)
vu.tqMu.RLock()
tq.Queue(func() error {
var resp goja.Value
resp, err = parsedOpts.PredicateFn(vu.Runtime().ToValue(p))
rtn = resp.ToBoolean()
close(c)
return nil
})
vu.tqMu.RUnlock()
<-c

return rtn, err //nolint:wrapcheck
Expand Down
3 changes: 0 additions & 3 deletions browser/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sync"

"github.com/dop251/goja"
"github.com/mstoykov/k6-taskqueue-lib/taskqueue"

"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/env"
Expand Down Expand Up @@ -68,8 +67,6 @@ func (m *RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
VU: vu,
pidRegistry: m.PidRegistry,
browserRegistry: newBrowserRegistry(vu, m.remoteRegistry, m.PidRegistry),
tqMu: &sync.RWMutex{},
tq: make(map[string]*taskqueue.TaskQueue),
taskQueueRegistry: newTaskQueueRegistry(vu),
}),
Devices: common.GetDevices(),
Expand Down
6 changes: 0 additions & 6 deletions browser/modulevu.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package browser

import (
"context"
"sync"

"github.com/mstoykov/k6-taskqueue-lib/taskqueue"

"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6ext"
Expand All @@ -22,9 +19,6 @@ type moduleVU struct {
*pidRegistry
*browserRegistry

tqMu *sync.RWMutex
tq map[string]*taskqueue.TaskQueue

*taskQueueRegistry
}

Expand Down

0 comments on commit d422aba

Please sign in to comment.