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

Commit

Permalink
Fix clearLifecycle for goto
Browse files Browse the repository at this point in the history
When we navigate to a page, lifecycle events were being stored. If a
new navigation occurred, the previous events would be used and fired
off to the internal handles. This is incorrect as it means that
WaitUntil will fail to wait for the correct event after the initial
navigation to a page.

Partly fixes: https://github.com/orgs/grafana/projects/80/views/14
  • Loading branch information
ankur22 committed Oct 19, 2022
1 parent d81d9aa commit d4c6087
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
6 changes: 1 addition & 5 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ func (f *Frame) clearLifecycle() {

// clear lifecycle events
f.lifecycleEventsMu.Lock()
{
for e := range f.lifecycleEvents {
f.lifecycleEvents[e] = false
}
}
f.lifecycleEvents = make(map[LifecycleEvent]bool)
f.lifecycleEventsMu.Unlock()

f.page.frameManager.MainFrame().recalculateLifecycle()
Expand Down
91 changes: 91 additions & 0 deletions tests/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"fmt"
"net/http"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -81,4 +82,94 @@ func TestLifecycleNetworkIdle(t *testing.T) {
assert.True(t, resolved)
assert.False(t, rejected)
})

t.Run("doesn't unblock wait for networkIdle too early", func(t *testing.T) {
t.Parallel()

tb := newTestBrowser(t, withHTTPServer())
p := tb.NewPage(nil)

counterMu := sync.RWMutex{}
var counter int

tb.withHandler("/home", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(w, `
<html>
<head></head>
<body>
<div id="prolongNetworkIdleLoad">Waiting...</div>
<div id="serverMsg">Waiting...</div>
<script>
var prolongNetworkIdleLoadOutput = document.getElementById("prolongNetworkIdleLoad");
var p = prolongNetworkIdleLoad();
p.then(() => {
prolongNetworkIdleLoadOutput.innerText += ' - for loop complete';
})
async function prolongNetworkIdleLoad() {
for (var i = 0; i < 4; i++) {
await fetch('/ping')
.then(response => response.text())
.then((data) => {
prolongNetworkIdleLoadOutput.innerText = 'Waiting... ' + data;
});
}
}
</script>
<script src="/ping.js" async></script>
</body>
</html>
`)
})
ch := make(chan bool)
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
<-ch

counterMu.Lock()
defer counterMu.Unlock()

time.Sleep(time.Millisecond * 50)

counter++
fmt.Fprintf(w, "pong %d", counter)
})
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(w, `
var serverMsgOutput = document.getElementById("serverMsg");
serverMsgOutput.innerText = "ping.js loaded from server";
`)

close(ch)
})

var resolved, rejected bool
err := tb.await(func() error {
opts := tb.toGojaValue(common.FrameGotoOptions{
WaitUntil: common.LifecycleEventNetworkIdle,
Timeout: 30 * time.Second,
})
tb.promise(p.Goto(tb.URL("/home"), opts)).then(
func() {
result := p.TextContent("#prolongNetworkIdleLoad", nil)
assert.EqualValues(t, "Waiting... pong 4 - for loop complete", result)

result = p.TextContent("#serverMsg", nil)
assert.EqualValues(t, "ping.js loaded from server", result)

resolved = true
},
func() {
rejected = true
},
)

return nil
})
require.NoError(t, err)

assert.True(t, resolved)
assert.False(t, rejected)
})
}

0 comments on commit d4c6087

Please sign in to comment.