@@ -36,17 +36,7 @@ func TestLifecycleWaitForLoadStateLoad(t *testing.T) {
36
36
http .Redirect (w , r , tb .staticURL ("wait_for_nav_lifecycle.html" ), http .StatusMovedPermanently )
37
37
})
38
38
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 )
50
40
51
41
tb .withHandler ("/ping.js" , func (w http.ResponseWriter , _ * http.Request ) {
52
42
fmt .Fprintf (w , `
@@ -90,17 +80,7 @@ func TestLifecycleWaitForLoadStateDOMContentLoaded(t *testing.T) {
90
80
http .Redirect (w , r , tb .staticURL ("wait_for_nav_lifecycle.html" ), http .StatusMovedPermanently )
91
81
})
92
82
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 )
104
84
105
85
tb .withHandler ("/ping.js" , func (w http.ResponseWriter , _ * http.Request ) {
106
86
fmt .Fprintf (w , `
@@ -144,15 +124,7 @@ func TestLifecycleWaitForLoadStateNetworkIdle(t *testing.T) {
144
124
http .Redirect (w , r , tb .staticURL ("wait_for_nav_lifecycle.html" ), http .StatusMovedPermanently )
145
125
})
146
126
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 )
156
128
157
129
tb .withHandler ("/ping.js" , func (w http.ResponseWriter , _ * http.Request ) {
158
130
fmt .Fprintf (w , `
@@ -193,17 +165,7 @@ func TestLifecycleWaitForLoadStateDOMContentLoadedThenNetworkIdle(t *testing.T)
193
165
http .Redirect (w , r , tb .staticURL ("wait_for_nav_lifecycle.html" ), http .StatusMovedPermanently )
194
166
})
195
167
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 )
207
169
208
170
tb .withHandler ("/ping.js" , func (w http.ResponseWriter , _ * http.Request ) {
209
171
fmt .Fprintf (w , `
@@ -232,17 +194,7 @@ func TestLifecycleReloadLoad(t *testing.T) {
232
194
http .Redirect (w , r , tb .staticURL ("reload_lifecycle.html" ), http .StatusMovedPermanently )
233
195
})
234
196
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 )
246
198
247
199
tb .withHandler ("/ping.js" , func (w http.ResponseWriter , _ * http.Request ) {
248
200
fmt .Fprintf (w , `
@@ -282,17 +234,7 @@ func TestLifecycleReloadDOMContentLoaded(t *testing.T) {
282
234
http .Redirect (w , r , tb .staticURL ("reload_lifecycle.html" ), http .StatusMovedPermanently )
283
235
})
284
236
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 )
296
238
297
239
tb .withHandler ("/ping.js" , func (w http.ResponseWriter , _ * http.Request ) {
298
240
fmt .Fprintf (w , `
@@ -334,15 +276,7 @@ func TestLifecycleReloadNetworkIdle(t *testing.T) {
334
276
http .Redirect (w , r , tb .staticURL ("reload_lifecycle.html" ), http .StatusMovedPermanently )
335
277
})
336
278
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 )
346
280
347
281
tb .withHandler ("/ping.js" , func (w http.ResponseWriter , _ * http.Request ) {
348
282
fmt .Fprintf (w , `
@@ -415,20 +349,8 @@ func TestLifecycleNetworkIdle(t *testing.T) {
415
349
http .Redirect (w , r , tb .staticURL ("prolonged_network_idle.html" ), http .StatusMovedPermanently )
416
350
})
417
351
418
- var counter int64
419
352
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 )
432
354
433
355
tb .withHandler ("/ping.js" , func (w http.ResponseWriter , _ * http.Request ) {
434
356
fmt .Fprintf (w , `
@@ -456,17 +378,7 @@ func TestLifecycleNetworkIdle(t *testing.T) {
456
378
http .Redirect (w , r , tb .staticURL ("prolonged_network_idle_10.html" ), http .StatusMovedPermanently )
457
379
})
458
380
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 )
470
382
471
383
assertHome (t , tb , p , common .LifecycleEventNetworkIdle , func () {
472
384
result := p .TextContent ("#pingRequestText" , nil )
@@ -475,6 +387,26 @@ func TestLifecycleNetworkIdle(t *testing.T) {
475
387
})
476
388
}
477
389
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
+
478
410
func assertHome (
479
411
t * testing.T ,
480
412
tb * testBrowser ,
0 commit comments