Skip to content

Fix WebSocket test flake and update UrlLib/AndroidExtensions#150

Merged
bghgary merged 9 commits intoBabylonJS:mainfrom
bghgary:fix/websocket-test-flake
Apr 13, 2026
Merged

Fix WebSocket test flake and update UrlLib/AndroidExtensions#150
bghgary merged 9 commits intoBabylonJS:mainfrom
bghgary:fix/websocket-test-flake

Conversation

@bghgary
Copy link
Copy Markdown
Contributor

@bghgary bghgary commented Apr 8, 2026

Fix WebSocket test flake and update dependencies.

The Bug

Tests called done() from both onerror and onclose, causing mocha's "done() called multiple times" error when the native layer fired both events.

Root Cause

The Windows and Apple WebSocket implementations only fired onError on connection failures, not onClose. Android's org.java_websocket library fires both naturally. This inconsistency meant tests couldn't rely on a single terminal event.

The Fix

UrlLib (bumped to 880c257): Added a single CloseOnce helper on Windows and invalidateAndCancelWithCloseCode on Apple that checks the close code internally — fires onError for abnormal codes, then onClose exactly once. All platforms now consistently fire onClose as the terminal event.

AndroidExtensions (bumped to 2e85a8d): Removed the redundant errorCallback from onClose that was re-interpreting close codes. The org.java_websocket library already fires onError for error conditions.

Tests: Refactored so done() is called exactly once from onclose (the terminal event). Errors from earlier phases (assertion failures, onerror) are collected in an error variable and reported via done(error). Catch blocks call ws.close() to ensure onclose fires promptly on assertion failure. All expect assertions are preserved for property and value checks (readyState, url, message content). The invalid domain test verifies onerror fires before onclose.

Dependencies

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
bghgary and others added 3 commits April 9, 2026 17:18
Bump UrlLib to 880c2575e57ca0b59068ecc4860f185b9970e0ce (onClose after
onError on all platforms) and AndroidExtensions to
2e85a8d43b89246c460112c9e5546ad54b6e87b4 (remove redundant errorCallback).

With onclose guaranteed as the terminal event, tests now collect errors
from earlier phases and report them in onclose — done() is called
exactly once. The invalid domain test now verifies onerror fires before
onclose.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bghgary bghgary changed the title Fix WebSocket polyfill spec compliance and test flake Fix WebSocket test flake and update UrlLib/AndroidExtensions Apr 13, 2026
bghgary and others added 3 commits April 13, 2026 13:33
Replace expect() assertions with plain if-checks so async callbacks
don't need try/catch. Each test calls done() exactly once from onclose
(the terminal event). Errors from onerror or message validation are
collected and passed through done(error).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep expect() for all property and value checks. Errors are collected
in an 'error' variable (typed unknown) and reported via done(error) in
onclose. Catch blocks call ws.close() to ensure onclose fires promptly
on assertion failure instead of waiting for timeout.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bghgary bghgary merged commit d0926d4 into BabylonJS:main Apr 13, 2026
19 checks passed
@bghgary bghgary deleted the fix/websocket-test-flake branch April 14, 2026 19:33
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.

4 participants