Skip to content

Commit

Permalink
Add pauseOnBreakpoint helper
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Dec 3, 2024
1 parent 4348bfe commit ef68a20
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
7 changes: 1 addition & 6 deletions browser/page_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
26 changes: 22 additions & 4 deletions browser/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"sync"
"sync/atomic"
"time"

"github.com/mstoykov/k6-taskqueue-lib/taskqueue"
"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ef68a20

Please sign in to comment.