Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.
Merged
Changes from 1 commit
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
Prev Previous commit
Initialize TaskQueue only when page.on() is called
In this way, we only require users to close the page in order to be able
to finish the iteration when page.on() method is used in the test. If
page.on() method is not used, the iteration will end normally whether
page.close() is called or not.
  • Loading branch information
ka3de committed Aug 29, 2023
commit bb4a1f621e114cb484fe6d27816f2d3d89b1cfbe
17 changes: 13 additions & 4 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ func NewPage(
workers: make(map[target.SessionID]*Worker),
routes: make([]api.Route, 0),
vu: k6ext.GetVU(ctx),
tq: taskqueue.New(k6ext.GetVU(ctx).RegisterCallback),
logger: logger,
}

Expand Down Expand Up @@ -187,9 +186,12 @@ func (p *Page) initEvents() {
defer func() {
p.logger.Debugf("Page:initEvents:go:return",
"sid:%v tid:%v", p.session.ID(), p.targetID)
// The TaskQueue must be closed in order to let
// the event loop finish
p.tq.Close()
// TaskQueue is only initialized when calling page.on() method
// so users are not always required to close the page in order
// to let the iteration finish.
if p.tq != nil {
p.tq.Close()
}
}()

for {
Expand Down Expand Up @@ -800,6 +802,13 @@ func (p *Page) On(event string, handler func(*api.ConsoleMessage) error) error {
return fmt.Errorf("unknown page event: %q, must be %q", event, eventPageConsoleAPICalled)
}

// Once the TaskQueue is initialized, it has to be closed so the event loop can finish.
// Therefore, instead of doing it in the constructor, we initialize it only when page.on()
// is called, so the user is only required to close the page it using this method.
if p.tq == nil {
p.tq = taskqueue.New(p.vu.RegisterCallback)
}

p.eventHandlersMu.Lock()
defer p.eventHandlersMu.Unlock()

Expand Down