Skip to content

Commit 5003f94

Browse files
authored
Convert more browser tests to btest_exit. NFC (#16535)
1 parent c804ce7 commit 5003f94

File tree

8 files changed

+65
-112
lines changed

8 files changed

+65
-112
lines changed

tests/browser/test_em_asm_blocking.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <assert.h>
2-
#include <emscripten.h>
2+
#include <emscripten/em_asm.h>
33
#include <stdio.h>
44
#include <thread>
55
#include <math.h>
@@ -18,15 +18,12 @@ void foo() {
1818
});
1919
printf("almost PI: %f\n", almost_PI);
2020
assert(fabs(almost_PI - 3.14159) < 0.001);
21-
atomic_store(&ret, len);
21+
ret = len;
2222
}
2323

2424
int main() {
2525
std::thread t(foo);
2626
t.join();
27-
printf("ret: %d\n", atomic_load(&ret));
28-
#ifdef REPORT_RESULT
29-
REPORT_RESULT(atomic_load(&ret));
30-
#endif
31-
return atomic_load(&ret);
27+
printf("ret: %d\n", ret.load());
28+
return ret.load();
3229
}

tests/fs/test_lz4fs.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
double before_it_all;
1616

17-
extern "C" {
18-
19-
void EMSCRIPTEN_KEEPALIVE finish() {
17+
extern "C" void EMSCRIPTEN_KEEPALIVE finish() {
2018
// load some file data, SYNCHRONOUSLY :)
2119
char buffer[100];
2220
int num;
@@ -123,9 +121,7 @@ void EMSCRIPTEN_KEEPALIVE finish() {
123121
#else
124122
result = 2;
125123
#endif
126-
REPORT_RESULT(result);
127-
}
128-
124+
emscripten_force_exit(result);
129125
}
130126

131127
int main() {

tests/fs/test_memfs_fsync.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55
* found in the LICENSE file.
66
*/
77

8+
#include <assert.h>
89
#include <stdio.h>
9-
#include <emscripten.h>
1010
#include <fcntl.h>
1111
#include <unistd.h>
1212
#include <sys/stat.h>
1313
#include <errno.h>
1414
#include <string.h>
1515

16-
int result = 1;
17-
1816
int main() {
19-
int fd;
17+
int fd, rtn;
2018

2119
// We first make sure the file doesn't currently exist.
2220
// We then write a file, call fsync, and close the file,
@@ -25,22 +23,20 @@ int main() {
2523
struct stat st;
2624

2725
// a file whose contents are just 'az'
28-
if ((stat("/wakaka.txt", &st) != -1) || (errno != ENOENT))
29-
result = -1000 - errno;
26+
rtn = stat("/wakaka.txt", &st);
27+
assert(rtn == -1 && errno == ENOENT);
28+
3029
fd = open("/wakaka.txt", O_RDWR | O_CREAT, 0666);
31-
if (fd == -1)
32-
result = -2000 - errno;
33-
else
34-
{
35-
if (write(fd,"az",2) != 2)
36-
result = -3000 - errno;
30+
assert(fd >= 0);
31+
32+
rtn = write(fd, "az", 2);
33+
assert(rtn == 2);
3734

38-
if (fsync(fd) != 0)
39-
result = -4000 - errno;
35+
rtn = fsync(fd);
36+
assert(rtn == 0);
4037

41-
if (close(fd) != 0)
42-
result = -5000 - errno;
43-
}
38+
rtn = close(fd);
39+
assert(rtn == 0);
4440

45-
REPORT_RESULT(result);
41+
return 0;
4642
}

tests/fs/test_workerfs_read.c

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* found in the LICENSE file.
66
*/
77

8-
#include <emscripten.h>
8+
#include <assert.h>
99
#include <sys/stat.h>
1010
#include <stdio.h>
11+
#include <stdbool.h>
1112
#include <string.h>
1213
#include <errno.h>
1314
#include <unistd.h>
@@ -16,94 +17,60 @@
1617

1718
#define SECRET_LEN 10
1819

19-
int result = 1;
20-
2120
int main() {
2221
int fd;
2322
int fd2;
23+
int rtn;
2424
struct stat st;
2525
struct stat st2;
2626
char buf[100];
2727
char secret2[] = SECRET2;
2828
int len2 = SECRET_LEN / 2;
2929

30-
if (stat("/work/notexist.txt", &st) != -1 || errno != ENOENT) {
31-
result = -1000 - errno;
32-
goto exit;
33-
}
30+
rtn = stat("/work/notexist.txt", &st);
31+
assert(rtn == -1 && errno == ENOENT);
3432

35-
if (stat("/work/blob.txt", &st) != 0) {
36-
result = -2000 - errno;
37-
goto exit;
38-
}
33+
rtn = stat("/work/blob.txt", &st);
34+
assert(rtn == 0);
3935

4036
fd = open("/work/blob.txt", O_RDWR, 0666);
41-
if (fd == -1) {
42-
result = -3000 - errno;
43-
goto exit;
44-
}
37+
assert(fd >= 0);
4538

46-
if (read(fd, buf, 1000) != SECRET_LEN ||
47-
strncmp(buf, SECRET, SECRET_LEN) != 0) {
48-
result = -4000 - errno;
49-
goto exit;
50-
}
39+
rtn = read(fd, buf, 1000);
40+
assert(rtn == SECRET_LEN);
41+
assert(strncmp(buf, SECRET, SECRET_LEN) == 0);
5142

5243
fd2 = open("/work/file.txt", O_RDONLY, 0666);
53-
if (fd2 == -1) {
54-
result = -5000 - errno;
55-
goto exit;
56-
}
44+
assert(fd2 != -1);
5745

58-
if (lseek(fd2, len2, SEEK_SET) != len2) {
59-
result = -6000 - errno;
60-
goto exit;
61-
}
46+
rtn = lseek(fd2, len2, SEEK_SET);
47+
assert(rtn == len2);
6248

63-
if (read(fd2, buf, len2) != len2 ||
64-
strncmp(buf, secret2 + len2, len2) != 0) {
65-
result = -7000 - errno;
66-
goto exit;
67-
}
49+
rtn = read(fd2, buf, len2);
50+
assert(rtn == len2);
51+
assert(strncmp(buf, secret2 + len2, len2) == 0);
6852

6953
stat("/work/file.txt", &st);
7054
chmod("/work/file.txt", 0640);
7155
stat("/work/file.txt", &st2);
72-
if (st.st_mode != (0777 | S_IFREG) || st2.st_mode != (0640 | S_IFREG)) {
73-
result = -8000 - errno;
74-
goto exit;
75-
}
76-
56+
assert(st.st_mode == (0777 | S_IFREG) && st2.st_mode == (0640 | S_IFREG));
57+
7758
DIR *pDir = opendir("/work/");
78-
if (pDir == NULL) {
79-
result = -9000 - errno;
80-
goto exit;
81-
}
82-
59+
assert(pDir);
60+
8361
int blobFileExists = 0;
8462
int fileTxtExists = 0;
8563
struct dirent *pDirent;
8664
while ((pDirent = readdir(pDir)) != NULL) {
8765
if (strcmp(pDirent->d_name, "blob.txt") == 0) {
8866
blobFileExists = 1;
89-
}
67+
}
9068
if (strcmp(pDirent->d_name, "file.txt") == 0) {
9169
fileTxtExists = 1;
9270
}
93-
94-
}
95-
96-
if (blobFileExists == 0) {
97-
result = -10000-errno;
98-
goto exit;
9971
}
100-
101-
if (fileTxtExists == 0) {
102-
result = -20000-errno;
103-
goto exit;
104-
}
105-
10672

107-
exit:
108-
REPORT_RESULT(result);
73+
assert(blobFileExists);
74+
assert(fileTxtExists);
75+
return 0;
10976
}

tests/hello_world_sdl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <emscripten.h>
1111
#endif
1212

13-
extern "C" int main(int argc, char** argv) {
13+
int main(int argc, char** argv) {
1414
printf("hello, world!\n");
1515

1616
SDL_Init(SDL_INIT_VIDEO);

tests/sdl_free_screen.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
#include <stdio.h>
77
#include <SDL/SDL.h>
88

9-
10-
extern "C" int main(int argc, char** argv) {
11-
9+
int main(int argc, char** argv) {
1210
SDL_Init(SDL_INIT_VIDEO);
1311
SDL_Surface *screen = SDL_SetVideoMode(256, 256, 32, SDL_SWSURFACE);
1412
printf("freeing screen...\n");

tests/sdl_key.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ int SDLCALL EventHandler(void *userdata, SDL_Event *event) {
4242
printf("b scancode\n"); result *= 23; break;
4343
}
4444
printf("unknown key: sym %d scancode %d\n", event->key.keysym.sym, event->key.keysym.scancode);
45-
REPORT_RESULT(result);
46-
emscripten_run_script("throw 'done'"); // comment this out to leave event handling active. Use the following to log DOM keys:
47-
// addEventListener('keyup', function(event) { console.log(event->keyCode) }, true)
45+
emscripten_force_exit(result); // comment this out to leave event handling active. Use the following to log DOM keys:
46+
// addEventListener('keyup', function(event) { console.log(event->keyCode) }, true)
4847
}
4948
}
5049
break;

tests/test_browser.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,8 @@ def test_sdl_key(self):
873873
%s
874874
}
875875
''' % ('setTimeout(function() {' if delay else '', '}, 1);' if delay else '', 'setTimeout(function() {' if delay else '', '}, 1);' if delay else ''))
876-
self.compile_btest([test_file('sdl_key.c'), '-o', 'page.html'] + defines + async_ + ['--pre-js', 'pre.js', '-sEXPORTED_FUNCTIONS=_main', '-lSDL', '-lGL'])
877-
self.run_browser('page.html', '', '/report_result?223092870')
876+
self.compile_btest([test_file('sdl_key.c'), '-o', 'page.html'] + defines + async_ + ['--pre-js', 'pre.js', '-sEXPORTED_FUNCTIONS=_main', '-lSDL', '-lGL', '-sEXIT_RUNTIME'])
877+
self.run_browser('page.html', '', '/report_result?exit:223092870')
878878

879879
def test_sdl_key_proxy(self):
880880
create_file('pre.js', '''
@@ -1359,7 +1359,7 @@ def test_fs_idbfs_fsync(self):
13591359
def test_fs_memfs_fsync(self):
13601360
args = ['-sASYNCIFY', '-sEXIT_RUNTIME']
13611361
secret = str(time.time())
1362-
self.btest(test_file('fs/test_memfs_fsync.c'), '1', args=args + ['-DSECRET=\"' + secret + '\"'])
1362+
self.btest_exit(test_file('fs/test_memfs_fsync.c'), args=args + ['-DSECRET=\"' + secret + '\"'])
13631363

13641364
def test_fs_workerfs_read(self):
13651365
secret = 'a' * 10
@@ -1376,7 +1376,7 @@ def test_fs_workerfs_read(self):
13761376
}, '/work');
13771377
};
13781378
''' % (secret, secret2))
1379-
self.btest(test_file('fs/test_workerfs_read.c'), '1', args=['-lworkerfs.js', '--pre-js', 'pre.js', '-DSECRET=\"' + secret + '\"', '-DSECRET2=\"' + secret2 + '\"', '--proxy-to-worker', '-lworkerfs.js'])
1379+
self.btest_exit(test_file('fs/test_workerfs_read.c'), args=['-lworkerfs.js', '--pre-js', 'pre.js', '-DSECRET=\"' + secret + '\"', '-DSECRET2=\"' + secret2 + '\"', '--proxy-to-worker', '-lworkerfs.js'])
13801380

13811381
def test_fs_workerfs_package(self):
13821382
create_file('file1.txt', 'first')
@@ -1396,37 +1396,37 @@ def test_fs_lz4fs_package(self):
13961396

13971397
# compress in emcc, -sLZ4 tells it to tell the file packager
13981398
print('emcc-normal')
1399-
self.btest(Path('fs/test_lz4fs.cpp'), '2', args=['-sLZ4=1', '--preload-file', 'file1.txt', '--preload-file', 'subdir/file2.txt', '--preload-file', 'file3.txt'])
1399+
self.btest_exit(Path('fs/test_lz4fs.cpp'), 2, args=['-sLZ4=1', '--preload-file', 'file1.txt', '--preload-file', 'subdir/file2.txt', '--preload-file', 'file3.txt'])
14001400
assert os.path.getsize('file1.txt') + os.path.getsize(Path('subdir/file2.txt')) + os.path.getsize('file3.txt') == 3 * 1024 * 128 * 10 + 1
14011401
assert os.path.getsize('test.data') < (3 * 1024 * 128 * 10) / 2 # over half is gone
14021402
print(' emcc-opts')
1403-
self.btest(Path('fs/test_lz4fs.cpp'), '2', args=['-sLZ4=1', '--preload-file', 'file1.txt', '--preload-file', 'subdir/file2.txt', '--preload-file', 'file3.txt', '-O2'])
1403+
self.btest_exit(Path('fs/test_lz4fs.cpp'), 2, args=['-sLZ4=1', '--preload-file', 'file1.txt', '--preload-file', 'subdir/file2.txt', '--preload-file', 'file3.txt', '-O2'])
14041404

14051405
# compress in the file packager, on the server. the client receives compressed data and can just use it. this is typical usage
14061406
print('normal')
14071407
out = subprocess.check_output([FILE_PACKAGER, 'files.data', '--preload', 'file1.txt', 'subdir/file2.txt', 'file3.txt', '--lz4'])
14081408
create_file('files.js', out, binary=True)
1409-
self.btest(Path('fs/test_lz4fs.cpp'), '2', args=['--pre-js', 'files.js', '-sLZ4=1', '-sFORCE_FILESYSTEM'])
1409+
self.btest_exit(test_file('fs/test_lz4fs.cpp'), 2, args=['--pre-js', 'files.js', '-sLZ4=1', '-sFORCE_FILESYSTEM'])
14101410
print(' opts')
1411-
self.btest(Path('fs/test_lz4fs.cpp'), '2', args=['--pre-js', 'files.js', '-sLZ4=1', '-sFORCE_FILESYSTEM', '-O2'])
1411+
self.btest_exit(test_file('fs/test_lz4fs.cpp'), 2, args=['--pre-js', 'files.js', '-sLZ4=1', '-sFORCE_FILESYSTEM', '-O2'])
14121412
print(' modularize')
1413-
self.compile_btest([test_file('fs/test_lz4fs.cpp'), '--pre-js', 'files.js', '-sLZ4=1', '-sFORCE_FILESYSTEM', '-sMODULARIZE=1'])
1413+
self.compile_btest([test_file('fs/test_lz4fs.cpp'), '--pre-js', 'files.js', '-sLZ4=1', '-sFORCE_FILESYSTEM', '-sMODULARIZE=1', '-sEXIT_RUNTIME'])
14141414
create_file('a.html', '''
14151415
<script src="a.out.js"></script>
14161416
<script>
14171417
Module()
14181418
</script>
14191419
''')
1420-
self.run_browser('a.html', '.', '/report_result?2')
1420+
self.run_browser('a.html', '.', '/report_result?exit:2')
14211421

14221422
# load the data into LZ4FS manually at runtime. This means we compress on the client. This is generally not recommended
14231423
print('manual')
14241424
subprocess.check_output([FILE_PACKAGER, 'files.data', '--preload', 'file1.txt', 'subdir/file2.txt', 'file3.txt', '--separate-metadata', '--js-output=files.js'])
1425-
self.btest(Path('fs/test_lz4fs.cpp'), '1', args=['-DLOAD_MANUALLY', '-sLZ4=1', '-sFORCE_FILESYSTEM'])
1425+
self.btest_exit(test_file('fs/test_lz4fs.cpp'), 1, args=['-DLOAD_MANUALLY', '-sLZ4=1', '-sFORCE_FILESYSTEM'])
14261426
print(' opts')
1427-
self.btest(Path('fs/test_lz4fs.cpp'), '1', args=['-DLOAD_MANUALLY', '-sLZ4=1', '-sFORCE_FILESYSTEM', '-O2'])
1427+
self.btest_exit(test_file('fs/test_lz4fs.cpp'), 1, args=['-DLOAD_MANUALLY', '-sLZ4=1', '-sFORCE_FILESYSTEM', '-O2'])
14281428
print(' opts+closure')
1429-
self.btest(Path('fs/test_lz4fs.cpp'), '1', args=['-DLOAD_MANUALLY', '-sLZ4=1', '-sFORCE_FILESYSTEM', '-O2', '--closure=1', '-g1', '-sCLOSURE_WARNINGS=quiet'])
1429+
self.btest_exit(test_file('fs/test_lz4fs.cpp'), 1, args=['-DLOAD_MANUALLY', '-sLZ4=1', '-sFORCE_FILESYSTEM', '-O2', '--closure=1', '-g1', '-sCLOSURE_WARNINGS=quiet'])
14301430

14311431
'''# non-lz4 for comparison
14321432
try:
@@ -1438,7 +1438,7 @@ def test_fs_lz4fs_package(self):
14381438
shutil.copyfile('file3.txt', Path('files/file3.txt'))
14391439
out = subprocess.check_output([FILE_PACKAGER, 'files.data', '--preload', 'files/file1.txt', 'files/file2.txt', 'files/file3.txt'])
14401440
create_file('files.js', out, binary=True)
1441-
self.btest(Path('fs/test_lz4fs.cpp'), '2', args=['--pre-js', 'files.js'])'''
1441+
self.btest_exit(test_file('fs/test_lz4fs.cpp'), 2, args=['--pre-js', 'files.js'])'''
14421442

14431443
def test_separate_metadata_later(self):
14441444
# see issue #6654 - we need to handle separate-metadata both when we run before
@@ -4251,8 +4251,8 @@ def test_main_thread_async_em_asm(self):
42514251
def test_main_thread_em_asm_blocking(self):
42524252
create_file('page.html', read_file(test_file('browser/test_em_asm_blocking.html')))
42534253

4254-
self.compile_btest([test_file('browser/test_em_asm_blocking.cpp'), '-O2', '-o', 'wasm.js', '-sUSE_PTHREADS', '-sPROXY_TO_PTHREAD'])
4255-
self.run_browser('page.html', '', '/report_result?8')
4254+
self.compile_btest([test_file('browser/test_em_asm_blocking.cpp'), '-O2', '-o', 'wasm.js', '-sUSE_PTHREADS', '-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'])
4255+
self.run_browser('page.html', '', '/report_result?exit:8')
42564256

42574257
# Test that it is possible to send a signal via calling alarm(timeout), which in turn calls to the signal handler set by signal(SIGALRM, func);
42584258
def test_sigalrm(self):

0 commit comments

Comments
 (0)