Skip to content

Commit 4a174b8

Browse files
dgp1130atscott
authored andcommitted
test(platform-server): fix race condition in incremental hydration test
The test was using a brittle fixed timeout of 10ms to wait for change detection to run in Zoneless mode. This failed in CI sometimes presumably because CI can execute slower based on resource constraints. This commit replaces it with a polling approach which checks until the expected content is rendered.
1 parent b4a3abd commit 4a174b8

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

packages/platform-server/test/incremental_hydration_spec.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
} from '@angular/core';
3030

3131
import {
32+
DOCUMENT,
3233
isPlatformServer,
3334
Location,
3435
ɵPLATFORM_BROWSER_ID as PLATFORM_BROWSER_ID,
@@ -2131,6 +2132,12 @@ describe('platform-server partial hydration integration', () => {
21312132
});
21322133

21332134
describe('control flow', () => {
2135+
let pollingInterval: ReturnType<typeof setInterval>;
2136+
2137+
afterEach(() => {
2138+
if (pollingInterval !== undefined) clearInterval(pollingInterval);
2139+
});
2140+
21342141
it('should support hydration for all items in a for loop', async () => {
21352142
@Component({
21362143
selector: 'app',
@@ -2164,13 +2171,31 @@ describe('platform-server partial hydration integration', () => {
21642171
this.value.set('end');
21652172
}
21662173
registry = inject(DEHYDRATED_BLOCK_REGISTRY);
2174+
private readonly doc = inject(DOCUMENT);
21672175

21682176
constructor() {
21692177
// TODO: Understand why this is needed to get the full rendering of the HTML
21702178
// Without it, bindings aren't properly rendered in SSR and the test fails.
21712179
// There was no issue with the zone based scheduler.
2172-
const remove = inject(PendingTasks).add();
2173-
setTimeout(() => remove(), 10);
2180+
const pendingTasks = inject(PendingTasks);
2181+
pendingTasks.run(
2182+
() =>
2183+
new Promise<void>((resolve) => {
2184+
pollingInterval = setInterval(() => {
2185+
const el = this.doc.getElementById('item-1');
2186+
if (el && el.textContent?.includes('defer block 1 rendered')) {
2187+
clearInterval(pollingInterval);
2188+
resolve();
2189+
}
2190+
}, 10);
2191+
2192+
// Fallback timeout to prevent indefinite hanging
2193+
setTimeout(() => {
2194+
clearInterval(pollingInterval);
2195+
resolve();
2196+
}, 1000);
2197+
}),
2198+
);
21742199
}
21752200
}
21762201

0 commit comments

Comments
 (0)