Skip to content

Commit d0926d4

Browse files
bghgaryCopilot
andauthored
Fix WebSocket test flake and update UrlLib/AndroidExtensions (#150)
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 - BabylonJS/UrlLib#26 - BabylonJS/AndroidExtensions#16 --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7371f11 commit d0926d4

2 files changed

Lines changed: 60 additions & 35 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ include(FetchContent)
1111
# --------------------------------------------------
1212
FetchContent_Declare(AndroidExtensions
1313
GIT_REPOSITORY https://github.com/BabylonJS/AndroidExtensions.git
14-
GIT_TAG 2d5af72259cc73e5f249d3c99bee2010be9cb042
14+
GIT_TAG 2e85a8d43b89246c460112c9e5546ad54b6e87b4
1515
EXCLUDE_FROM_ALL)
1616
FetchContent_Declare(arcana.cpp
1717
GIT_REPOSITORY https://github.com/microsoft/arcana.cpp.git
@@ -37,7 +37,7 @@ FetchContent_Declare(llhttp
3737
EXCLUDE_FROM_ALL)
3838
FetchContent_Declare(UrlLib
3939
GIT_REPOSITORY https://github.com/BabylonJS/UrlLib.git
40-
GIT_TAG d53beb958b1cccafd5411260ace6d32b68b56a83
40+
GIT_TAG 880c2575e57ca0b59068ecc4860f185b9970e0ce
4141
EXCLUDE_FROM_ALL)
4242
# --------------------------------------------------
4343

Tests/UnitTests/Scripts/tests.ts

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Mocha from "mocha";
1+
import * as Mocha from "mocha";
22
import { expect } from "chai";
33

44
Mocha.setup('bdd');
@@ -407,45 +407,51 @@ if (hostPlatform !== "Unix") {
407407
it("should connect correctly with one websocket connection", function (done) {
408408
const ws = new WebSocket("wss://ws.postman-echo.com/raw");
409409
const testMessage = "testMessage";
410+
let error: unknown;
411+
410412
ws.onopen = () => {
411413
try {
412414
expect(ws).to.have.property("readyState", 1);
413415
expect(ws).to.have.property("url", "wss://ws.postman-echo.com/raw");
414416
ws.send(testMessage);
415417
}
416418
catch (e) {
417-
done(e);
419+
error = e;
420+
ws.close();
418421
}
419422
};
420423

421424
ws.onmessage = (msg) => {
422425
try {
423426
expect(msg.data).to.equal(testMessage);
424-
ws.close();
425427
}
426428
catch (e) {
427-
done(e);
429+
error = e;
428430
}
431+
ws.close();
429432
};
430433

431434
ws.onclose = () => {
432-
try {
433-
expect(ws).to.have.property("readyState", 3);
434-
done();
435-
}
436-
catch (e) {
437-
done(e);
435+
if (!error) {
436+
try {
437+
expect(ws).to.have.property("readyState", 3);
438+
}
439+
catch (e) {
440+
error = e;
441+
}
438442
}
443+
done(error);
439444
};
440445

441-
ws.onerror = (ev) => {
442-
done(new Error("WebSocket failed"));
446+
ws.onerror = () => {
447+
error = new Error("WebSocket failed");
443448
};
444449
});
445450

446451
it("should connect correctly with multiple websocket connections", function (done) {
447452
const testMessage1 = "testMessage1";
448453
const testMessage2 = "testMessage2";
454+
let error: unknown;
449455

450456
const ws1 = new WebSocket("wss://ws.postman-echo.com/raw");
451457
ws1.onopen = () => {
@@ -457,57 +463,66 @@ if (hostPlatform !== "Unix") {
457463
ws2.send(testMessage2);
458464
}
459465
catch (e) {
460-
done(e);
466+
error = e;
467+
ws2.close();
461468
}
462469
};
463470

464471
ws2.onmessage = (msg) => {
465472
try {
466473
expect(msg.data).to.equal(testMessage2);
467-
ws2.close();
468474
}
469475
catch (e) {
470-
done(e);
476+
error = e;
471477
}
478+
ws2.close();
472479
};
473480

474481
ws2.onclose = () => {
475-
try {
476-
expect(ws2).to.have.property("readyState", 3);
477-
ws1.send(testMessage1);
482+
if (!error) {
483+
try {
484+
expect(ws2).to.have.property("readyState", 3);
485+
ws1.send(testMessage1);
486+
}
487+
catch (e) {
488+
error = e;
489+
ws1.close();
490+
}
478491
}
479-
catch (e) {
480-
done(e);
492+
else {
493+
ws1.close();
481494
}
482495
};
483496

484-
ws2.onerror = (ev) => {
485-
done(new Error("Websocket failed"));
497+
ws2.onerror = () => {
498+
error = new Error("WebSocket failed");
486499
};
487500
}
488501

489502
ws1.onmessage = (msg) => {
490503
try {
491504
expect(msg.data).to.equal(testMessage1);
492-
ws1.close();
493505
}
494506
catch (e) {
495-
done(e);
507+
error = e;
496508
}
509+
ws1.close();
497510
}
498511

499512
ws1.onclose = () => {
500-
try {
501-
expect(ws1).to.have.property("readyState", 3);
502-
done();
503-
}
504-
catch (e) {
505-
done(e);
513+
if (!error) {
514+
try {
515+
expect(ws1).to.have.property("readyState", 3);
516+
}
517+
catch (e) {
518+
error = e;
519+
}
506520
}
521+
done(error);
507522
}
508523

509-
ws1.onerror = (ev) => {
510-
done(new Error("Websocket failed"));
524+
ws1.onerror = () => {
525+
error = new Error("WebSocket failed");
511526
};
512527
});
513528

@@ -522,8 +537,18 @@ if (hostPlatform !== "Unix") {
522537
it("should trigger error callback with invalid domain", function (done) {
523538
this.timeout(10000);
524539
const ws = new WebSocket("wss://example");
540+
let errorFired = false;
525541
ws.onerror = () => {
526-
done();
542+
errorFired = true;
543+
};
544+
ws.onclose = () => {
545+
try {
546+
expect(errorFired).to.be.true;
547+
done();
548+
}
549+
catch (e) {
550+
done(e);
551+
}
527552
};
528553
});
529554
})

0 commit comments

Comments
 (0)