test: integration tests for sourcedFrom and allowStaleWhileRevalidate#1208
test: integration tests for sourcedFrom and allowStaleWhileRevalidate#1208kriszyp wants to merge 4 commits into
Conversation
…date Closes #1189 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request introduces a new integration test suite (caching.test.ts) to verify caching behaviors such as sourcedFrom() and allowStaleWhileRevalidate() end-to-end using a mock HTTP origin server. The feedback focuses on robust resource management within the tests: specifically, calling server.closeAllConnections() during teardown to prevent hanging connections, and ensuring all fetch response bodies are consumed to avoid potential socket leaks in Node.js.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
|
||
| return { | ||
| url, | ||
| close: () => new Promise<void>((resolve, reject) => server.close((err) => (err ? reject(err) : resolve()))), |
There was a problem hiding this comment.
To prevent hanging connections from keeping the test runner alive or causing timeouts during teardown, call server.closeAllConnections() immediately after calling server.close(). This ensures all active keep-alive connections (such as those from Harper's internal fetch client) are forcefully closed.
| close: () => new Promise<void>((resolve, reject) => server.close((err) => (err ? reject(err) : resolve()))), | |
| close: () => | |
| new Promise<void>((resolve, reject) => { | |
| server.close((err) => (err ? reject(err) : resolve())); | |
| server.closeAllConnections(); | |
| }), |
| const r = await fetch(`${baseUrl}/CachedProduct/${id}`, { headers: { Authorization: authHeader } }); | ||
| strictEqual(r.status, 404, 'Should return 404 when origin returns 404'); | ||
| strictEqual(origin.fetchCount(id), 1, 'Origin should have been called once'); |
There was a problem hiding this comment.
The response body of the fetch call is not consumed. In Node.js (via undici), leaving response bodies unconsumed can lead to socket leaks and hanging connections. It is recommended to consume the body (e.g., by calling await r.arrayBuffer() or await r.text()) even for error responses like 404.
const r = await fetch(`${baseUrl}/CachedProduct/${id}`, { headers: { Authorization: authHeader } });
strictEqual(r.status, 404, 'Should return 404 when origin returns 404');
await r.arrayBuffer();
strictEqual(origin.fetchCount(id), 1, 'Origin should have been called once');| const r1 = await fetch(`${baseUrl}/CachedProduct/${id}`, { headers: { Authorization: authHeader } }); | ||
| strictEqual(r1.status, 200); | ||
| strictEqual(origin.fetchCount(id), 1, 'Should fetch from origin on first GET'); |
There was a problem hiding this comment.
The response body of r1 is not consumed. To prevent socket leaks and ensure the connection is released back to the pool, consume the response body.
| const r1 = await fetch(`${baseUrl}/CachedProduct/${id}`, { headers: { Authorization: authHeader } }); | |
| strictEqual(r1.status, 200); | |
| strictEqual(origin.fetchCount(id), 1, 'Should fetch from origin on first GET'); | |
| const r1 = await fetch(`${baseUrl}/CachedProduct/${id}`, { headers: { Authorization: authHeader } }); | |
| strictEqual(r1.status, 200); | |
| await r1.arrayBuffer(); | |
| strictEqual(origin.fetchCount(id), 1, 'Should fetch from origin on first GET'); |
| const rDel = await fetch(`${baseUrl}/CachedProduct/${id}`, { | ||
| method: 'DELETE', | ||
| headers: { Authorization: authHeader }, | ||
| }); | ||
| ok(rDel.status === 200 || rDel.status === 204, `DELETE should succeed, got ${rDel.status}`); |
There was a problem hiding this comment.
The response body of the DELETE request is not consumed. Consume the response body to prevent potential socket leaks.
| const rDel = await fetch(`${baseUrl}/CachedProduct/${id}`, { | |
| method: 'DELETE', | |
| headers: { Authorization: authHeader }, | |
| }); | |
| ok(rDel.status === 200 || rDel.status === 204, `DELETE should succeed, got ${rDel.status}`); | |
| const rDel = await fetch(`${baseUrl}/CachedProduct/${id}`, { | |
| method: 'DELETE', | |
| headers: { Authorization: authHeader }, | |
| }); | |
| ok(rDel.status === 200 || rDel.status === 204, `DELETE should succeed, got ${rDel.status}`); | |
| await rDel.arrayBuffer(); |
|
Reviewed; no blockers found. |
…on teardown, consume fetch bodies - Call server.closeAllConnections() before server.close() in MockOrigin.close() to drain keep-alive sockets immediately on teardown (Node ≥18.2, safe for engine ≥20). - Consume response bodies with .text() on the three fetch calls that only checked status (404 propagation test, DELETE test r1 and rDel) to avoid socket leaks. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…o unit suite (#1208) The four previous tests (cache miss/hit, 404 passthrough, DELETE invalidation, SWR) duplicate coverage already in unitTests/resources/caching.test.js. Replace them with an explanatory comment block and a TODO pointing to issue #1189 for the one genuinely new scenario (replicationSource: true) that requires a 2-node cluster harness not yet available in integrationTests/. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Closes #1189
Summary
After review feedback from Nathan Heskew: the four tests originally in this PR (cache miss/hit, 404 passthrough, DELETE invalidation, SWR) duplicate coverage already comprehensively provided by
unitTests/resources/caching.test.js. This PR has been scoped down accordingly.The file
integrationTests/server/caching.test.tsnow contains only an explanatory comment block that:replicationSource: true— with a TODO pointing to Add integration tests for sourcedFrom, stale-while-revalidate, and replicationSource cache patterns #1189. That scenario requires a 2-node cluster harness not yet available inintegrationTests/.What changed
What's not here (and why)
replicationSource: truecausessourcedFromto fetch on a replica node rather than the origin. Testing it end-to-end requires a live 2-node cluster setup. The tracking issue is #1189; a follow-up PR againstintegrationTests/can add that once a cluster harness exists.Generated by Claude Sonnet 4.6 (claude-sonnet-4-6[1m]).