Skip to content

Commit

Permalink
Initialize TaskQueue only when page.on() is called
Browse files Browse the repository at this point in the history
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 28, 2023
1 parent 26bab38 commit 2548cc3
Showing 1 changed file with 13 additions and 4 deletions.
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

0 comments on commit 2548cc3

Please sign in to comment.