test: fixed race condition on max_dl*
Massimo Melina committed
Apr 19, 2025 at 11:25 UTC
3e6bd73fad9e48d800119ca15cca1c5ad04ff207
2 files changed
+17
-11
dev.md
+1
-1
@@ -49,7 +49,7 @@ Additionally, you have the following folders:
49
50
# Known problems
51
- vite's proxying server (but also CRA's) doesn't play nicely with SSE, leaving sockets open
52
-- automatic tests max_dl* are subject to race conditions and may occasionally fail
52
+- automatic tests 'upload.interrupted' is subject to race conditions and may occasionally fail
53
54
# Guidelines
55
tests/test.ts
+16
-10
@@ -272,17 +272,23 @@ function reqUpload(dest: string, tester: Tester, body?: string | Readable, size?
272
}
273
274
async function testMaxDl(uri: string, good: number, bad: number) {
275
- let i = 0
276
- const reqs = []
277
- while (good--)
278
- reqs.push( req(uri + '?' + (++i), 200, { throttle })() )
279
- await wait(1) // ensure the requests are worked by hfs before the next ones, and slots are taken. This is subject to race conditions: if the operations take less than this, the test will fail
280
- while (bad--)
281
- reqs.push( req(uri + '?' + (++i), 429, { throttle })() )
282
- await Promise.all(reqs)
275
+ // make good+bad requests, and check results
276
+ await Promise.all(_.range(good + bad).map(i => req(uri + '?' + i, (_data, res) => {
277
+ if (res.statusCode === 429) {
278
+ if (!bad--)
279
+ throw "too many refused"
280
+ return
281
+ }
282
+ if (res.statusCode === 200) {
283
+ if (!good--)
284
+ throw "too many accepted"
285
+ return
286
+ }
287
+ throw "unexpected status " + res.statusCode
288
+ }, { throttle })() )) // slow down to ensure the attempted downloads are all concurrent
289
}
290
285
-type TesterFunction = ((data: any, fullResponse: any) => boolean)
291
+type TesterFunction = ((data: any, fullResponse: any) => boolean | void)
292
type Tester = number
293
| TesterFunction
294
| RegExp
@@ -344,7 +350,7 @@ function req(url: string, test:Tester, { baseUrl, throttle, ...requestOptions }:
350
throw Error(err)
351
}
352
if (typeof test === 'function')
347
- if (!test(obj ?? data, res))
353
+ if (test(obj ?? data, res) === false)
354
throw Error("failed test: " + test)
355
return obj ?? data
356
}