Skip to content

Commit 9886169

Browse files
authored
Cleanup test_fs_idbfs_sync. NFC (#24820)
Use `assert` rather then complex set of return codes. Use `btest_exit` to report the results.
1 parent ef5ae35 commit 9886169

File tree

2 files changed

+93
-98
lines changed

2 files changed

+93
-98
lines changed

test/fs/test_idbfs_sync.c

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

8+
#include <assert.h>
9+
#include <stdbool.h>
10+
#include <stdlib.h>
811
#include <stdio.h>
912
#include <emscripten.h>
1013
#include <fcntl.h>
@@ -13,136 +16,127 @@
1316
#include <errno.h>
1417
#include <string.h>
1518

16-
int result = 1;
19+
EM_JS_DEPS(deps, "$callUserCallback");
1720

18-
void report_result() {
19-
REPORT_RESULT(result);
20-
#ifdef FORCE_EXIT
21+
bool test_complete = false;
22+
23+
void finish() {
24+
printf("finish\n");
25+
test_complete = true;
2126
emscripten_force_exit(0);
22-
#endif
27+
}
28+
29+
void cleanup() {
30+
// If the test failed, then delete test files from IndexedDB so that the test
31+
// runner will not leak test state to subsequent tests that reuse this same
32+
// file.
33+
printf("cleaning up test files\n");
34+
unlink("/working1/empty.txt");
35+
unlink("/working1/waka.txt");
36+
unlink("/working1/moar.txt");
37+
rmdir("/working1/dir");
38+
EM_ASM(FS.syncfs(function(){})); // And persist deleted changes
2339
}
2440

2541
void test() {
26-
int fd;
42+
int fd, res;
2743
struct stat st;
2844

2945
#if FIRST
46+
printf("running test FIRST half ..\n");
47+
48+
// Run cleanup first in case a previous test failed half way through.
49+
cleanup();
3050

3151
// for each file, we first make sure it doesn't currently exist
3252
// (we delete it at the end of !FIRST). We then test an empty
3353
// file plus two files each with a small amount of content
3454

3555
// the empty file
36-
if ((stat("/working1/empty.txt", &st) != -1) || (errno != ENOENT))
37-
result = -1000 - errno;
56+
res = stat("/working1/empty.txt", &st);
57+
assert(res == -1 && errno == ENOENT);
58+
3859
fd = open("/working1/empty.txt", O_RDWR | O_CREAT, 0666);
39-
if (fd == -1)
40-
result = -2000 - errno;
41-
else if (close(fd) != 0)
42-
result = -3000 - errno;
60+
assert(fd != -1);
61+
res = close(fd);
62+
assert(res == 0);
4363

4464
// a file whose contents are just 'az'
45-
if ((stat("/working1/waka.txt", &st) != -1) || (errno != ENOENT))
46-
result = -4000 - errno;
65+
res = stat("/working1/waka.txt", &st);
66+
assert(res == -1 && errno == ENOENT);
4767
fd = open("/working1/waka.txt", O_RDWR | O_CREAT, 0666);
48-
if (fd == -1)
49-
result = -5000 - errno;
50-
else {
51-
if (write(fd,"az",2) != 2)
52-
result = -6000 - errno;
53-
if (close(fd) != 0)
54-
result = -7000 - errno;
55-
}
68+
assert(fd != -1);
69+
res = write(fd, "az", 2);
70+
assert(res == 2);
71+
res = close(fd);
72+
assert(res == 0);
5673

5774
// a file whose contents are random-ish string set by the test_browser.py file
58-
if ((stat("/working1/moar.txt", &st) != -1) || (errno != ENOENT))
59-
result = -8000 - errno;
75+
res = stat("/working1/moar.txt", &st);
76+
assert(res == -1 && errno == ENOENT);
6077
fd = open("/working1/moar.txt", O_RDWR | O_CREAT, 0666);
61-
if (fd == -1)
62-
result = -9000 - errno;
63-
else {
64-
if (write(fd, SECRET, strlen(SECRET)) != strlen(SECRET))
65-
result = -10000 - errno;
66-
if (close(fd) != 0)
67-
result = -11000 - errno;
68-
}
78+
assert(fd != -1);
79+
res = write(fd, SECRET, strlen(SECRET));
80+
assert(res == strlen(SECRET));
81+
res = close(fd);
82+
assert(res == 0);
6983

7084
// a directory
71-
if ((stat("/working1/dir", &st) != -1) || (errno != ENOENT))
72-
result = -12000 - errno;
73-
else if (mkdir("/working1/dir", 0777) != 0)
74-
result = -13000 - errno;
85+
res = stat("/working1/dir", &st);
86+
assert(res == -1 && errno == ENOENT);
87+
res = mkdir("/working1/dir", 0777);
88+
assert(res == 0);
7589

7690
#else
91+
printf("running test SECOND half ..\n");
7792

7893
// does the empty file exist?
7994
fd = open("/working1/empty.txt", O_RDONLY);
80-
if (fd == -1)
81-
result = -14000 - errno;
82-
else if (close(fd) != 0)
83-
result = -15000 - errno;
84-
if (unlink("/working1/empty.txt") != 0)
85-
result = -16000 - errno;
95+
assert(fd != -1);
96+
res = close(fd);
97+
assert(res == 0);
98+
res = unlink("/working1/empty.txt");
99+
assert(res == 0);
86100

87101
// does the 'az' file exist, and does it contain 'az'?
88102
fd = open("/working1/waka.txt", O_RDONLY);
89-
if (fd == -1)
90-
result = -17000 - errno;
91-
else {
103+
assert(fd != -1);
104+
{
92105
char bf[4];
93106
int bytes_read = read(fd,&bf[0],sizeof(bf));
94-
if (bytes_read != 2)
95-
result = -18000;
96-
else if ((bf[0] != 'a') || (bf[1] != 'z'))
97-
result = -19000;
98-
if (close(fd) != 0)
99-
result = -20000 - errno;
100-
if (unlink("/working1/waka.txt") != 0)
101-
result = -21000 - errno;
107+
assert(bytes_read == 2);
108+
assert(bf[0] == 'a' && bf[1] == 'z');
102109
}
110+
res = close(fd);
111+
assert(res == 0);
112+
res = unlink("/working1/waka.txt");
113+
assert(res == 0);
103114

104115
// does the random-ish file exist and does it contain SECRET?
105116
fd = open("/working1/moar.txt", O_RDONLY);
106-
if (fd == -1) {
107-
result = -22000 - errno;
108-
} else {
117+
assert(fd != -1);
118+
{
109119
char bf[256];
110120
int bytes_read = read(fd,&bf[0],sizeof(bf));
111-
if (bytes_read != strlen(SECRET)) {
112-
result = -23000;
113-
} else {
114-
bf[strlen(SECRET)] = 0;
115-
if (strcmp(bf,SECRET) != 0)
116-
result = -24000;
117-
}
118-
if (close(fd) != 0)
119-
result = -25000 - errno;
120-
if (unlink("/working1/moar.txt") != 0)
121-
result = -26000 - errno;
121+
assert(bytes_read == strlen(SECRET));
122+
bf[strlen(SECRET)] = 0;
123+
assert(strcmp(bf, SECRET) == 0);
122124
}
125+
res = close(fd);
126+
assert(res == 0);
127+
res = unlink("/working1/moar.txt");
128+
assert(res == 0);
123129

124130
// does the directory exist?
125-
if (stat("/working1/dir", &st) != 0) {
126-
result = -27000 - errno;
127-
} else {
128-
if (!S_ISDIR(st.st_mode))
129-
result = -28000;
130-
if (rmdir("/working1/dir") != 0)
131-
result = -29000 - errno;
132-
}
131+
res = stat("/working1/dir", &st);
132+
assert(res == 0);
133+
assert(S_ISDIR(st.st_mode));
134+
res = rmdir("/working1/dir");
135+
assert(res == 0);
133136

134137
#endif
135138

136-
// If the test failed, then delete test files from IndexedDB so that the test
137-
// runner will not leak test state to subsequent tests that reuse this same
138-
// file.
139-
if (result != 1) {
140-
unlink("/working1/empty.txt");
141-
unlink("/working1/waka.txt");
142-
unlink("/working1/moar.txt");
143-
rmdir("/working1/dir");
144-
EM_ASM(FS.syncfs(function(){})); // And persist deleted changes
145-
}
139+
printf("done test ..\n");
146140

147141
#if EXTRA_WORK && !FIRST
148142
EM_ASM(
@@ -156,27 +150,29 @@ void test() {
156150
#endif
157151

158152
#ifdef IDBFS_AUTO_PERSIST
159-
report_result();
153+
finish();
160154
#else
161155
// sync from memory state to persisted and then
162-
// run 'report_result'
163-
EM_ASM(
156+
// run 'finish'
157+
EM_ASM({
164158
// Ensure IndexedDB is closed at exit.
165-
Module['onExit'] = function() {
159+
var orig = Module['onExit'];
160+
Module['onExit'] = (status) => {
166161
assert(Object.keys(IDBFS.dbs).length == 0);
162+
orig(status);
167163
};
168-
FS.syncfs(function (err) {
164+
FS.syncfs((err) => {
169165
assert(!err);
170-
ccall('report_result', 'v');
166+
callUserCallback(_finish);
167+
});
171168
});
172-
);
173169
#endif
174170
}
175171

176172
int main() {
177173
EM_ASM(
178174
FS.mkdir('/working1');
179-
FS.mount(IDBFS, {
175+
FS.mount(IDBFS, {
180176
#ifdef IDBFS_AUTO_PERSIST
181177
autoPersist: true
182178
#endif
@@ -191,7 +187,7 @@ int main() {
191187
// run the 'test' function
192188
FS.syncfs(true, function (err) {
193189
assert(!err);
194-
ccall('test', 'v');
190+
callUserCallback(_test);
195191
});
196192
);
197193

test/test_browser.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,13 +1343,12 @@ def test_fflush(self):
13431343
'': ([],),
13441344
'extra': (['-DEXTRA_WORK'],),
13451345
'autopersist': (['-DIDBFS_AUTO_PERSIST'],),
1346-
'force_exit': (['-sEXIT_RUNTIME', '-DFORCE_EXIT'],),
13471346
})
13481347
def test_fs_idbfs_sync(self, args):
1349-
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', '$ccall')
13501348
secret = str(time.time())
1351-
self.btest('fs/test_idbfs_sync.c', '1', cflags=['-lidbfs.js', f'-DSECRET="{secret}"', '-sEXPORTED_FUNCTIONS=_main,_test,_report_result', '-lidbfs.js'] + args + ['-DFIRST'])
1352-
self.btest('fs/test_idbfs_sync.c', '1', cflags=['-lidbfs.js', f'-DSECRET="{secret}"', '-sEXPORTED_FUNCTIONS=_main,_test,_report_result', '-lidbfs.js'] + args)
1349+
self.btest_exit('fs/test_idbfs_sync.c', cflags=['-lidbfs.js', f'-DSECRET="{secret}"', '-sEXPORTED_FUNCTIONS=_main,_test,_finish', '-lidbfs.js'] + args + ['-DFIRST'])
1350+
print('done first half')
1351+
self.btest_exit('fs/test_idbfs_sync.c', cflags=['-lidbfs.js', f'-DSECRET="{secret}"', '-sEXPORTED_FUNCTIONS=_main,_test,_finish', '-lidbfs.js'] + args)
13531352

13541353
def test_fs_idbfs_fsync(self):
13551354
# sync from persisted state into memory before main()

0 commit comments

Comments
 (0)