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)

test/custom_messages_proxy_shell.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
function sendResult(success) {
1717
if (window.sentTestResult) return;
1818
window.sentTestResult = true;
19-
var xhr = new XMLHttpRequest();
20-
xhr.open('GET', 'http://localhost:8888/report_result?' + (success ? 1 : 0));
21-
xhr.send();
22-
setTimeout(function() { window.close() }, 1000);
19+
fetch('http://localhost:8888/report_result?' + (success ? 1 : 0))
20+
.then(() => window.close());
2321
}
2422

2523
var Module = {

test/emscripten_throw_number_pre.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
addEventListener('error', function(event) {
1+
addEventListener('error', (event) => {
22
window.disableErrorReporting = true;
33
var result = event.error === 42 ? 0 : 1;
4-
var xhr = new XMLHttpRequest();
5-
xhr.open('GET', 'http://localhost:8888/report_result?' + result, true);
6-
xhr.send();
4+
fetch('http://localhost:8888/report_result?' + result);
75
});

test/emscripten_throw_string_pre.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
addEventListener('error', function(event) {
1+
addEventListener('error', (event) => {
22
window.disableErrorReporting = true;
33
var result = event.error === 'Hello!' ? 0 : 1;
4-
var xhr = new XMLHttpRequest();
5-
xhr.open('GET', 'http://localhost:8888/report_result?' + result, true);
6-
xhr.send();
4+
fetch('http://localhost:8888/report_result?' + result);
75
});

test/hello_world_gles_shell.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@
3232
});
3333
document.body.dispatchEvent(event);
3434
}
35-
function reportResult(result) {
36-
var xhr = new XMLHttpRequest();
37-
xhr.open('GET', 'http://localhost:8888/report_gl_result?' + result, true);
38-
xhr.send();
39-
setTimeout(function() { window.close() }, 1000);
35+
async function reportResult(result) {
36+
await fetch('http://localhost:8888/report_gl_result?' + result);
37+
window.close();
4038
}
4139
function doTest() {
4240
var firstImage = Module.canvas.toDataURL();

test/test_browser.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,16 @@ def decorated(self, *args, **kwargs):
176176
def also_with_threads(f):
177177
assert callable(f)
178178

179-
def metafunc(self, threads, *args, **kwargs):
179+
@wraps(f)
180+
def decorated(self, threads, *args, **kwargs):
180181
if threads:
181182
self.emcc_args += ['-pthread']
182183
f(self, *args, **kwargs)
183184

184-
metafunc._parameterize = {'': (False,),
185-
'threads': (True,)}
185+
parameterize(decorated, {'': (False,),
186+
'pthreads': (True,)})
186187

187-
return metafunc
188+
return decorated
188189

189190

190191
def skipExecIf(cond, message):
@@ -700,16 +701,14 @@ def setup(assetLocalization):
700701
<center><canvas id='canvas' width='256' height='256'></canvas></center>
701702
<hr><div id='output'></div><hr>
702703
<script type='text/javascript'>
703-
const handler = (event) => {
704+
const handler = async (event) => {
704705
event.stopImmediatePropagation();
705706
const error = String(event instanceof ErrorEvent ? event.message : (event.reason || event));
706707
window.disableErrorReporting = true;
707708
window.onerror = null;
708709
var result = error.includes("test.data") ? 1 : 0;
709-
var xhr = new XMLHttpRequest();
710-
xhr.open('GET', 'http://localhost:8888/report_result?' + result, true);
711-
xhr.send();
712-
setTimeout(function() { window.close() }, 1000);
710+
await fetch('http://localhost:8888/report_result?' + result);
711+
window.close();
713712
}
714713
window.addEventListener('error', handler);
715714
window.addEventListener('unhandledrejection', handler);
@@ -1638,11 +1637,9 @@ def test_worker(self):
16381637
Worker Test
16391638
<script>
16401639
var worker = new Worker('worker.js');
1641-
worker.onmessage = (event) => {
1642-
var xhr = new XMLHttpRequest();
1643-
xhr.open('GET', 'http://localhost:%s/report_result?' + event.data);
1644-
xhr.send();
1645-
setTimeout(function() { window.close() }, 1000);
1640+
worker.onmessage = async (event) => {
1641+
await fetch('http://localhost:%s/report_result?' + event.data);
1642+
window.close();
16461643
};
16471644
</script>
16481645
</body>
@@ -1689,12 +1686,10 @@ def test_chunked_synchronous_xhr(self):
16891686
<script>
16901687
var worker = new Worker("%s");
16911688
var buffer = [];
1692-
worker.onmessage = (event) => {
1689+
worker.onmessage = async (event) => {
16931690
if (event.data.channel === "stdout") {
1694-
var xhr = new XMLHttpRequest();
1695-
xhr.open('GET', 'http://localhost:%s/report_result?' + event.data.line);
1696-
xhr.send();
1697-
setTimeout(function() { window.close() }, 1000);
1691+
await fetch('http://localhost:%s/report_result?' + event.data.line);
1692+
window.close();
16981693
} else {
16991694
if (event.data.trace) event.data.trace.split("\n").map(function(v) { console.error(v); });
17001695
if (event.data.line) {
@@ -2450,13 +2445,11 @@ def test_runtime_misuse(self):
24502445
post_hook = r'''
24512446
function myJSCallback() {
24522447
// Run on the next event loop, as code may run in a postRun right after main().
2453-
setTimeout(function() {
2454-
var xhr = new XMLHttpRequest();
2448+
setTimeout(async () => {
24552449
out('done timeout noted = ' + Module.noted);
24562450
assert(Module.noted);
2457-
xhr.open('GET', 'http://localhost:%s/report_result?' + HEAP32[Module.noted/4]);
2458-
xhr.send();
2459-
setTimeout(function() { window.close() }, 1000);
2451+
await fetch('http://localhost:%s/report_result?' + HEAP32[Module.noted/4]);
2452+
window.close();
24602453
}, 0);
24612454
// called from main, this is an ok time
24622455
doCcall(100);
@@ -4338,7 +4331,7 @@ def test_small_js_flags(self):
43384331
print('size:', size)
43394332
# Note that this size includes test harness additions (for reporting the result, etc.).
43404333
if not self.is_wasm64() and not self.is_2gb():
4341-
self.assertLess(abs(size - 4675), 100)
4334+
self.assertLess(abs(size - 4510), 100)
43424335

43434336
# Tests that it is possible to initialize and render WebGL content in a
43444337
# pthread by using OffscreenCanvas.
@@ -4962,11 +4955,10 @@ def test_browser_modularize_no_current_script(self):
49624955
shutil.move('test.wasm', Path(filesystem_path, 'test.wasm'))
49634956
create_file(Path(filesystem_path, 'test.html'), '''
49644957
<script>
4965-
setTimeout(function() {
4966-
var xhr = new XMLHttpRequest();
4967-
xhr.open('GET', 'test.js', false);
4968-
xhr.send(null);
4969-
eval(xhr.responseText);
4958+
setTimeout(async () => {
4959+
let response = await fetch('test.js');
4960+
let text = await response.text();
4961+
eval(text);
49704962
%s
49714963
}, 1);
49724964
</script>
@@ -5573,7 +5565,7 @@ def test(args, expect_fail):
55735565
self.compile_btest('main.c', ['-fPIC', 'library.so', '-sMAIN_MODULE=2', '-sEXIT_RUNTIME', '-o', 'a.out.html'] + args)
55745566
if expect_fail:
55755567
js = read_file('a.out.js')
5576-
create_file('a.out.js', 'fetch = undefined;\n' + js)
5568+
create_file('a.out.js', 'let origFetch = fetch; fetch = undefined;\n' + js)
55775569
return self.run_browser('a.out.html', '/report_result?abort:TypeError')
55785570
else:
55795571
return self.run_browser('a.out.html', '/report_result?exit:42')

test/test_fflush.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@
8181
postRun: [],
8282

8383
reportSuccess: function() {
84-
var xhr = new XMLHttpRequest();
85-
xhr.open('GET', 'http://localhost:8888/report_result?0');
86-
xhr.send();
87-
setTimeout(function() { window.close() }, 1000);
84+
fetch('http://localhost:8888/report_result?0').then(() => window.close());
8885
},
8986

9087
print: (function() {

0 commit comments

Comments
 (0)