Skip to content

Commit 19ef74b

Browse files
committed
fix: sequence the bun script's fetches so the ordering is observable
The script raced its two fetches, and /__webjs/ready answers immediately (it reports unready without blocking on the analysis), so the pair passed whether or not the hoist exists: the ready fetched at t=0 was 503 either way. Proven vacuous by running it against a server without the hoist, where it printed OK. Sequenced, the timeline discriminates. With the hoist the css returns in milliseconds, long before the fixture middleware's sleep releases the analysis, so the ready fetched after it is still 503. Without the hoist the css itself blocks on ensureReady, so by the time it returns the analysis is done and the same fetch reads 200. Verified in both directions: OK against this branch's server on node and bun, and the assertion fails against main's server. The middleware sleep widened from 1500ms to 3000ms so the discrimination has margin over css fetch latency.
1 parent 7532ac0 commit 19ef74b

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

test/bun/dev-public-before-warm.mjs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ try {
8080
writeFileSync(join(dir, 'app/page.ts'), "import { html } from '@webjsdev/core';\nexport default () => html`<h1>ok</h1>`;\n");
8181
writeFileSync(join(dir, 'public/a.css'), 'body{color:red}\n');
8282
// Top-level await, so the module does not finish evaluating (and therefore
83-
// `loadMiddleware`, and therefore `ensureReady()`, does not resolve) for 1.5s.
83+
// `loadMiddleware`, and therefore `ensureReady()`, does not resolve) for 3s, wide margin over the css fetch latency.
8484
writeFileSync(
8585
join(dir, 'middleware.ts'),
86-
'await new Promise((r) => setTimeout(r, 1500));\n'
86+
'await new Promise((r) => setTimeout(r, 3000));\n'
8787
+ 'export default async function middleware(req: Request, next: () => Promise<Response>) { return next(); }\n',
8888
);
8989
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'public-warm', type: 'module', imports: { '#*': './*' }, webjs: {} }));
@@ -104,18 +104,25 @@ try {
104104
const listening = await until(portAccepts, { timeoutMs: 30_000 });
105105
assert.ok(listening, `dev server never listened on ${runtime}\n--- server log ---\n${log}`);
106106

107-
// One pass: the CSS must answer while readiness is still gated on the
108-
// analysis. Both requests are issued together so neither waits on the other.
109-
const [css, ready] = await Promise.all([
110-
fetch(`${BASE}/public/a.css`),
111-
fetch(`${BASE}/__webjs/ready`),
112-
]);
107+
// ORDER of these two fetches is the whole assertion, so they are sequenced,
108+
// never raced. `/__webjs/ready` answers IMMEDIATELY (it reports unready
109+
// without blocking on the analysis), so a concurrent pair passes whether or
110+
// not the hoist exists: the css resolves whenever it resolves and the ready
111+
// fetched at t=0 was 503 either way. That exact vacuous shape shipped first
112+
// and passed against a server WITHOUT the hoist. Sequenced, the timeline
113+
// discriminates: with the hoist the css returns in milliseconds, long before
114+
// the fixture middleware's 3s sleep releases the analysis, so the ready
115+
// fetched AFTER it is still 503; without the hoist the css itself blocks on
116+
// `ensureReady()`, so by the time it returns the analysis is done and the
117+
// same ready fetch reads 200, failing the assertion below.
118+
const css = await fetch(`${BASE}/public/a.css`);
113119
const cssBody = await css.text();
120+
const ready = await fetch(`${BASE}/__webjs/ready`);
114121
assert.equal(css.status, 200, `/public/a.css was not served cold on ${runtime}\n--- server log ---\n${log}`);
115122
assert.equal(cssBody, 'body{color:red}\n', `/public/a.css served the wrong bytes on ${runtime}`);
116123
assert.equal(
117124
ready.status, 503,
118-
`the analysis had already completed on ${runtime}, so this proves nothing about ordering\n--- server log ---\n${log}`,
125+
`the analysis had already completed before the CSS was served on ${runtime}, so the hoist is not doing its job\n--- server log ---\n${log}`,
119126
);
120127

121128
console.log(`OK dev serves /public/* before the analysis completes on ${runtime} (#1397)`);

0 commit comments

Comments
 (0)