Skip to content

Commit 031d92a

Browse files
committed
Rename/cleanup fetch tests. NFC
1 parent 75892b3 commit 031d92a

14 files changed

+113
-118
lines changed

test/fetch/response_headers.cpp

Lines changed: 0 additions & 79 deletions
This file was deleted.

test/fetch/cached_xhr.cpp renamed to test/fetch/test_fetch_cached_xhr.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
int result = 0;
1313

1414
// Fetch file without XHRing.
15-
void fetchFromIndexedDB()
16-
{
15+
void fetchFromIndexedDB() {
1716
emscripten_fetch_attr_t attr;
1817
emscripten_fetch_attr_init(&attr);
1918
strcpy(attr.requestMethod, "GET");
@@ -36,8 +35,7 @@ void fetchFromIndexedDB()
3635
}
3736

3837
// XHR and store to cache.
39-
int main()
40-
{
38+
int main() {
4139
emscripten_fetch_attr_t attr;
4240
emscripten_fetch_attr_init(&attr);
4341
strcpy(attr.requestMethod, "GET");
@@ -60,6 +58,8 @@ int main()
6058
attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_PERSIST_FILE;
6159
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
6260
assert(fetch != 0);
63-
memset(&attr, 0, sizeof(attr)); // emscripten_fetch() must be able to operate without referencing to this structure after the call.
61+
// emscripten_fetch() must be able to operate without referencing to this
62+
// structure after the call.
63+
memset(&attr, 0, sizeof(attr));
6464
return 99;
6565
}
File renamed without changes.

test/fetch/headers_received.cpp renamed to test/fetch/test_fetch_headers_received.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
#include <assert.h>
1010
#include <emscripten/fetch.h>
1111

12-
void readyStateChange(emscripten_fetch_t *fetch)
13-
{
14-
if(fetch->readyState != 2) return;
12+
void readyStateChange(emscripten_fetch_t *fetch) {
13+
if (fetch->readyState != 2) return;
1514

1615
size_t headersLengthBytes = emscripten_fetch_get_response_headers_length(fetch) + 1;
17-
char *headerString = new char[headersLengthBytes];
16+
char *headerString = malloc(headersLengthBytes);
1817

1918
assert(headerString);
2019
emscripten_fetch_get_response_headers(fetch, headerString, headersLengthBytes);
@@ -23,11 +22,10 @@ void readyStateChange(emscripten_fetch_t *fetch)
2322
char **responseHeaders = emscripten_fetch_unpack_response_headers(headerString);
2423
assert(responseHeaders);
2524

26-
delete[] headerString;
25+
free(headerString);
2726

2827
int numHeaders = 0;
29-
for(; responseHeaders[numHeaders * 2]; ++numHeaders)
30-
{
28+
for (; responseHeaders[numHeaders * 2]; ++numHeaders) {
3129
// Check both the header and its value are present.
3230
assert(responseHeaders[(numHeaders * 2) + 1]);
3331
printf("Got response header: %s:%s\n", responseHeaders[numHeaders * 2], responseHeaders[(numHeaders * 2) + 1]);
@@ -38,22 +36,27 @@ void readyStateChange(emscripten_fetch_t *fetch)
3836
emscripten_fetch_free_unpacked_response_headers(responseHeaders);
3937
}
4038

41-
void success(emscripten_fetch_t *fetch)
42-
{
39+
void success(emscripten_fetch_t *fetch) {
4340
printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url);
4441
// The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];
4542
emscripten_fetch_close(fetch); // Free data associated with the fetch.
4643
}
4744

48-
int main()
49-
{
45+
void onerror(emscripten_fetch_t *fetch) {
46+
printf("onerror: %d '%s'\n", fetch->status, fetch->statusText);
47+
abort();
48+
}
49+
50+
int main() {
5051
emscripten_fetch_attr_t attr;
5152
emscripten_fetch_attr_init(&attr);
5253
strcpy(attr.requestMethod, "GET");
5354
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_REPLACE;
5455
attr.onsuccess = success;
56+
attr.onerror = onerror;
5557
attr.onreadystatechange = readyStateChange;
5658
attr.timeoutMSecs = 2*60;
59+
printf("Calling emscripten_fetch\n");
5760
emscripten_fetch(&attr, "myfile.dat");
5861
return 0;
5962
}

test/fetch/idb_delete.cpp renamed to test/fetch/test_fetch_idb_delete.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ int main()
1717
strcpy(attr.requestMethod, "GET");
1818
attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_PERSIST_FILE;
1919
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
20+
printf("fetch->status: %d\n", fetch->status);
2021
assert(fetch->status == 200 && "Initial XHR GET of gears.png should have succeeded");
2122
emscripten_fetch_close(fetch);
2223

File renamed without changes.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2018 The Emscripten Authors. All rights reserved.
2+
// Emscripten is available under two separate licenses, the MIT license and the
3+
// University of Illinois/NCSA Open Source License. Both these licenses can be
4+
// found in the LICENSE file.
5+
6+
#include <string.h>
7+
#include <stdio.h>
8+
#include <math.h>
9+
#include <assert.h>
10+
#include <emscripten/fetch.h>
11+
12+
int result = 1;
13+
14+
int main() {
15+
static const char* const headers[] = {
16+
"X-Emscripten-Test",
17+
"1",
18+
0,
19+
};
20+
const size_t n_values = sizeof(headers) / sizeof(headers[0]) - 1;
21+
emscripten_fetch_attr_t attr;
22+
emscripten_fetch_attr_init(&attr);
23+
strcpy(attr.requestMethod, "GET");
24+
attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
25+
attr.requestHeaders = headers;
26+
27+
attr.onsuccess = [] (emscripten_fetch_t *fetch) {
28+
assert(fetch->__attributes.requestHeaders != 0);
29+
assert(fetch->__attributes.requestHeaders != headers);
30+
for (size_t i = 0; i < n_values; ++i) {
31+
const char* origHeader = headers[i];
32+
const char* header = fetch->__attributes.requestHeaders[i];
33+
assert(origHeader != header);
34+
assert(strcmp(origHeader, header) == 0);
35+
}
36+
assert(fetch->__attributes.requestHeaders[n_values] == 0);
37+
38+
printf("Finished downloading %llu bytes\n", fetch->numBytes);
39+
// Compute rudimentary checksum of data
40+
uint8_t checksum = 0;
41+
for (int i = 0; i < fetch->numBytes; ++i) {
42+
checksum ^= fetch->data[i];
43+
}
44+
printf("Data checksum: %02X\n", checksum);
45+
assert(checksum == 0x08);
46+
emscripten_fetch_close(fetch);
47+
48+
if (result == 1) result = 0;
49+
};
50+
51+
attr.onprogress = [] (emscripten_fetch_t *fetch) {
52+
if (fetch->totalBytes > 0) {
53+
printf("Downloading.. %.2f%% complete.\n", (fetch->dataOffset + fetch->numBytes) * 100.0 / fetch->totalBytes);
54+
} else {
55+
printf("Downloading.. %lld bytes complete.\n", fetch->dataOffset + fetch->numBytes);
56+
}
57+
};
58+
59+
attr.onerror = [] (emscripten_fetch_t *fetch) {
60+
printf("Download failed!\n");
61+
emscripten_fetch_close(fetch);
62+
assert(false && "Shouldn't fail!");
63+
};
64+
65+
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
66+
if (result != 0) {
67+
result = 2;
68+
printf("emscripten_fetch() failed to run synchronously!\n");
69+
}
70+
return result;
71+
}
File renamed without changes.

test/fetch/sync_xhr.cpp renamed to test/fetch/test_fetch_sync_xhr.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212

1313
int result = -1;
1414

15-
int main()
16-
{
17-
// If an exception is thrown from the user callback, it bubbles up to self.onerror but is otherwise completely
18-
// swallowed by xhr.send.
15+
int main() {
16+
// If an exception is thrown from the user callback, it bubbles up to
17+
// self.onerror but is otherwise completely swallowed by xhr.send.
1918
EM_ASM({self.onerror = function() {
2019
out('Got error');
2120
HEAP32[$0 >> 2] = 2;

test/fetch/to_indexeddb.cpp renamed to test/fetch/test_fetch_to_indexeddb.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
#include <assert.h>
1010
#include <emscripten/fetch.h>
1111

12-
int main()
13-
{
12+
int main() {
1413
emscripten_fetch_attr_t attr;
1514
emscripten_fetch_attr_init(&attr);
1615
strcpy(attr.requestMethod, "GET");
@@ -45,6 +44,8 @@ int main()
4544

4645
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
4746
assert(fetch != 0);
48-
memset(&attr, 0, sizeof(attr)); // emscripten_fetch() must be able to operate without referencing to this structure after the call.
47+
// emscripten_fetch() must be able to operate without referencing to this
48+
// structure after the call.
49+
memset(&attr, 0, sizeof(attr));
4950
return 99;
5051
}
File renamed without changes.
File renamed without changes.

test/test_browser.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4823,13 +4823,13 @@ def test_preallocated_heap(self):
48234823
@also_with_wasm2js
48244824
def test_fetch_to_memory(self):
48254825
# Test error reporting in the negative case when the file URL doesn't exist. (http 404)
4826-
self.btest_exit('fetch/to_memory.cpp',
4826+
self.btest_exit('fetch/test_fetch_to_memory.cpp',
48274827
args=['-sFETCH_DEBUG', '-sFETCH', '-DFILE_DOES_NOT_EXIST'])
48284828

48294829
# Test the positive case when the file URL exists. (http 200)
48304830
shutil.copyfile(test_file('gears.png'), 'gears.png')
48314831
for arg in [[], ['-sFETCH_SUPPORT_INDEXEDDB=0']]:
4832-
self.btest_exit('fetch/to_memory.cpp',
4832+
self.btest_exit('fetch/test_fetch_to_memory.cpp',
48334833
args=['-sFETCH_DEBUG', '-sFETCH'] + arg)
48344834

48354835
@parameterized({
@@ -4840,30 +4840,28 @@ def test_fetch_to_memory(self):
48404840
@requires_threads
48414841
def test_fetch_from_thread(self, args):
48424842
shutil.copyfile(test_file('gears.png'), 'gears.png')
4843-
self.btest_exit('fetch/from_thread.cpp',
4843+
self.btest_exit('fetch/test_fetch_from_thread.cpp',
48444844
args=args + ['-pthread', '-sPROXY_TO_PTHREAD', '-sFETCH_DEBUG', '-sFETCH', '-DFILE_DOES_NOT_EXIST'],
48454845
also_wasm2js=True)
48464846

48474847
@also_with_wasm2js
48484848
def test_fetch_to_indexdb(self):
48494849
shutil.copyfile(test_file('gears.png'), 'gears.png')
4850-
self.btest_exit('fetch/to_indexeddb.cpp',
4851-
args=['-sFETCH_DEBUG', '-sFETCH'])
4850+
self.btest_exit('fetch/test_fetch_to_indexeddb.cpp', args=['-sFETCH_DEBUG', '-sFETCH'])
48524851

48534852
# Tests emscripten_fetch() usage to persist an XHR into IndexedDB and subsequently load up from there.
48544853
@also_with_wasm2js
48554854
def test_fetch_cached_xhr(self):
48564855
shutil.copyfile(test_file('gears.png'), 'gears.png')
4857-
self.btest_exit('fetch/cached_xhr.cpp',
4858-
args=['-sFETCH_DEBUG', '-sFETCH'])
4856+
self.btest_exit('fetch/test_fetch_cached_xhr.cpp', args=['-sFETCH_DEBUG', '-sFETCH'])
48594857

48604858
# Tests that response headers get set on emscripten_fetch_t values.
48614859
@no_firefox('https://github.com/emscripten-core/emscripten/issues/16868')
48624860
@also_with_wasm2js
48634861
@requires_threads
48644862
def test_fetch_response_headers(self):
48654863
shutil.copyfile(test_file('gears.png'), 'gears.png')
4866-
self.btest_exit('fetch/response_headers.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD'])
4864+
self.btest_exit('fetch/test_fetch_response_headers.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD'])
48674865

48684866
# Test emscripten_fetch() usage to stream a XHR in to memory without storing the full file in memory
48694867
@also_with_wasm2js
@@ -4877,15 +4875,16 @@ def test_fetch_stream_file(self):
48774875
with open('largefile.txt', 'w') as f:
48784876
for _ in range(1024):
48794877
f.write(s)
4880-
self.btest_exit('fetch/stream_file.cpp',
4878+
self.btest_exit('fetch/test_fetch_stream_file.cpp',
48814879
args=['-sFETCH_DEBUG', '-sFETCH', '-sINITIAL_MEMORY=536870912'])
48824880

48834881
def test_fetch_headers_received(self):
4884-
self.btest_exit('fetch/headers_received.cpp', args=['-sFETCH_DEBUG', '-sFETCH'])
4882+
create_file('myfile.dat', 'hello world\n')
4883+
self.btest_exit('fetch/test_fetch_headers_received.c', args=['-sFETCH_DEBUG', '-sFETCH'])
48854884

48864885
def test_fetch_xhr_abort(self):
48874886
shutil.copyfile(test_file('gears.png'), 'gears.png')
4888-
self.btest_exit('fetch/xhr_abort.cpp', args=['-sFETCH_DEBUG', '-sFETCH'])
4887+
self.btest_exit('fetch/test_fetch_xhr_abort.cpp', args=['-sFETCH_DEBUG', '-sFETCH'])
48894888

48904889
# Tests emscripten_fetch() usage in synchronous mode when used from the main
48914890
# thread proxied to a Worker with -sPROXY_TO_PTHREAD option.
@@ -4894,7 +4893,7 @@ def test_fetch_xhr_abort(self):
48944893
@requires_threads
48954894
def test_fetch_sync_xhr(self):
48964895
shutil.copyfile(test_file('gears.png'), 'gears.png')
4897-
self.btest_exit('fetch/sync_xhr.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD'])
4896+
self.btest_exit('fetch/test_fetch_sync_xhr.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD'])
48984897

48994898
# Tests emscripten_fetch() usage when user passes none of the main 3 flags (append/replace/no_download).
49004899
# In that case, in append is implicitly understood.
@@ -4917,26 +4916,26 @@ def test_fetch_sync_xhr_in_wasm(self):
49174916
@requires_threads
49184917
def test_fetch_sync_xhr_in_proxy_to_worker(self):
49194918
shutil.copyfile(test_file('gears.png'), 'gears.png')
4920-
self.btest_exit('fetch/sync_xhr.cpp',
4919+
self.btest_exit('fetch/test_fetch_sync_xhr.cpp',
49214920
args=['-sFETCH_DEBUG', '-sFETCH', '--proxy-to-worker'])
49224921

49234922
# Tests waiting on EMSCRIPTEN_FETCH_WAITABLE request from a worker thread
49244923
@unittest.skip("emscripten_fetch_wait relies on an asm.js-based web worker")
49254924
@requires_threads
49264925
def test_fetch_sync_fetch_in_main_thread(self):
49274926
shutil.copyfile(test_file('gears.png'), 'gears.png')
4928-
self.btest_exit('fetch/sync_fetch_in_main_thread.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-pthread', '-sPROXY_TO_PTHREAD'])
4927+
self.btest_exit('fetch/test_fetch_sync_in_main_thread.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-pthread', '-sPROXY_TO_PTHREAD'])
49294928

49304929
@requires_threads
49314930
@disabled('https://github.com/emscripten-core/emscripten/issues/16746')
49324931
def test_fetch_idb_store(self):
4933-
self.btest_exit('fetch/idb_store.cpp', args=['-pthread', '-sFETCH', '-sWASM=0', '-sPROXY_TO_PTHREAD'])
4932+
self.btest_exit('fetch/test_fetch_idb_store.cpp', args=['-pthread', '-sFETCH', '-sPROXY_TO_PTHREAD'])
49344933

49354934
@requires_threads
49364935
@disabled('https://github.com/emscripten-core/emscripten/issues/16746')
49374936
def test_fetch_idb_delete(self):
49384937
shutil.copyfile(test_file('gears.png'), 'gears.png')
4939-
self.btest_exit('fetch/idb_delete.cpp', args=['-pthread', '-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-sPROXY_TO_PTHREAD'])
4938+
self.btest_exit('fetch/test_fetch_idb_delete.cpp', args=['-pthread', '-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-sPROXY_TO_PTHREAD'])
49404939

49414940
def test_fetch_post(self):
49424941
self.btest_exit('fetch/test_fetch_post.c', args=['-sFETCH'])

0 commit comments

Comments
 (0)