Skip to content

Commit 3a0499e

Browse files
authored
Run also_with_pthreads decorator and use it more places. NFC (#26843)
This decorator now also delegates to `require_pthreads()` helper method. Split out from #26827
1 parent 5daefa5 commit 3a0499e

6 files changed

Lines changed: 45 additions & 57 deletions

File tree

test/common.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,14 @@ def check_dylink(self):
457457

458458
def require_pthreads(self):
459459
self.cflags += ['-Wno-pthreads-mem-growth', '-pthread']
460-
if self.get_setting('MINIMAL_RUNTIME'):
461-
self.skipTest('non-browser pthreads not yet supported with MINIMAL_RUNTIME')
462-
for engine in self.js_engines:
463-
if engine_is_node(engine) or engine_is_bun(engine) or engine_is_deno(engine):
464-
self.require_engine(engine)
465-
return
466-
self.fail('no JS engine found capable of running pthreads')
460+
if not self.is_browser_test():
461+
if self.get_setting('MINIMAL_RUNTIME'):
462+
self.skipTest('non-browser pthreads not yet supported with MINIMAL_RUNTIME')
463+
for engine in self.js_engines:
464+
if engine_is_node(engine) or engine_is_bun(engine) or engine_is_deno(engine):
465+
self.require_engine(engine)
466+
return
467+
self.fail('no JS engine found capable of running pthreads')
467468

468469
def require_v8(self):
469470
if 'EMTEST_SKIP_V8' in os.environ:

test/decorators.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ def modified(self, *args, **kwargs):
252252
return decorated
253253

254254

255+
def also_with_pthreads(f):
256+
assert callable(f)
257+
258+
@wraps(f)
259+
def decorated(self, threads, *args, **kwargs):
260+
if threads:
261+
self.require_pthreads()
262+
f(self, *args, **kwargs)
263+
264+
parameterize(decorated, {'': (False,),
265+
'pthreads': (True,)})
266+
267+
return decorated
268+
269+
255270
def also_with_wasmfs(func):
256271
assert callable(func)
257272

@@ -594,7 +609,7 @@ def parameterize(func, parameters):
594609
test functions.
595610
"""
596611
prev = getattr(func, '_parameterize', None)
597-
assert not any(p.startswith('_') for p in parameters)
612+
assert not any(p.startswith('_') for p in parameters), 'test variant names should not start with _'
598613
if prev:
599614
# If we're parameterizing 2nd time, construct a cartesian product for various combinations.
600615
func._parameterize = {

test/test_browser.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
also_with_asan,
5050
also_with_fetch_streaming,
5151
also_with_minimal_runtime,
52+
also_with_pthreads,
5253
also_with_wasm2js,
5354
also_with_wasmfs,
5455
disabled,
@@ -181,21 +182,6 @@ def is_jspi(args):
181182
return '-sJSPI' in args
182183

183184

184-
def also_with_threads(f):
185-
assert callable(f)
186-
187-
@wraps(f)
188-
def decorated(self, threads, *args, **kwargs):
189-
if threads:
190-
self.cflags += ['-pthread']
191-
f(self, *args, **kwargs)
192-
193-
parameterize(decorated, {'': (False,),
194-
'pthreads': (True,)})
195-
196-
return decorated
197-
198-
199185
def also_with_proxy_to_pthread(f):
200186
assert callable(f)
201187

@@ -3676,7 +3662,7 @@ def test_pthread_64bit_atomics(self):
36763662
self.btest_exit('pthread/test_pthread_64bit_atomics.c', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8'])
36773663

36783664
# Test 64-bit C++11 atomics.
3679-
@also_with_threads
3665+
@also_with_pthreads
36803666
@parameterized({
36813667
'': ([],),
36823668
'O3': (['-O3'],),
@@ -3932,7 +3918,7 @@ def test_pthread_sbrk(self, args):
39323918

39333919
# Test that -sABORTING_MALLOC=0 works in both pthreads and non-pthreads
39343920
# builds. (sbrk fails gracefully)
3935-
@also_with_threads
3921+
@also_with_pthreads
39363922
@parameterized({
39373923
'': ([],),
39383924
'O2': (['-O2'],),
@@ -4157,11 +4143,11 @@ def test_wasm_locate_file(self):
41574143
shutil.move('test.wasm', Path('cdn/test.wasm'))
41584144
self.run_browser('test.html', '/report_result?0')
41594145

4160-
@also_with_threads
4146+
@also_with_pthreads
41614147
def test_utf8_textdecoder(self):
41624148
self.btest_exit('benchmark/benchmark_utf8.c', 0, cflags=['--embed-file', test_file('utf8_corpus.txt') + '@/utf8_corpus.txt'])
41634149

4164-
@also_with_threads
4150+
@also_with_pthreads
41654151
def test_utf16_textdecoder(self):
41664152
self.btest_exit('benchmark/benchmark_utf16.cpp', 0, cflags=['--embed-file', test_file('utf16_corpus.txt') + '@/utf16_corpus.txt', '-sEXPORTED_RUNTIME_METHODS=UTF16ToString,stringToUTF16,lengthBytesUTF16'])
41674153

@@ -5560,7 +5546,7 @@ def test_error_reporting(self):
55605546
create_file('post.js', 'throw "foo";')
55615547
self.btest('hello_world.c', cflags=['--post-js=post.js'], expected='exception:foo')
55625548

5563-
@also_with_threads
5549+
@also_with_pthreads
55645550
@also_with_wasm2js
55655551
@parameterized({
55665552
'': ([],),
@@ -5582,7 +5568,7 @@ def test_webpack(self, args):
55825568
shutil.copy('src/hello.wasm', 'dist/')
55835569
self.run_browser('dist/index.html', '/report_result?exit:0')
55845570

5585-
@also_with_threads
5571+
@also_with_pthreads
55865572
@requires_dev_dependency('vite')
55875573
@parameterized({
55885574
'': ([],),
@@ -5594,7 +5580,7 @@ def test_vite(self, args):
55945580
self.run_process(shared.get_npm_cmd('vite') + ['build'])
55955581
self.run_browser('dist/index.html', '/report_result?exit:0')
55965582

5597-
@also_with_threads
5583+
@also_with_pthreads
55985584
@requires_dev_dependency('rollup')
55995585
def test_rollup(self):
56005586
copytree(test_file('rollup'), '.')

test/test_core.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
also_with_nodefs,
4646
also_with_nodefs_both,
4747
also_with_noderawfs,
48+
also_with_pthreads,
4849
also_with_standalone_wasm,
4950
also_with_wasmfs,
5051
also_without_bigint,
@@ -6812,16 +6813,10 @@ def test_freetype(self):
68126813
@no_ubsan('local count too large for VMs')
68136814
@is_slow_test
68146815
@also_with_wasmfs
6815-
@parameterized({
6816-
'': (False,),
6817-
'pthreads': (True,),
6818-
})
6819-
def test_sqlite(self, use_pthreads):
6816+
@also_with_pthreads
6817+
def test_sqlite(self):
68206818
if self.get_setting('STRICT'):
68216819
self.cflags += ['-lstubs']
6822-
if use_pthreads:
6823-
self.cflags.append('-pthread')
6824-
self.require_pthreads()
68256820
self.do_run_in_out_file_test('sqlite/test.c', cflags=['-sUSE_SQLITE3'])
68266821

68276822
@needs_make('mingw32-make')
@@ -9758,15 +9753,10 @@ def test_esm_integration(self):
97589753
def test_modularize_instance_hello(self):
97599754
self.do_core_test('test_hello_world.c', cflags=['-sMODULARIZE=instance', '-Wno-experimental'])
97609755

9761-
@parameterized({
9762-
'': ([],),
9763-
'pthreads': (['-pthread'],),
9764-
})
97659756
@no_omit_asm_module_exports('MODULARIZE is not compatible with DECLARE_ASM_MODULE_EXPORTS=0')
97669757
@no_strict_js('EXPORT_ES6 is not compatible with STRICT_JS')
9767-
def test_modularize_instance(self, args):
9768-
if args:
9769-
self.require_pthreads()
9758+
@also_with_pthreads
9759+
def test_modularize_instance(self):
97709760
create_file('library.js', '''\
97719761
addToLibrary({
97729762
$baz: () => console.log('baz'),
@@ -9778,7 +9768,7 @@ def test_modularize_instance(self, args):
97789768
'-sEXPORTED_RUNTIME_METHODS=baz,addOnExit,HEAP32,runtimeKeepalivePush,runtimeKeepalivePop',
97799769
'-sEXPORTED_FUNCTIONS=_bar,_main,qux',
97809770
'--js-library', 'library.js',
9781-
'-o', 'modularize_instance.mjs'] + args + self.get_cflags())
9771+
'-o', 'modularize_instance.mjs'] + self.get_cflags())
97829772

97839773
create_file('runner.mjs', '''
97849774
import { strict as assert } from 'assert';

test/test_emrun.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from browser_common import BrowserCore, get_browser, has_browser
1212
from common import EMRUN, RunnerCore, path_from_root, read_file, test_file
13-
from test_browser import also_with_threads
13+
from decorators import also_with_pthreads
1414

1515
from tools.shared import EMCC, PIPE
1616

@@ -60,7 +60,7 @@ def test_program_arg_separator(self):
6060
self.assertContained('error: unrecognized arguments: --foo', err)
6161
self.assertContained('remember to add `--` between arguments', err)
6262

63-
@also_with_threads
63+
@also_with_pthreads
6464
def test_emrun(self):
6565
self.emcc('test_emrun.c', ['--emrun', '-o', 'test_emrun.html'])
6666
if not has_browser():

test/test_other.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
also_with_modularize,
6363
also_with_nodefs_both,
6464
also_with_noderawfs,
65+
also_with_pthreads,
6566
also_with_standalone_wasm,
6667
also_with_wasm2js,
6768
also_with_wasm64,
@@ -15145,22 +15146,17 @@ def test_install(self):
1514515146
self.assertNotExists('newdir/tools/maint/')
1514615147

1514715148
@requires_node
15148-
@parameterized({
15149-
'': ([],),
15150-
'pthreads': (['-pthread'],),
15151-
})
15149+
@also_with_pthreads
1515215150
@parameterized({
1515315151
'': ([],),
1515415152
'closure': (['--closure=1'],),
1515515153
})
15156-
def test_TextDecoder(self, args1, args2):
15157-
self.cflags += args1 + args2
15158-
15159-
self.do_runf('hello_world.c')
15154+
def test_TextDecoder(self, args):
15155+
self.do_runf('hello_world.c', cflags=args)
1516015156
td_with_fallback = os.path.getsize('hello_world.js')
1516115157
print('td_with_fallback:\t%s' % td_with_fallback)
1516215158

15163-
self.do_runf('hello_world.c', cflags=['-sTEXTDECODER=2'])
15159+
self.do_runf('hello_world.c', cflags=args + ['-sTEXTDECODER=2'])
1516415160
td_without_fallback = os.path.getsize('hello_world.js')
1516515161
print('td_without_fallback:\t%s' % td_without_fallback)
1516615162

0 commit comments

Comments
 (0)