Skip to content

Commit b2fa2bd

Browse files
authored
Minor refactoring/cleanup of tests/test_sockets.py. NFC (#16534)
I have a followup that paramaterizes these tests so that there is just one harness used per test, and this precursor is mostly to make that change simpler and easier to review.
1 parent 0e2b95f commit b2fa2bd

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

tests/test_sockets.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
def clean_processes(processes):
2727
for p in processes:
28-
if (not hasattr(p, 'exitcode') or p.exitcode is None) and (not hasattr(p, 'returncode') or p.returncode is None):
28+
if getattr(p, 'exitcode', None) is None and getattr(p, 'returncode', None) is None:
2929
# ask nicely (to try and catch the children)
3030
try:
3131
p.terminate() # SIGTERM
@@ -53,7 +53,9 @@ def __enter__(self):
5353
# NOTE empty filename support is a hack to support
5454
# the current test_enet
5555
if self.filename:
56-
run_process([CLANG_CC, test_file(self.filename), '-o', 'server', '-DSOCKK=%d' % self.target_port] + clang_native.get_clang_native_args() + self.args, env=clang_native.get_clang_native_env())
56+
cmd = [CLANG_CC, test_file(self.filename), '-o', 'server', '-DSOCKK=%d' % self.target_port] + clang_native.get_clang_native_args() + self.args
57+
print(cmd)
58+
run_process(cmd, env=clang_native.get_clang_native_env())
5759
process = Popen([os.path.abspath('server')])
5860
self.processes.append(process)
5961

@@ -81,6 +83,7 @@ def __enter__(self):
8183
raise Exception('[Websockify failed to start up in a timely manner]')
8284

8385
print('[Websockify on process %s]' % str(self.processes[-2:]))
86+
return self
8487

8588
def __exit__(self, *args, **kwargs):
8689
# try to kill the websockify proxy gracefully
@@ -114,6 +117,7 @@ def __enter__(self):
114117

115118
process = Popen(config.NODE_JS + ['server.js'])
116119
self.processes.append(process)
120+
return self
117121

118122
def __exit__(self, *args, **kwargs):
119123
# clean up any processes we started
@@ -133,6 +137,7 @@ def __enter__(self):
133137
print('Running background server: ' + str(self.args))
134138
process = Popen(self.args)
135139
self.processes.append(process)
140+
return self
136141

137142
def __exit__(self, *args, **kwargs):
138143
clean_processes(self.processes)
@@ -187,8 +192,7 @@ def test_sockets_echo_pthreads(self):
187192
self.test_sockets_echo(['-sUSE_PTHREADS', '-sPROXY_TO_PTHREAD'])
188193

189194
def test_sdl2_sockets_echo(self):
190-
harness = CompiledServerHarness('sdl2_net_server.c', ['-sUSE_SDL=2', '-sUSE_SDL_NET=2'], 49164)
191-
with harness:
195+
with CompiledServerHarness('sdl2_net_server.c', ['-sUSE_SDL=2', '-sUSE_SDL_NET=2'], 49164) as harness:
192196
self.btest_exit('sdl2_net_client.c', args=['-sUSE_SDL=2', '-sUSE_SDL_NET=2', '-DSOCKK=%d' % harness.listen_port])
193197

194198
def test_sockets_async_echo(self):
@@ -274,11 +278,8 @@ def test_enet(self):
274278
self.run_process([path_from_root('emmake'), 'make'])
275279
enet = [self.in_dir('enet', '.libs', 'libenet.a'), '-I' + self.in_dir('enet', 'include')]
276280

277-
for harness in [
278-
CompiledServerHarness(test_file('sockets/test_enet_server.c'), enet, 49210)
279-
]:
280-
with harness:
281-
self.btest_exit(test_file('sockets/test_enet_client.c'), args=enet + ['-DSOCKK=%d' % harness.listen_port])
281+
with CompiledServerHarness(test_file('sockets/test_enet_server.c'), enet, 49210) as harness:
282+
self.btest_exit(test_file('sockets/test_enet_client.c'), args=enet + ['-DSOCKK=%d' % harness.listen_port])
282283

283284
def test_nodejs_sockets_echo(self):
284285
# This test checks that sockets work when the client code is run in Node.js
@@ -304,15 +305,12 @@ def test_nodejs_sockets_echo(self):
304305
# server because as long as the subprotocol list contains binary it will configure itself to accept binary
305306
# This test also checks that the connect url contains the correct subprotocols.
306307
print("\nTesting compile time WebSocket configuration.\n")
307-
for harness in [
308-
WebsockifyServerHarness(test_file('sockets/test_sockets_echo_server.c'), [], 59166)
309-
]:
310-
with harness:
311-
self.run_process([EMCC, '-Werror', test_file('sockets/test_sockets_echo_client.c'), '-o', 'client.js', '-sSOCKET_DEBUG', '-sWEBSOCKET_SUBPROTOCOL="base64, binary"', '-DSOCKK=59166'])
308+
with WebsockifyServerHarness(test_file('sockets/test_sockets_echo_server.c'), [], 59166):
309+
self.run_process([EMCC, '-Werror', test_file('sockets/test_sockets_echo_client.c'), '-o', 'client.js', '-sSOCKET_DEBUG', '-sWEBSOCKET_SUBPROTOCOL="base64, binary"', '-DSOCKK=59166'])
312310

313-
out = self.run_js('client.js')
314-
self.assertContained('do_msg_read: read 14 bytes', out)
315-
self.assertContained(['connect: ws://127.0.0.1:59166, base64,binary', 'connect: ws://127.0.0.1:59166/, base64,binary'], out)
311+
out = self.run_js('client.js')
312+
self.assertContained('do_msg_read: read 14 bytes', out)
313+
self.assertContained(['connect: ws://127.0.0.1:59166, base64,binary', 'connect: ws://127.0.0.1:59166/, base64,binary'], out)
316314

317315
# Test against a Websockified server with runtime WebSocket configuration. We specify both url and subprotocol.
318316
# In this test we have *deliberately* used the wrong port '-DSOCKK=12345' to configure the echo_client.c, so
@@ -326,15 +324,12 @@ def test_nodejs_sockets_echo(self):
326324
}
327325
};
328326
''')
329-
for harness in [
330-
WebsockifyServerHarness(test_file('sockets/test_sockets_echo_server.c'), [], 59168)
331-
]:
332-
with harness:
333-
self.run_process([EMCC, '-Werror', test_file('sockets/test_sockets_echo_client.c'), '-o', 'client.js', '--pre-js=websocket_pre.js', '-sSOCKET_DEBUG', '-DSOCKK=12345'])
334-
335-
out = self.run_js('client.js')
336-
self.assertContained('do_msg_read: read 14 bytes', out)
337-
self.assertContained('connect: ws://localhost:59168/testA/testB, text,base64,binary', out)
327+
with WebsockifyServerHarness(test_file('sockets/test_sockets_echo_server.c'), [], 59168) as harness:
328+
self.run_process([EMCC, '-Werror', test_file('sockets/test_sockets_echo_client.c'), '-o', 'client.js', '--pre-js=websocket_pre.js', '-sSOCKET_DEBUG', '-DSOCKK=12345'])
329+
330+
out = self.run_js('client.js')
331+
self.assertContained('do_msg_read: read 14 bytes', out)
332+
self.assertContained('connect: ws://localhost:59168/testA/testB, text,base64,binary', out)
338333

339334
# Test Emscripten WebSockets API to send and receive text and binary messages against an echo server.
340335
# N.B. running this test requires 'npm install ws' in Emscripten root directory

0 commit comments

Comments
 (0)