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

Commit 26a0687

Browse files
committed
Refactor lifecycle tests ping handler
We're deduplicating the ping handler to make it simpler to read the tests.
1 parent 54b570e commit 26a0687

File tree

1 file changed

+29
-97
lines changed

1 file changed

+29
-97
lines changed

tests/lifecycle_wait_test.go

Lines changed: 29 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,7 @@ func TestLifecycleWaitForLoadStateLoad(t *testing.T) {
3636
http.Redirect(w, r, tb.staticURL("wait_for_nav_lifecycle.html"), http.StatusMovedPermanently)
3737
})
3838

39-
var counter int64
40-
var counterMu sync.Mutex
41-
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
42-
counterMu.Lock()
43-
defer counterMu.Unlock()
44-
45-
time.Sleep(time.Millisecond * 100)
46-
47-
counter++
48-
fmt.Fprintf(w, "pong %d", counter)
49-
})
39+
withPingHandler(t, tb, time.Millisecond*100, nil)
5040

5141
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
5242
fmt.Fprintf(w, `
@@ -90,17 +80,7 @@ func TestLifecycleWaitForLoadStateDOMContentLoaded(t *testing.T) {
9080
http.Redirect(w, r, tb.staticURL("wait_for_nav_lifecycle.html"), http.StatusMovedPermanently)
9181
})
9282

93-
var counter int64
94-
var counterMu sync.Mutex
95-
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
96-
counterMu.Lock()
97-
defer counterMu.Unlock()
98-
99-
time.Sleep(time.Millisecond * 100)
100-
101-
counter++
102-
fmt.Fprintf(w, "pong %d", counter)
103-
})
83+
withPingHandler(t, tb, time.Millisecond*100, nil)
10484

10585
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
10686
fmt.Fprintf(w, `
@@ -144,15 +124,7 @@ func TestLifecycleWaitForLoadStateNetworkIdle(t *testing.T) {
144124
http.Redirect(w, r, tb.staticURL("wait_for_nav_lifecycle.html"), http.StatusMovedPermanently)
145125
})
146126

147-
var counter int64
148-
var counterMu sync.Mutex
149-
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
150-
counterMu.Lock()
151-
defer counterMu.Unlock()
152-
153-
counter++
154-
fmt.Fprintf(w, "pong %d", counter)
155-
})
127+
withPingHandler(t, tb, 0, nil)
156128

157129
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
158130
fmt.Fprintf(w, `
@@ -193,17 +165,7 @@ func TestLifecycleWaitForLoadStateDOMContentLoadedThenNetworkIdle(t *testing.T)
193165
http.Redirect(w, r, tb.staticURL("wait_for_nav_lifecycle.html"), http.StatusMovedPermanently)
194166
})
195167

196-
var counter int64
197-
var counterMu sync.Mutex
198-
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
199-
counterMu.Lock()
200-
defer counterMu.Unlock()
201-
202-
time.Sleep(time.Millisecond * 100)
203-
204-
counter++
205-
fmt.Fprintf(w, "pong %d", counter)
206-
})
168+
withPingHandler(t, tb, time.Millisecond*100, nil)
207169

208170
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
209171
fmt.Fprintf(w, `
@@ -232,17 +194,7 @@ func TestLifecycleReloadLoad(t *testing.T) {
232194
http.Redirect(w, r, tb.staticURL("reload_lifecycle.html"), http.StatusMovedPermanently)
233195
})
234196

235-
var counter int64
236-
var counterMu sync.Mutex
237-
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
238-
counterMu.Lock()
239-
defer counterMu.Unlock()
240-
241-
time.Sleep(time.Millisecond * 100)
242-
243-
counter++
244-
fmt.Fprintf(w, "pong %d", counter)
245-
})
197+
withPingHandler(t, tb, time.Millisecond*100, nil)
246198

247199
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
248200
fmt.Fprintf(w, `
@@ -282,17 +234,7 @@ func TestLifecycleReloadDOMContentLoaded(t *testing.T) {
282234
http.Redirect(w, r, tb.staticURL("reload_lifecycle.html"), http.StatusMovedPermanently)
283235
})
284236

285-
var counter int64
286-
var counterMu sync.Mutex
287-
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
288-
counterMu.Lock()
289-
defer counterMu.Unlock()
290-
291-
time.Sleep(time.Millisecond * 100)
292-
293-
counter++
294-
fmt.Fprintf(w, "pong %d", counter)
295-
})
237+
withPingHandler(t, tb, time.Millisecond*100, nil)
296238

297239
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
298240
fmt.Fprintf(w, `
@@ -334,15 +276,7 @@ func TestLifecycleReloadNetworkIdle(t *testing.T) {
334276
http.Redirect(w, r, tb.staticURL("reload_lifecycle.html"), http.StatusMovedPermanently)
335277
})
336278

337-
var counter int64
338-
var counterMu sync.Mutex
339-
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
340-
counterMu.Lock()
341-
defer counterMu.Unlock()
342-
343-
counter++
344-
fmt.Fprintf(w, "pong %d", counter)
345-
})
279+
withPingHandler(t, tb, 0, nil)
346280

347281
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
348282
fmt.Fprintf(w, `
@@ -415,20 +349,8 @@ func TestLifecycleNetworkIdle(t *testing.T) {
415349
http.Redirect(w, r, tb.staticURL("prolonged_network_idle.html"), http.StatusMovedPermanently)
416350
})
417351

418-
var counter int64
419352
ch := make(chan bool)
420-
var counterMu sync.Mutex
421-
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
422-
<-ch
423-
424-
counterMu.Lock()
425-
defer counterMu.Unlock()
426-
427-
time.Sleep(time.Millisecond * 50)
428-
429-
counter++
430-
fmt.Fprintf(w, "pong %d", counter)
431-
})
353+
withPingHandler(t, tb, time.Millisecond*50, ch)
432354

433355
tb.withHandler("/ping.js", func(w http.ResponseWriter, _ *http.Request) {
434356
fmt.Fprintf(w, `
@@ -456,17 +378,7 @@ func TestLifecycleNetworkIdle(t *testing.T) {
456378
http.Redirect(w, r, tb.staticURL("prolonged_network_idle_10.html"), http.StatusMovedPermanently)
457379
})
458380

459-
var counterMu sync.Mutex
460-
var counter int64
461-
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
462-
counterMu.Lock()
463-
defer counterMu.Unlock()
464-
465-
time.Sleep(time.Millisecond * 50)
466-
467-
counter++
468-
fmt.Fprintf(w, "pong %d", counter)
469-
})
381+
withPingHandler(t, tb, time.Millisecond*50, nil)
470382

471383
assertHome(t, tb, p, common.LifecycleEventNetworkIdle, func() {
472384
result := p.TextContent("#pingRequestText", nil)
@@ -475,6 +387,26 @@ func TestLifecycleNetworkIdle(t *testing.T) {
475387
})
476388
}
477389

390+
func withPingHandler(t *testing.T, tb *testBrowser, slow time.Duration, ch chan bool) {
391+
t.Helper()
392+
393+
var counter int64
394+
var counterMu sync.Mutex
395+
tb.withHandler("/ping", func(w http.ResponseWriter, _ *http.Request) {
396+
if ch != nil {
397+
<-ch
398+
}
399+
400+
counterMu.Lock()
401+
defer counterMu.Unlock()
402+
403+
time.Sleep(slow)
404+
405+
counter++
406+
fmt.Fprintf(w, "pong %d", counter)
407+
})
408+
}
409+
478410
func assertHome(
479411
t *testing.T,
480412
tb *testBrowser,

0 commit comments

Comments
 (0)