1212from subprocess import Popen
1313from typing import List
1414
15- if __name__ == '__main__' :
16- raise Exception ('do not run this file directly; do something like: test/runner sockets' )
17-
1815import clang_native
1916import common
2017from common import BrowserCore , no_windows , create_file , test_file , read_file
@@ -36,11 +33,24 @@ def clean_processes(processes):
3633 time .sleep (1 )
3734 # send a forcible kill immediately afterwards. If the process did not die before, this should clean it.
3835 try :
39- p .terminate () # SIGKILL
36+ p .kill () # SIGKILL
4037 except OSError :
4138 pass
4239
4340
41+ def run_websockify (listen_port , target_port , run_once ):
42+ import websockify # type: ignore
43+ print ('running websockify on %d, forward to tcp %d' % (listen_port , target_port ), file = sys .stderr )
44+ # source_is_ipv6=True here signals to websockify that it should prefer ipv6 address when
45+ # resolving host names. This matches what the node `ws` module does and means that `localhost`
46+ # resolves to `::1` on IPv6 systems.
47+ wsp = websockify .WebSocketProxy (verbose = True , source_is_ipv6 = True , listen_port = listen_port , target_host = "127.0.0.1" ,
48+ target_port = target_port , run_once = run_once )
49+ proc = multiprocessing .Process (target = wsp .start_server )
50+ proc .start ()
51+ return proc
52+
53+
4454class WebsockifyServerHarness ():
4555 def __init__ (self , filename , args , listen_port , do_server_check = True ):
4656 self .processes = []
@@ -55,22 +65,15 @@ def __enter__(self):
5565 # NOTE empty filename support is a hack to support
5666 # the current test_enet
5767 if self .filename :
58- cmd = [CLANG_CC , test_file (self .filename ), '-o' , 'server' , '-DSOCKK=%d' % self .target_port ] + clang_native .get_clang_native_args () + self .args
68+ server_name = os .path .splitext (os .path .basename (self .filename ))[0 ]
69+ cmd = [CLANG_CC , test_file (self .filename ), '-o' , server_name , '-DSOCKK=%d' % self .target_port ] + clang_native .get_clang_native_args () + self .args
5970 print (cmd )
6071 run_process (cmd , env = clang_native .get_clang_native_env ())
61- process = Popen ([os .path .abspath ('server' )])
72+ process = Popen ([os .path .abspath (server_name )])
6273 self .processes .append (process )
6374
64- import websockify # type: ignore
65-
6675 # start the websocket proxy
67- print ('running websockify on %d, forward to tcp %d' % (self .listen_port , self .target_port ), file = sys .stderr )
68- # source_is_ipv6=True here signals to websockify that it should prefer ipv6 address when
69- # resolving host names. This matches what the node `ws` module does and means that `localhost`
70- # resolves to `::1` on IPv6 systems.
71- wsp = websockify .WebSocketProxy (verbose = True , source_is_ipv6 = True , listen_port = self .listen_port , target_host = "127.0.0.1" , target_port = self .target_port , run_once = True )
72- self .websockify = multiprocessing .Process (target = wsp .start_server )
73- self .websockify .start ()
76+ self .websockify = run_websockify (self .listen_port , self .target_port , run_once = True )
7477 self .processes .append (self .websockify )
7578 # Make sure both the actual server and the websocket proxy are running
7679 for _ in range (10 ):
@@ -370,3 +373,7 @@ def setUp(self):
370373 self .set_setting ('MEMORY64' )
371374 self .emcc_args .append ('-Wno-experimental' )
372375 self .require_wasm64 ()
376+
377+
378+ if __name__ == '__main__' :
379+ run_websockify (int (sys .argv [1 ]), int (sys .argv [2 ]), run_once = False )
0 commit comments