Skip to content

Commit 7504e57

Browse files
authored
Use btest_exit in tests/test_sockets.py. NFC (#16444)
1 parent 9b73e68 commit 7504e57

8 files changed

+58
-76
lines changed

tests/sdl2_net_client.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ void finish(int result) {
4646
SDLNet_Quit();
4747
}
4848
#ifdef __EMSCRIPTEN__
49-
REPORT_RESULT(result);
50-
emscripten_force_exit(result);
51-
#else
52-
exit(result);
49+
emscripten_cancel_main_loop();
5350
#endif
51+
exit(result);
5452
}
5553

5654
char *msgs[] = {

tests/sockets/test_enet_client.c

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

8+
#include <assert.h>
89
#include <stdio.h>
910
#include <string.h>
1011
#include <enet/enet.h>
@@ -40,12 +41,8 @@ void main_loop() {
4041
(char*)event.peer -> data,
4142
event.channelID);
4243

43-
int result = strcmp("packetfoo", (char*)event.packet->data);
44-
#ifdef __EMSCRIPTEN__
45-
REPORT_RESULT(result);
46-
#else
44+
assert(strcmp("packetfoo", (char*)event.packet->data) == 0);
4745
exit(EXIT_SUCCESS);
48-
#endif
4946

5047
/* Clean up the packet now that we're done using it. */
5148
enet_packet_destroy (event.packet);

tests/sockets/test_sockets_echo_client.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,19 @@ int echo_read;
4545
int echo_wrote;
4646

4747
void finish(int result) {
48+
printf("finish: %d\n", result);
4849
if (server.fd) {
4950
close(server.fd);
5051
server.fd = 0;
5152
}
5253
#ifdef __EMSCRIPTEN__
53-
#ifdef REPORT_RESULT
54-
REPORT_RESULT(result);
55-
#endif
54+
#if TEST_ASYNC
5655
emscripten_force_exit(result);
5756
#else
58-
exit(result);
57+
emscripten_cancel_main_loop();
5958
#endif
59+
#endif
60+
exit(result);
6061
}
6162

6263
void main_loop() {

tests/sockets/test_sockets_select_server_closes_connection_client_rw.c

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ msg_t writemsg;
3232
void finish(int result) {
3333
close(sockfd);
3434
#ifdef __EMSCRIPTEN__
35-
REPORT_RESULT(result);
36-
emscripten_force_exit(result);
37-
#else
38-
exit(result);
35+
emscripten_cancel_main_loop();
3936
#endif
37+
exit(result);
4038
}
4139

4240
void main_loop() {
@@ -45,12 +43,12 @@ void main_loop() {
4543
static int writePos = 0;
4644
int selectRes;
4745
ssize_t transferAmount;
48-
fd_set sett;
49-
46+
fd_set sett;
47+
5048
switch (state) {
5149
case 0:
5250
// writing 10 bytes to the server
53-
51+
5452
// since the socket in the read file descriptors has no available data,
5553
// select should tell us 0 handles are ready
5654
FD_ZERO(&sett);
@@ -60,7 +58,7 @@ void main_loop() {
6058
printf("case 0: read select != 0 (%d)\n", selectRes);
6159
finish(EXIT_FAILURE);
6260
}
63-
61+
6462
// the socket in the write file descriptors has to result in either a 0 or 1
6563
// the connection either is setting up or is established and writing is possible
6664
FD_ZERO(&sett);
@@ -72,17 +70,17 @@ void main_loop() {
7270
} else if (selectRes == 0) {
7371
return;
7472
}
75-
73+
7674
// send a single byte
7775
transferAmount = do_msg_write(sockfd, &writemsg, writePos, 1, NULL, 0);
7876
if (transferAmount != -1) writePos += transferAmount;
79-
77+
8078
// after 10 bytes switch to next state
8179
if (writePos >= writemsg.length) {
8280
state = 1;
8381
}
8482
break;
85-
83+
8684
case 1:
8785
// wait until we can read one byte to make sure the server
8886
// has sent the data and then closed the connection
@@ -104,13 +102,13 @@ void main_loop() {
104102
} else if (transferAmount != -1) {
105103
readPos += transferAmount;
106104
}
107-
105+
108106
// if successfully reading 1 byte, switch to next state
109107
if (readPos >= 1) {
110108
state = 2;
111109
}
112110
break;
113-
111+
114112
case 2:
115113
// calling select with the socket in the write file descriptors should
116114
// succeed, but the socket should not set in the set.
@@ -122,7 +120,7 @@ void main_loop() {
122120
finish(EXIT_FAILURE);
123121
}
124122

125-
// calling select with the socket in the read file descriptors
123+
// calling select with the socket in the read file descriptors
126124
// has to succeed because there is still data in the inQueue
127125
FD_ZERO(&sett);
128126
FD_SET(sockfd, &sett);
@@ -133,7 +131,7 @@ void main_loop() {
133131
} else if (selectRes == 0) {
134132
return;
135133
}
136-
134+
137135
// read a single byte
138136
transferAmount = do_msg_read(sockfd, &readmsg, readPos, 1, NULL, NULL);
139137
if (transferAmount == 0) {
@@ -142,15 +140,15 @@ void main_loop() {
142140
} else if (transferAmount != -1) {
143141
readPos += transferAmount;
144142
}
145-
143+
146144
// with 10 bytes read the inQueue is empty => switch state
147145
if (readPos >= readmsg.length) {
148146
state = 3;
149147
}
150148
break;
151-
149+
152150
case 3:
153-
// calling select with the socket in the read file descriptors
151+
// calling select with the socket in the read file descriptors
154152
// should succeed
155153
FD_ZERO(&sett);
156154
FD_SET(sockfd, &sett);
@@ -167,30 +165,30 @@ void main_loop() {
167165
printf("case 3: read != 0\n");
168166
finish(EXIT_FAILURE);
169167
}
170-
171-
// report back success, the 266 is just an arbitrary value without
168+
169+
// report back success, the 266 is just an arbitrary value without
172170
// deeper meaning
173-
finish(266);
171+
finish(0);
174172
break;
175-
173+
176174
default:
177175
printf("Impossible state!\n");
178176
finish(EXIT_FAILURE);
179177
break;
180178
}
181-
179+
182180
return;
183181
}
184182

185183
// This test checks for an intended asymmetry in the behavior of the select function.
186-
// Scenario: the client sends data to the server. After 10 received bytes the
184+
// Scenario: the client sends data to the server. After 10 received bytes the
187185
// server sends 10 bytes on its own and immediately afterwards closes the connection.
188186
// This mimics a typical connect-request-response-disconnect situation.
189-
// After the server closed the connection select calls with the socket in the write file
190-
// descriptors have to fail as the tcp connection is already down and there is no way
191-
// anymore to send data.
192-
// Select calls with the socket in the read file descriptor list still have to succeed
193-
// as there are still 10 bytes to read from the inQueue. So, for the same socket the
187+
// After the server closed the connection select calls with the socket in the write file
188+
// descriptors have to fail as the tcp connection is already down and there is no way
189+
// anymore to send data.
190+
// Select calls with the socket in the read file descriptor list still have to succeed
191+
// as there are still 10 bytes to read from the inQueue. So, for the same socket the
194192
// select call behaves differently depending on whether the socket is listed in the
195193
// read or write file descriptors.
196194
int main() {

tests/sockets/test_sockets_select_server_down_client.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ int sockfd = -1;
2828
void finish(int result) {
2929
close(sockfd);
3030
#ifdef __EMSCRIPTEN__
31-
REPORT_RESULT(result);
32-
emscripten_force_exit(result);
33-
#else
34-
exit(result);
31+
emscripten_cancel_main_loop();
3532
#endif
33+
exit(result);
3634
}
3735

3836
void iter() {
@@ -61,7 +59,7 @@ void iter() {
6159
finish(EXIT_FAILURE);
6260
} else if (!n) {
6361
perror("Connection to websocket server failed as expected.");
64-
finish(266);
62+
finish(0);
6563
}
6664
}
6765
}

tests/test_sockets.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ def test_sockets_echo(self, extra_args=[]):
189189

190190
for harness, datagram in harnesses:
191191
with harness:
192-
self.btest(test_file('sockets/test_sockets_echo_client.c'), expected='0', args=['-DSOCKK=%d' % harness.listen_port, '-DTEST_DGRAM=%d' % datagram, sockets_include])
192+
self.btest_exit(test_file('sockets/test_sockets_echo_client.c'), args=['-DSOCKK=%d' % harness.listen_port, '-DTEST_DGRAM=%d' % datagram, sockets_include])
193193

194194
def test_sockets_echo_pthreads(self, extra_args=[]):
195195
self.test_sockets_echo(['-sUSE_PTHREADS', '-sPROXY_TO_PTHREAD'])
196196

197197
def test_sdl2_sockets_echo(self):
198198
harness = CompiledServerHarness('sdl2_net_server.c', ['-sUSE_SDL=2', '-sUSE_SDL_NET=2'], 49164)
199199
with harness:
200-
self.btest('sdl2_net_client.c', expected='0', args=['-sUSE_SDL=2', '-sUSE_SDL_NET=2', '-DSOCKK=%d' % harness.listen_port])
200+
self.btest_exit('sdl2_net_client.c', args=['-sUSE_SDL=2', '-sUSE_SDL_NET=2', '-DSOCKK=%d' % harness.listen_port])
201201

202202
def test_sockets_async_echo(self):
203203
sockets_include = '-I' + test_file('sockets')
@@ -216,11 +216,12 @@ def test_sockets_async_echo(self):
216216
for harness, datagram in harnesses:
217217
print('harness:', harness)
218218
with harness:
219-
self.btest(test_file('sockets/test_sockets_echo_client.c'), expected='0', args=['-DSOCKK=%d' % harness.listen_port, '-DTEST_DGRAM=%d' % datagram, '-DTEST_ASYNC=1', sockets_include])
219+
self.btest_exit(test_file('sockets/test_sockets_echo_client.c'), args=['-DSOCKK=%d' % harness.listen_port, '-DTEST_DGRAM=%d' % datagram, '-DTEST_ASYNC=1', sockets_include])
220+
return
220221

221222
# Deliberately attempt a connection on a port that will fail to test the error callback and getsockopt
222223
print('expect fail')
223-
self.btest(test_file('sockets/test_sockets_echo_client.c'), expected='0', args=['-DSOCKK=49169', '-DTEST_ASYNC=1', sockets_include])
224+
self.btest_exit(test_file('sockets/test_sockets_echo_client.c'), args=['-DSOCKK=49169', '-DTEST_ASYNC=1', sockets_include])
224225

225226
def test_sockets_echo_bigdata(self):
226227
sockets_include = '-I' + test_file('sockets')
@@ -231,9 +232,8 @@ def test_sockets_echo_bigdata(self):
231232
message += str(chr(ord('a') + (i % 26)))
232233

233234
# re-write the client test with this literal (it's too big to pass via command line)
234-
input_filename = test_file('sockets/test_sockets_echo_client.c')
235-
input = read_file(input_filename)
236-
create_file('test_sockets_echo_bigdata.c', input.replace('#define MESSAGE "pingtothepong"', '#define MESSAGE "%s"' % message))
235+
src = read_file(test_file('sockets/test_sockets_echo_client.c'))
236+
create_file('test_sockets_echo_bigdata.c', src.replace('#define MESSAGE "pingtothepong"', '#define MESSAGE "%s"' % message))
237237

238238
harnesses = [
239239
(CompiledServerHarness(test_file('sockets/test_sockets_echo_server.c'), [sockets_include, '-DTEST_DGRAM=0'], 49172), 0),
@@ -245,7 +245,7 @@ def test_sockets_echo_bigdata(self):
245245

246246
for harness, datagram in harnesses:
247247
with harness:
248-
self.btest('test_sockets_echo_bigdata.c', expected='0', args=[sockets_include, '-DSOCKK=%d' % harness.listen_port, '-DTEST_DGRAM=%d' % datagram])
248+
self.btest_exit('test_sockets_echo_bigdata.c', args=[sockets_include, '-DSOCKK=%d' % harness.listen_port, '-DTEST_DGRAM=%d' % datagram])
249249

250250
@no_windows('This test is Unix-specific.')
251251
def test_sockets_partial(self):
@@ -263,7 +263,7 @@ def test_sockets_select_server_down(self):
263263
CompiledServerHarness(test_file('sockets/test_sockets_select_server_down_server.c'), [], 49191)
264264
]:
265265
with harness:
266-
self.btest(test_file('sockets/test_sockets_select_server_down_client.c'), expected='266', args=['-DSOCKK=%d' % harness.listen_port])
266+
self.btest_exit(test_file('sockets/test_sockets_select_server_down_client.c'), args=['-DSOCKK=%d' % harness.listen_port])
267267

268268
@no_windows('This test is Unix-specific.')
269269
def test_sockets_select_server_closes_connection_rw(self):
@@ -274,7 +274,7 @@ def test_sockets_select_server_closes_connection_rw(self):
274274
CompiledServerHarness(test_file('sockets/test_sockets_echo_server.c'), [sockets_include, '-DCLOSE_CLIENT_AFTER_ECHO'], 49201)
275275
]:
276276
with harness:
277-
self.btest(test_file('sockets/test_sockets_select_server_closes_connection_client_rw.c'), expected='266', args=[sockets_include, '-DSOCKK=%d' % harness.listen_port])
277+
self.btest_exit(test_file('sockets/test_sockets_select_server_closes_connection_client_rw.c'), args=[sockets_include, '-DSOCKK=%d' % harness.listen_port])
278278

279279
@no_windows('This test uses Unix-specific build architecture.')
280280
def test_enet(self):
@@ -290,7 +290,7 @@ def test_enet(self):
290290
CompiledServerHarness(test_file('sockets/test_enet_server.c'), enet, 49210)
291291
]:
292292
with harness:
293-
self.btest(test_file('sockets/test_enet_client.c'), expected='0', args=enet + ['-DSOCKK=%d' % harness.listen_port])
293+
self.btest_exit(test_file('sockets/test_enet_client.c'), args=enet + ['-DSOCKK=%d' % harness.listen_port])
294294

295295
def test_nodejs_sockets_echo(self):
296296
# This test checks that sockets work when the client code is run in Node.js
@@ -354,7 +354,7 @@ def test_nodejs_sockets_echo(self):
354354
# N.B. running this test requires 'npm install ws' in Emscripten root directory
355355
def test_websocket_send(self):
356356
with NodeJsWebSocketEchoServerProcess():
357-
self.btest(test_file('websocket/test_websocket_send.c'), expected='101', args=['-lwebsocket', '-sNO_EXIT_RUNTIME', '-sWEBSOCKET_DEBUG'])
357+
self.btest_exit(test_file('websocket/test_websocket_send.c'), args=['-lwebsocket', '-sNO_EXIT_RUNTIME', '-sWEBSOCKET_DEBUG'])
358358

359359
# Test that native POSIX sockets API can be used by proxying calls to an intermediate WebSockets -> POSIX sockets bridge server
360360
def test_posix_proxy_sockets(self):
@@ -369,4 +369,4 @@ def test_posix_proxy_sockets(self):
369369
with BackgroundServerProcess([proxy_server, '8080']):
370370
with PythonTcpEchoServerProcess('7777'):
371371
# Build and run the TCP echo client program with Emscripten
372-
self.btest(test_file('websocket/tcp_echo_client.cpp'), expected='101', args=['-lwebsocket', '-sPROXY_POSIX_SOCKETS', '-sUSE_PTHREADS', '-sPROXY_TO_PTHREAD'])
372+
self.btest_exit(test_file('websocket/tcp_echo_client.cpp'), args=['-lwebsocket', '-sPROXY_POSIX_SOCKETS', '-sUSE_PTHREADS', '-sPROXY_TO_PTHREAD'])

tests/websocket/tcp_echo_client.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ int main(int argc , char *argv[])
7979
if (sock == -1)
8080
{
8181
printf("Could not create socket");
82-
exit(1);
82+
return 1;
8383
}
8484
printf("Socket created: %d\n", sock);
8585

@@ -116,8 +116,5 @@ int main(int argc , char *argv[])
116116
}
117117

118118
close(sock);
119-
#ifdef REPORT_RESULT
120-
REPORT_RESULT(101);
121-
#endif
122119
return 0;
123120
}

tests/websocket/test_websocket_send.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,21 @@ EM_BOOL WebSocketMessage(int eventType, const EmscriptenWebSocketMessageEvent *e
3535
if (e->isText)
3636
{
3737
printf("text data: \"%s\"\n", e->data);
38-
#ifdef REPORT_RESULT
39-
if (!!strcmp((const char*)e->data, "hello on the other side")) REPORT_RESULT(-1);
40-
passed += 1;
41-
#endif
38+
assert(strcmp((const char*)e->data, "hello on the other side") == 0);
4239
}
4340
else
4441
{
4542
printf("binary data:");
4643
for(int i = 0; i < e->numBytes; ++i)
4744
{
4845
printf(" %02X", e->data[i]);
49-
#ifdef REPORT_RESULT
50-
if (e->data[i] != i) REPORT_RESULT(-2);
51-
#endif
46+
assert(e->data[i] == i);
5247
}
5348
printf("\n");
54-
passed += 100;
5549

5650
emscripten_websocket_close(e->socket, 0, 0);
5751
emscripten_websocket_delete(e->socket);
58-
#ifdef REPORT_RESULT
59-
printf("%d\n", passed);
60-
REPORT_RESULT(passed);
61-
#endif
52+
emscripten_force_exit(0);
6253
}
6354
return 0;
6455
}
@@ -123,4 +114,6 @@ int main()
123114
emscripten_websocket_set_onclose_callback(socket, (void*)43, WebSocketClose);
124115
emscripten_websocket_set_onerror_callback(socket, (void*)44, WebSocketError);
125116
emscripten_websocket_set_onmessage_callback(socket, (void*)45, WebSocketMessage);
117+
emscripten_exit_with_live_runtime();
118+
return 0;
126119
}

0 commit comments

Comments
 (0)