Skip to content

Commit f00a377

Browse files
committed
fix(test-utils): Prune old events from buffer when listener registers
Instead of just filtering old events, actually remove them from the buffer. This prevents stale events from ever being matched by future listeners, even if timestamps are somehow compared incorrectly. Events older than the listener registration time are definitely stale and will never be needed by any listener.
1 parent 87f6dc6 commit f00a377

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

dev-packages/e2e-tests/test-applications/nextjs-15-spotlight/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"@types/node": "^18.19.1",
1515
"@types/react": "18.0.26",
1616
"@types/react-dom": "18.0.9",
17-
"next": "15.5.7",
17+
"next": "15.5.9",
1818
"react": "latest",
1919
"react-dom": "latest",
2020
"typescript": "~5.0.0"

dev-packages/test-utils/src/event-proxy-server.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,16 @@ export async function startProxyServer(
150150

151151
eventCallbackListeners.add(callbackListener);
152152

153-
// Use strict inequality to prevent stale events from previous tests leaking through.
154-
// If a previous test's event was buffered at the same millisecond as this listener started,
155-
// we want to exclude it.
153+
// Prune old events from the buffer that are definitely stale (older than this listener).
154+
// This prevents memory buildup and ensures old events can never leak to future listeners.
155+
while (eventBuffer.length > 0 && eventBuffer[0].timestamp <= listenerTimestamp) {
156+
eventBuffer.shift();
157+
}
158+
159+
// Send any remaining buffered events (those that arrived after this listener was registered,
160+
// which can happen due to race conditions with HTTP request processing).
156161
eventBuffer.forEach(bufferedEvent => {
157-
if (bufferedEvent.timestamp > listenerTimestamp) {
158-
callbackListener.callback(bufferedEvent.data);
159-
}
162+
callbackListener.callback(bufferedEvent.data);
160163
});
161164

162165
eventCallbackRequest.on('close', () => {

0 commit comments

Comments
 (0)