Skip to content

Commit 28c10a1

Browse files
authored
[test] Use fetch over XMLHttpRequest for browser test reporting. NFC (#22018)
Inspired by #22015
1 parent afa0b8b commit 28c10a1

16 files changed

+59
-103
lines changed

test/browser/async_bad_list.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@
99
int main() {
1010
int x = EM_ASM_INT({
1111
window.disableErrorReporting = true;
12-
window.onerror = function(e) {
12+
window.onerror = async (e) => {
1313
var message = e.toString();
1414
var success = message.indexOf("unreachable") >= 0 || // firefox
1515
message.indexOf("Script error.") >= 0; // chrome
1616
if (success && !Module.reported) {
1717
Module.reported = true;
1818
console.log("reporting success");
1919
// manually REPORT_RESULT; we shouldn't call back into native code at this point
20-
var xhr = new XMLHttpRequest();
21-
xhr.open("GET", "http://localhost:8888/report_result?0");
22-
xhr.onload = xhr.onerror = function() {
23-
window.close();
24-
};
25-
xhr.send();
20+
await fetch("http://localhost:8888/report_result?0");
21+
window.close();
2622
}
2723
};
2824
return 0;

test/browser/async_returnvalue.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,14 @@ int main() {
3333
#ifdef BAD
3434
EM_ASM({
3535
window.disableErrorReporting = true;
36-
window.onerror = function(e) {
36+
window.onerror = async (e) => {
3737
var success = e.toString().indexOf("import sync_tunnel was not in ASYNCIFY_IMPORTS, but changed the state") > 0;
3838
if (success && !Module.reported) {
3939
Module.reported = true;
4040
console.log("reporting success");
4141
// manually REPORT_RESULT; we shouldn't call back into native code at this point
42-
var xhr = new XMLHttpRequest();
43-
xhr.open("GET", "http://localhost:8888/report_result?0");
44-
xhr.onload = xhr.onerror = function() {
45-
window.close();
46-
};
47-
xhr.send();
42+
await fetch("http://localhost:8888/report_result?0");
43+
window.close();
4844
}
4945
};
5046
});

test/browser/cwrap_early.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ Module['preRun'] = () => {
55
// doesn't actually call it
66
var wrappedAdd = Module['cwrap']('add', 'number', ['number', 'number']);
77
// but to call the compiled code, we must wait for the runtime
8-
Module['onRuntimeInitialized'] = function() {
8+
Module['onRuntimeInitialized'] = async () => {
99
console.log('onRuntimeInitialized');
1010
if (wrappedAdd(5, 6) != 11) throw '5 + 6 should be 11';
1111
// report success
12-
var xhr = new XMLHttpRequest();
13-
xhr.open('GET', 'http://localhost:8888/report_result?0', true);
14-
xhr.send();
15-
setTimeout(function() { window.close() }, 1000);
12+
await fetch('http://localhost:8888/report_result?0');
13+
window.close();
1614
};
1715
};

test/browser/test_offset_converter.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ void *get_pc(void) { return __builtin_return_address(0); }
66
int magic_test_function(void) {
77
int result = EM_ASM_INT({
88
function report(x) {
9-
var xhr = new XMLHttpRequest();
10-
xhr.open('GET', encodeURI('http://localhost:8888?stdout=' + x));
11-
xhr.send();
9+
fetch(encodeURI('http://localhost:8888?stdout=' + x));
1210
}
1311
report('magic_test_function: input=' + $0);
1412
var converted = wasmOffsetConverter.getName($0);

test/browser/webgl_draw_base_vertex_base_instance_test.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ int main() {
7070

7171
if (!extAvailable) {
7272
EM_ASM({
73-
xhr = new XMLHttpRequest();
74-
xhr.open('GET', 'http://localhost:8888/report_result?skipped:%20WEBGL_draw_instanced_base_vertex_base_instance%20is%20not%20supported!');
75-
xhr.send();
76-
setTimeout(function() { window.close() }, 2000);
73+
fetch('http://localhost:8888/report_result?skipped:%20WEBGL_draw_instanced_base_vertex_base_instance%20is%20not%20supported!').then(() => window.close());
7774
});
7875
return 0;
7976
}

test/browser/webgl_multi_draw_test.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ int main()
6060

6161
if (!extAvailable) {
6262
EM_ASM({
63-
xhr = new XMLHttpRequest();
64-
xhr.open('GET', 'http://localhost:8888/report_result?skipped:%20WEBGL_multi_draw%20is%20not%20supported!');
65-
xhr.send();
66-
setTimeout(function() { window.close() }, 2000);
63+
fetch("http://localhost:8888/report_result?skipped:%20WEBGL_multi_draw%20is%20not%20supported!").then(() => windows.close());
6764
});
6865
return 0;
6966
}

test/browser_reporting.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ function reportResultToServer(result, port) {
1313
if ((typeof ENVIRONMENT_IS_NODE !== 'undefined' && ENVIRONMENT_IS_NODE) || (typeof ENVIRONMENT_IS_AUDIO_WORKLET !== 'undefined' && ENVIRONMENT_IS_AUDIO_WORKLET)) {
1414
out('RESULT: ' + result);
1515
} else {
16-
var xhr = new XMLHttpRequest();
17-
xhr.open('GET', 'http://localhost:' + port + '/report_result?' + result);
18-
xhr.send();
19-
if (typeof window === 'object' && window && hasModule && !Module['pageThrewException']) {
20-
/* for easy debugging, don't close window on failure */
21-
setTimeout(function() { window.close() }, 1000);
22-
}
16+
let doFetch = typeof origFetch != 'undefined' ? origFetch : fetch;
17+
doFetch('http://localhost:' + port + '/report_result?' + result).then(() => {
18+
if (typeof window === 'object' && window && hasModule && !Module['pageThrewException']) {
19+
/* for easy debugging, don't close window on failure */
20+
window.close();
21+
}
22+
});
2323
}
2424
}
2525

@@ -36,8 +36,7 @@ function reportErrorToServer(message) {
3636
if (typeof ENVIRONMENT_IS_NODE !== 'undefined' && ENVIRONMENT_IS_NODE) {
3737
err(message);
3838
} else {
39-
xhr.open('GET', encodeURI('http://localhost:8888?stderr=' + message));
40-
xhr.send();
39+
fetch(encodeURI('http://localhost:8888?stderr=' + message));
4140
}
4241
}
4342

test/canvas_animate_resize.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,8 @@ int main()
126126
if (!ctx) {
127127
if (!emscripten_supports_offscreencanvas()) {
128128
EM_ASM({
129-
xhr = new XMLHttpRequest();
130-
xhr.open('GET', 'http://localhost:8888/report_result?skipped:%20OffscreenCanvas%20is%20not%20supported!');
131-
xhr.send();
132-
setTimeout(function() { window.close() }, 2000);
129+
fetch("http://localhost:8888/report_result?skipped:%20OffscreenCanvas%20is%20not%20supported!")
130+
.then(() => window.close());
133131
});
134132
}
135133
return 0;

test/canvas_style_proxy_shell.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,10 @@
151151
canvas.style.cursor = 'none';
152152
}
153153

154-
window.verifyCanvasStyle = function () {
154+
window.verifyCanvasStyle = async () => {
155155
var success = Module.canvas.style.cursor === 'pointer';
156-
var xhr = new XMLHttpRequest();
157-
xhr.open('GET', 'http://localhost:8888/report_result?' + (success ? 1 : 0));
158-
xhr.send();
159-
setTimeout(function() { window.close() }, 1000);
156+
await fetch('http://localhost:8888/report_result?' + (success ? 1 : 0));
157+
window.close();
160158
}
161159

162160
</script>

test/common.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,9 +1954,7 @@ def do_GET(self):
19541954
To get logging to the console from browser tests, add this to
19551955
print/printErr/the exception handler in src/shell.html:
19561956
1957-
var xhr = new XMLHttpRequest();
1958-
xhr.open('GET', encodeURI('http://localhost:8888?stdout=' + text));
1959-
xhr.send();
1957+
fetch(encodeURI('http://localhost:8888?stdout=' + text));
19601958
'''
19611959
print('[client logging:', unquote_plus(self.path), ']')
19621960
self.send_response(200)

0 commit comments

Comments
 (0)