Skip to content

Commit 7646bf0

Browse files
committed
Cleanup the emscripten tests relating to the event loop. NFC
The browser tests were only being run in `PROXY_TO_PTHREAD` mode for some reason even though these tests should pass on the main thread. In test_other the `PROXY_TO_PTHREAD` flags were being passed as args (rather then emcc_args). Likely a copy/paste bug.
1 parent d71d76e commit 7646bf0

6 files changed

+70
-69
lines changed

test/emscripten_performance_now.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ void test(void *userData) {
1212
double now3 = emscripten_date_now();
1313
assert(now3 >= dateNow + 100);
1414

15-
#ifdef REPORT_RESULT
16-
REPORT_RESULT(0);
17-
#endif
15+
exit(0);
1816
}
1917

2018
int main() {
@@ -30,4 +28,5 @@ int main() {
3028
#else
3129
emscripten_set_timeout(test, 200, 0);
3230
#endif
31+
return 99;
3332
}

test/emscripten_set_interval.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
int funcExecuted = 0;
88

99
void testDone(void *userData) {
10+
printf("testDone\n");
1011
assert((long)userData == 2);
1112
assert(funcExecuted == 10);
1213
exit(0);
@@ -15,6 +16,7 @@ void testDone(void *userData) {
1516
long intervalId = 0;
1617

1718
void tick(void *userData) {
19+
printf("tick: %d\n", funcExecuted);
1820
assert((long)userData == 1);
1921
++funcExecuted;
2022
if (funcExecuted == 10) {
@@ -28,6 +30,5 @@ void tick(void *userData) {
2830

2931
int main() {
3032
intervalId = emscripten_set_interval(tick, 100, (void*)1);
31-
emscripten_exit_with_live_runtime();
3233
return 99;
3334
}

test/emscripten_set_timeout.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
#include <emscripten/emscripten.h>
22
#include <emscripten/html5.h>
3-
#include <emscripten/em_asm.h>
43
#include <assert.h>
54
#include <stdlib.h>
5+
#include <stdio.h>
66

77
int func1Executed = 0;
88
int func2Executed = 0;
99

1010
void func1(void *userData);
1111

1212
void func2(void *userData) {
13-
assert((long)userData == 2);
1413
++func2Executed;
14+
printf("func2: %d\n", func2Executed);
15+
assert((long)userData == 2);
1516

16-
if (func2Executed == 1)
17-
{
17+
if (func2Executed == 1) {
1818
// Test canceling a setTimeout: register a callback but then cancel it immediately
1919
long id = emscripten_set_timeout(func1, 10, (void*)2);
2020
emscripten_clear_timeout(id);
2121

22+
// Without this, the test will not exit correctly
23+
// https://github.com/emscripten-core/emscripten/issues/23763
24+
emscripten_runtime_keepalive_pop();
25+
2226
emscripten_set_timeout(func2, 100, (void*)2);
2327
}
24-
if (func2Executed == 2)
25-
{
28+
29+
if (func2Executed == 2) {
2630
assert(func1Executed == 1);
31+
printf("done\n");
2732
exit(0);
2833
}
2934
}
3035

3136
void func1(void *userData) {
37+
printf("func1\n");
3238
assert((long)userData == 1);
3339
++func1Executed;
3440

@@ -39,6 +45,5 @@ void func1(void *userData) {
3945

4046
int main() {
4147
emscripten_set_timeout(func1, 100, (void*)1);
42-
emscripten_exit_with_live_runtime();
4348
return 99;
4449
}

test/emscripten_set_timeout_loop.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
#include <emscripten/emscripten.h>
22
#include <emscripten/html5.h>
3-
#include <emscripten/em_asm.h>
43
#include <assert.h>
54
#include <stdlib.h>
5+
#include <stdio.h>
66

77
double previousSetTimeouTime = 0;
88
int funcExecuted = 0;
99

1010
void testDone(void *userData) {
11+
printf("testDone\n");
1112
assert((long)userData == 2);
1213
assert(funcExecuted == 10);
14+
emscripten_runtime_keepalive_pop();
1315
exit(0);
1416
}
1517

1618
bool tick(double time, void *userData) {
19+
printf("tick: %d\n", funcExecuted);
1720
assert(time >= previousSetTimeouTime);
1821
previousSetTimeouTime = time;
1922
assert((long)userData == 1);
2023
++funcExecuted;
21-
if (funcExecuted == 10)
22-
{
24+
if (funcExecuted == 10) {
2325
emscripten_set_timeout(testDone, 300, (void*)2);
2426
}
2527
return funcExecuted < 10;
2628
}
2729

2830
int main() {
2931
emscripten_set_timeout_loop(tick, 100, (void*)1);
30-
emscripten_exit_with_live_runtime();
32+
emscripten_runtime_keepalive_push();
3133
return 99;
3234
}

test/test_browser.py

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,22 @@ def decorated(self, threads, *args, **kwargs):
199199
f(self, *args, **kwargs)
200200

201201
parameterize(decorated, {'': (False,),
202-
'pthreads': (True,)})
202+
'pthread': (True,)})
203+
204+
return decorated
205+
206+
207+
def also_with_proxy_to_pthread(f):
208+
assert callable(f)
209+
210+
@wraps(f)
211+
def decorated(self, threads, *args, **kwargs):
212+
if threads:
213+
self.emcc_args += ['-pthread', '-sPROXY_TO_PTHREAD']
214+
f(self, *args, **kwargs)
215+
216+
parameterize(decorated, {'': (False,),
217+
'pthread': (True,)})
203218

204219
return decorated
205220

@@ -495,14 +510,11 @@ def make_main_two_files(path1, path2, nonexistingpath):
495510
self.btest_exit('main.c', emcc_args=['--pre-js', 'pre.js', '--use-preload-plugins'])
496511

497512
# Tests that user .html shell files can manually download .data files created with --preload-file cmdline.
498-
@parameterized({
499-
'': ([],),
500-
'pthreads': (['-pthread', '-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'],),
501-
})
502-
def test_preload_file_with_manual_data_download(self, args):
513+
@also_with_proxy_to_pthread
514+
def test_preload_file_with_manual_data_download(self):
503515
create_file('file.txt', 'Hello!')
504516

505-
self.compile_btest('browser/test_manual_download_data.c', ['-sEXIT_RUNTIME', '-o', 'out.js', '--preload-file', 'file.txt@/file.txt'] + args)
517+
self.compile_btest('browser/test_manual_download_data.c', ['-sEXIT_RUNTIME', '-o', 'out.js', '--preload-file', 'file.txt@/file.txt'])
506518
shutil.copy(test_file('browser/test_manual_download_data.html'), '.')
507519

508520
# Move .data file out of server root to ensure that getPreloadedPackage is actually used
@@ -1602,12 +1614,9 @@ def test_glfw_time(self):
16021614
def test_egl(self, args):
16031615
self.btest_exit('test_egl.c', emcc_args=['-O2', '-lEGL', '-lGL', '-sGL_ENABLE_GET_PROC_ADDRESS'] + args)
16041616

1605-
@parameterized({
1606-
'': ([],),
1607-
'proxy_to_pthread': (['-pthread', '-sPROXY_TO_PTHREAD'],),
1608-
})
1609-
def test_egl_width_height(self, args):
1610-
self.btest_exit('test_egl_width_height.c', emcc_args=['-O2', '-lEGL', '-lGL'] + args)
1617+
@also_with_proxy_to_pthread
1618+
def test_egl_width_height(self):
1619+
self.btest_exit('test_egl_width_height.c', emcc_args=['-O2', '-lEGL', '-lGL'])
16111620

16121621
@requires_graphics_hardware
16131622
def test_egl_createcontext_error(self):
@@ -1891,27 +1900,17 @@ def setup():
18911900
def test_emscripten_api_infloop(self):
18921901
self.btest_exit('emscripten_api_browser_infloop.cpp')
18931902

1894-
@parameterized({
1895-
'': ([],),
1896-
'pthreads': (['-pthread', '-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'],),
1897-
})
1898-
def test_emscripten_main_loop(self, args):
1899-
self.btest_exit('test_emscripten_main_loop.c', emcc_args=args)
1903+
@also_with_proxy_to_pthread
1904+
def test_emscripten_main_loop(self):
1905+
self.btest_exit('test_emscripten_main_loop.c')
19001906

1901-
@parameterized({
1902-
'': ([],),
1903-
# test pthreads + AUTO_JS_LIBRARIES mode as well
1904-
'pthreads': (['-pthread', '-sPROXY_TO_PTHREAD', '-sAUTO_JS_LIBRARIES=0'],),
1905-
})
1906-
def test_emscripten_main_loop_settimeout(self, args):
1907-
self.btest_exit('test_emscripten_main_loop_settimeout.c', emcc_args=args)
1907+
@also_with_proxy_to_pthread
1908+
def test_emscripten_main_loop_settimeout(self):
1909+
self.btest_exit('test_emscripten_main_loop_settimeout.c', args=['-sAUTO_JS_LIBRARIES=0'])
19081910

1909-
@parameterized({
1910-
'': ([],),
1911-
'pthreads': (['-pthread', '-sPROXY_TO_PTHREAD'],),
1912-
})
1913-
def test_emscripten_main_loop_and_blocker(self, args):
1914-
self.btest_exit('test_emscripten_main_loop_and_blocker.c', emcc_args=args)
1911+
@also_with_proxy_to_pthread
1912+
def test_emscripten_main_loop_and_blocker(self):
1913+
self.btest_exit('test_emscripten_main_loop_and_blocker.c')
19151914

19161915
def test_emscripten_main_loop_and_blocker_exit(self):
19171916
# Same as above but tests that EXIT_RUNTIME works with emscripten_main_loop. The
@@ -1967,13 +1966,10 @@ def test_sdl_glshader2(self):
19671966
def test_gl_glteximage(self):
19681967
self.btest('gl_teximage.c', '1', emcc_args=['-lGL', '-lSDL'])
19691968

1970-
@parameterized({
1971-
'': ([],),
1972-
'pthreads': (['-pthread', '-sPROXY_TO_PTHREAD', '-sOFFSCREEN_FRAMEBUFFER'],),
1973-
})
19741969
@requires_graphics_hardware
1970+
@also_with_proxy_to_pthread
19751971
def test_gl_textures(self, args):
1976-
self.btest_exit('gl_textures.c', emcc_args=['-lGL', '-g', '-sSTACK_SIZE=1MB'] + args)
1972+
self.btest_exit('gl_textures.c', args=['-lGL', '-g', '-sSTACK_SIZE=1MB'] + args)
19771973

19781974
@requires_graphics_hardware
19791975
def test_gl_ps(self):
@@ -2633,10 +2629,10 @@ def test_html5_core(self, opts):
26332629
self.emcc_args.append('--pre-js=pre.js')
26342630
self.btest_exit('test_html5_core.c', emcc_args=opts)
26352631

2632+
@also_with_proxy_to_pthread
26362633
@parameterized({
26372634
'': ([],),
26382635
'closure': (['-O2', '-g1', '--closure=1'],),
2639-
'pthread': (['-pthread', '-sPROXY_TO_PTHREAD'],),
26402636
})
26412637
def test_html5_gamepad(self, args):
26422638
self.btest_exit('test_gamepad.c', emcc_args=args)
@@ -4968,24 +4964,28 @@ def test_emscripten_request_animation_frame_loop(self):
49684964
def test_request_animation_frame(self):
49694965
self.btest_exit('test_request_animation_frame.c')
49704966

4967+
@also_with_proxy_to_pthread
49714968
def test_emscripten_set_timeout(self):
4972-
self.btest_exit('emscripten_set_timeout.c', emcc_args=['-pthread', '-sPROXY_TO_PTHREAD'])
4969+
self.btest_exit('emscripten_set_timeout.c')
49734970

4971+
@also_with_proxy_to_pthread
49744972
def test_emscripten_set_timeout_loop(self):
4975-
self.btest_exit('emscripten_set_timeout_loop.c', emcc_args=['-pthread', '-sPROXY_TO_PTHREAD'])
4973+
self.btest_exit('emscripten_set_timeout_loop.c')
49764974

49774975
def test_emscripten_set_immediate(self):
49784976
self.btest_exit('emscripten_set_immediate.c')
49794977

49804978
def test_emscripten_set_immediate_loop(self):
49814979
self.btest_exit('emscripten_set_immediate_loop.c')
49824980

4981+
@also_with_proxy_to_pthread
49834982
def test_emscripten_set_interval(self):
4984-
self.btest_exit('emscripten_set_interval.c', emcc_args=['-pthread', '-sPROXY_TO_PTHREAD'])
4983+
self.btest_exit('emscripten_set_interval.c')
49854984

49864985
# Test emscripten_performance_now() and emscripten_date_now()
4986+
@also_with_proxy_to_pthread
49874987
def test_emscripten_performance_now(self):
4988-
self.btest('emscripten_performance_now.c', '0', emcc_args=['-pthread', '-sPROXY_TO_PTHREAD'])
4988+
self.btest_exit('emscripten_performance_now.c')
49894989

49904990
def test_embind_with_pthreads(self):
49914991
self.btest_exit('embind/test_pthreads.cpp', emcc_args=['-lembind', '-pthread', '-sPTHREAD_POOL_SIZE=2'])
@@ -5057,12 +5057,9 @@ def test_minimal_runtime_loader_shell(self, args):
50575057
def test_minimal_runtime_hello_world(self, args):
50585058
self.btest_exit('small_hello_world.c', emcc_args=args + ['-sMINIMAL_RUNTIME'])
50595059

5060-
@parameterized({
5061-
'': ([],),
5062-
'pthread': (['-sPROXY_TO_PTHREAD', '-pthread'],),
5063-
})
5064-
def test_offset_converter(self, args):
5065-
self.btest_exit('test_offset_converter.c', emcc_args=['-sUSE_OFFSET_CONVERTER', '-gsource-map'] + args)
5060+
@also_with_proxy_to_pthread
5061+
def test_offset_converter(self):
5062+
self.btest_exit('test_offset_converter.c', emcc_args=['-sUSE_OFFSET_CONVERTER', '-gsource-map'])
50665063

50675064
# Tests emscripten_unwind_to_js_event_loop() behavior
50685065
def test_emscripten_unwind_to_js_event_loop(self):

test/test_other.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14150,9 +14150,8 @@ def test_pthread_trap(self):
1415014150
def test_pthread_kill(self):
1415114151
self.do_run_in_out_file_test('pthread/test_pthread_kill.c')
1415214152

14153-
@node_pthreads
1415414153
def test_emscripten_set_interval(self):
14155-
self.do_runf('emscripten_set_interval.c', args=['-pthread', '-sPROXY_TO_PTHREAD'])
14154+
self.do_runf('emscripten_set_interval.c')
1415614155

1415714156
# Test emscripten_console_log(), emscripten_console_warn() and emscripten_console_error()
1415814157
def test_emscripten_console_log(self):
@@ -14162,13 +14161,11 @@ def test_emscripten_console_log(self):
1416214161
def test_emscripten_unwind_to_js_event_loop(self):
1416314162
self.do_runf('test_emscripten_unwind_to_js_event_loop.c')
1416414163

14165-
@node_pthreads
1416614164
def test_emscripten_set_timeout(self):
14167-
self.do_runf('emscripten_set_timeout.c', args=['-pthread', '-sPROXY_TO_PTHREAD'])
14165+
self.do_runf('emscripten_set_timeout.c', emcc_args=['-sEXIT_RUNTIME'])
1416814166

14169-
@node_pthreads
1417014167
def test_emscripten_set_timeout_loop(self):
14171-
self.do_runf('emscripten_set_timeout_loop.c', args=['-pthread', '-sPROXY_TO_PTHREAD'])
14168+
self.do_runf('emscripten_set_timeout_loop.c', emcc_args=['-sEXIT_RUNTIME'])
1417214169

1417314170
# Verify that we are able to successfully compile a script when the Windows 7
1417414171
# and Python workaround env. vars are enabled.

0 commit comments

Comments
 (0)