diff --git a/browser/page_mapping.go b/browser/page_mapping.go index da22b8b2e..1b202fcae 100644 --- a/browser/page_mapping.go +++ b/browser/page_mapping.go @@ -135,12 +135,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }) }, "goto": func(url string, opts sobek.Value) (*sobek.Promise, error) { - bp := vu.breakpointRegistry - pos := getCurrentLineNumber(vu) - if bp.matches(pos) { - time.AfterFunc(5*time.Second, func() { bp.resume(pos) }) - bp.pause(pos) - } + pauseOnBreakpoint(vu) gopts := common.NewFrameGotoOptions( p.Referrer(), diff --git a/browser/registry.go b/browser/registry.go index c004d90c5..b0698948a 100644 --- a/browser/registry.go +++ b/browser/registry.go @@ -12,6 +12,7 @@ import ( "strings" "sync" "sync/atomic" + "time" "github.com/mstoykov/k6-taskqueue-lib/taskqueue" "go.opentelemetry.io/otel/attribute" @@ -76,20 +77,37 @@ func (b *breakpointRegistry) matches(p position) bool { } // pause pauses the script execution. -func (b *breakpointRegistry) pause(p position) { +func (b *breakpointRegistry) pause() { c := make(chan struct{}) b.pauser <- c - fmt.Println("pausing at", p.Filename, p.Line) <-c } // resume resumes the script execution -func (b *breakpointRegistry) resume(p position) { +func (b *breakpointRegistry) resume() { c := <-b.pauser - fmt.Println("resuming at", p.Filename, p.Line) close(c) } +// pauseOnBreakpoint is a helper that pauses the script execution +// when a breakpoint is hit in the script. +func pauseOnBreakpoint(vu moduleVU) { + bp := vu.breakpointRegistry + + pos := getCurrentLineNumber(vu) + if !bp.matches(pos) { + return + } + + time.AfterFunc(5*time.Second, func() { + fmt.Println("resuming at", pos.Filename, pos.Line) + bp.resume() + }) + + fmt.Println("pausing at", pos.Filename, pos.Line) + bp.pause() +} + // pidRegistry keeps track of the launched browser process IDs. type pidRegistry struct { mu sync.RWMutex