Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit ef68a20

Browse files
committed
Add pauseOnBreakpoint helper
1 parent 4348bfe commit ef68a20

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

browser/page_mapping.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop
135135
})
136136
},
137137
"goto": func(url string, opts sobek.Value) (*sobek.Promise, error) {
138-
bp := vu.breakpointRegistry
139-
pos := getCurrentLineNumber(vu)
140-
if bp.matches(pos) {
141-
time.AfterFunc(5*time.Second, func() { bp.resume(pos) })
142-
bp.pause(pos)
143-
}
138+
pauseOnBreakpoint(vu)
144139

145140
gopts := common.NewFrameGotoOptions(
146141
p.Referrer(),

browser/registry.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313
"sync"
1414
"sync/atomic"
15+
"time"
1516

1617
"github.com/mstoykov/k6-taskqueue-lib/taskqueue"
1718
"go.opentelemetry.io/otel/attribute"
@@ -76,20 +77,37 @@ func (b *breakpointRegistry) matches(p position) bool {
7677
}
7778

7879
// pause pauses the script execution.
79-
func (b *breakpointRegistry) pause(p position) {
80+
func (b *breakpointRegistry) pause() {
8081
c := make(chan struct{})
8182
b.pauser <- c
82-
fmt.Println("pausing at", p.Filename, p.Line)
8383
<-c
8484
}
8585

8686
// resume resumes the script execution
87-
func (b *breakpointRegistry) resume(p position) {
87+
func (b *breakpointRegistry) resume() {
8888
c := <-b.pauser
89-
fmt.Println("resuming at", p.Filename, p.Line)
9089
close(c)
9190
}
9291

92+
// pauseOnBreakpoint is a helper that pauses the script execution
93+
// when a breakpoint is hit in the script.
94+
func pauseOnBreakpoint(vu moduleVU) {
95+
bp := vu.breakpointRegistry
96+
97+
pos := getCurrentLineNumber(vu)
98+
if !bp.matches(pos) {
99+
return
100+
}
101+
102+
time.AfterFunc(5*time.Second, func() {
103+
fmt.Println("resuming at", pos.Filename, pos.Line)
104+
bp.resume()
105+
})
106+
107+
fmt.Println("pausing at", pos.Filename, pos.Line)
108+
bp.pause()
109+
}
110+
93111
// pidRegistry keeps track of the launched browser process IDs.
94112
type pidRegistry struct {
95113
mu sync.RWMutex

0 commit comments

Comments
 (0)