From 67731b61ddff842a8e45dba30824dfa88664ecf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 3 Dec 2024 13:54:57 -0500 Subject: [PATCH] Fix race conditions --- browser/breakpoint.go | 6 +----- browser/breakpoint_test.go | 32 ++++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/browser/breakpoint.go b/browser/breakpoint.go index 7797d1cce..6671e87b1 100644 --- a/browser/breakpoint.go +++ b/browser/breakpoint.go @@ -12,6 +12,7 @@ import ( "time" "github.com/gorilla/websocket" + "github.com/grafana/xk6-browser/env" ) @@ -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() } diff --git a/browser/breakpoint_test.go b/browser/breakpoint_test.go index 6e0a62c4e..409c59dd5 100644 --- a/browser/breakpoint_test.go +++ b/browser/breakpoint_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "sync" "testing" "time" @@ -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) { @@ -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) {