Skip to content

Commit

Permalink
Fix race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Dec 3, 2024
1 parent dd0b93b commit 67731b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
6 changes: 1 addition & 5 deletions browser/breakpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/gorilla/websocket"

"github.com/grafana/xk6-browser/env"
)

Expand Down Expand Up @@ -108,11 +109,6 @@ func pauseOnBreakpoint(vu moduleVU) {
return
}

time.AfterFunc(5*time.Second, func() {
log.Printf("resuming at %v:%v", pos.Filename, pos.Line)
bp.resume()
})

log.Printf("pausing at %v:%v", pos.Filename, pos.Line)
bp.pause()
}
Expand Down
32 changes: 26 additions & 6 deletions browser/breakpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"

Expand All @@ -14,18 +15,35 @@ import (
)

type breakpointTest struct {
mu sync.Mutex
updated []breakpoint
resumeCalled bool
}

func (bpt *breakpointTest) update(breakpoints []breakpoint) {
bpt.mu.Lock()
defer bpt.mu.Unlock()
bpt.updated = breakpoints
}

func (bpt *breakpointTest) resume() {
bpt.mu.Lock()
defer bpt.mu.Unlock()
bpt.resumeCalled = true
}

func (bpt *breakpointTest) all() []breakpoint {
bpt.mu.Lock()
defer bpt.mu.Unlock()
return bpt.updated
}

func (bpt *breakpointTest) isResumeCalled() bool {
bpt.mu.Lock()
defer bpt.mu.Unlock()
return bpt.resumeCalled
}

func newBreakpointClientTest(
t *testing.T, serverHandler func(conn *websocket.Conn),
) (*breakpointClient, *breakpointTest) {
Expand Down Expand Up @@ -85,12 +103,14 @@ func TestBreakpointClient(t *testing.T) {
case <-time.After(1 * time.Second):
t.Fatalf("timeout waiting for server to handle the pause message")
}
require.Len(t, breakpoints.updated, 2)
assert.Equal(t, "file1.js", breakpoints.updated[0].File)
assert.Equal(t, 10, breakpoints.updated[0].Line)
assert.Equal(t, "file2.js", breakpoints.updated[1].File)
assert.Equal(t, 20, breakpoints.updated[1].Line)
assert.True(t, breakpoints.resumeCalled)
time.Sleep(5 * time.Second) // TODO: find a better way to wait for the message to be processed
items := breakpoints.all()
require.Len(t, items, 2)
assert.Equal(t, "file1.js", items[0].File)
assert.Equal(t, 10, items[0].Line)
assert.Equal(t, "file2.js", items[1].File)
assert.Equal(t, 20, items[1].Line)
assert.True(t, breakpoints.isResumeCalled())
}

func TestBreakpointClient_SendPause(t *testing.T) {
Expand Down

0 comments on commit 67731b6

Please sign in to comment.