From adc9343e5bc8936d59f35dfe864808a94b55db4b Mon Sep 17 00:00:00 2001 From: ka3de Date: Mon, 28 Aug 2023 09:08:37 +0200 Subject: [PATCH] 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. --- common/page.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/common/page.go b/common/page.go index c79a19673..0ca09a40c 100644 --- a/common/page.go +++ b/common/page.go @@ -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, } @@ -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 { @@ -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()