Skip to content

Fix WebSocket polyfill spec compliance and test flake#150

Open
bghgary wants to merge 3 commits intoBabylonJS:mainfrom
bghgary:fix/websocket-test-flake
Open

Fix WebSocket polyfill spec compliance and test flake#150
bghgary wants to merge 3 commits intoBabylonJS:mainfrom
bghgary:fix/websocket-test-flake

Conversation

@bghgary
Copy link
Copy Markdown
Contributor

@bghgary bghgary commented Apr 8, 2026

Fix WebSocket polyfill and test flake.

Polyfill Fix

Per the WebSocket spec, onclose must always fire after onerror. The polyfill's ErrorCallback only fired onerror without triggering a close event. This caused tests to call done() from both onerror and onclose (when the native layer fired close separately), resulting in mocha's "done() called multiple times" error.

Changes:

  • ErrorCallback now calls CloseCallback after firing onerror, matching the spec
  • CloseCallback is now idempotent (skips if already closed) so it's safe if the native layer also calls it separately

Test Fix

With onclose guaranteed as the terminal event, tests now collect errors from earlier phases and report them in onclosedone() is called exactly once. No guard flags needed.

Note

The tests still depend on an external WebSocket echo service (ws.postman-echo.com). If the service is down, the tests will fail with "WebSocket failed" rather than crash with double-done.

When the WebSocket connection fails, onerror fires followed by onclose.
Both called done(), causing mocha's 'done() called multiple times' error.

Guard done() with a finished flag so only the first callback completes
the test. This fixes the intermittent CI flake on Win32_x64_JSI.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 8, 2026 18:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an intermittent Mocha CI flake in the Win32_x64_JSI WebSocket unit tests by preventing done() from being invoked multiple times when onerror is followed by onclose.

Changes:

  • Added a finished flag and finish() wrapper to ensure only the first callback completes the test.
  • Updated both the single-connection and multiple-connection WebSocket tests to use finish() instead of calling done() directly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bghgary bghgary changed the title Fix WebSocket test double-done flake Fix WebSocket polyfill spec compliance and test flake Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants