Skip to content

Commit f1a664b

Browse files
committed
test: give servers under test a realistic startup budget
1 parent a1a4cb9 commit f1a664b

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

‎packages/nuxt-cli/test/e2e/commands.spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('commands', () => {
9797
})
9898

9999
// Test that server responds
100-
constresponse=awaitfetchWithPolling(`http://127.0.0.1:${port}`,{},30,300)
100+
constresponse=awaitfetchWithPolling(`http://127.0.0.1:${port}`)
101101
expect.soft(response?.status).toBe(200)
102102

103103
controller.abort()
@@ -190,7 +190,7 @@ describe('extends support', () => {
190190
})
191191

192192
// Test that server responds
193-
constresponse=awaitfetchWithPolling(`http://127.0.0.1:${port}/extended`,{},30,300)
193+
constresponse=awaitfetchWithPolling(`http://127.0.0.1:${port}/extended`)
194194
expect.soft(response?.status).toBe(200)
195195
expect(awaitresponse?.text()).toContain('This is an extended page from a layer.')
196196

‎packages/nuxt-cli/test/utils/index.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import { join, sep } from 'node:path'
33
import{fileURLToPath}from'node:url'
44
import{isCI}from'std-env'
55

6-
exportasyncfunctionfetchWithPolling(url: string,options: RequestInit={},maxAttempts=10,interval=100): Promise<Response|null>{
6+
/**
7+
* Poll `url` until it answers, allowing for a server that is still booting.
8+
*
9+
* The deadline is a wall-clock budget rather than an attempt count, so a slow
10+
* machine gets the same number of seconds as a fast one.
11+
*/
12+
exportasyncfunctionfetchWithPolling(url: string,options: RequestInit={},timeout=isCI ? 120_000 : 30_000,interval=100): Promise<Response|null>{
13+
constdeadline=Date.now()+timeout
714
letresponse: Response|null=null
8-
letattempts=0
9-
while(attempts<maxAttempts){
15+
do{
1016
try{
1117
response=awaitfetch(url,options)
1218
if(response.ok){
@@ -16,9 +22,8 @@ export async function fetchWithPolling(url: string, options: RequestInit = {}, m
1622
catch{
1723
// Ignore errors and retry
1824
}
19-
attempts++
20-
awaitnewPromise(resolve=>setTimeout(resolve,isCI ? interval*10 : interval))
21-
}
25+
awaitnewPromise(resolve=>setTimeout(resolve,interval))
26+
}while(Date.now()<deadline)
2227
returnresponse
2328
}
2429

0 commit comments

Comments
 (0)