Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:16:34

0001 import signal
0002 import subprocess
0003 import time
0004 import os
0005 import urllib
0006 import urllib.request
0007 import urllib.error
0008 
0009 
0010 def test_pyrobird_serve_runs_and_responds():
0011     port = 5461
0012     proc = subprocess.Popen(
0013         ["pyrobird", "serve", "--port", str(port)],
0014         stdout=subprocess.PIPE,
0015         stderr=subprocess.STDOUT,
0016         env=os.environ.copy(),
0017         text=True  # output is string
0018     )
0019 
0020     time.sleep(3)
0021 
0022     page_served_ok = False
0023     status_code = -1
0024 
0025     for try_count in range(15):
0026         try:
0027             print(f"Making try: {try_count+1}")
0028             response = urllib.request.urlopen(f"http://127.0.0.1:{port}", timeout=2)
0029             status_code = response.getcode()
0030             if status_code == 200:
0031                 page_served_ok = True
0032                 print(f"Success!")
0033                 break
0034         except Exception as e:
0035             print(f"(warn) Error during request: {e}")
0036 
0037         time.sleep(1)
0038 
0039     # what is our server doing?
0040     if os.name == "nt":
0041         proc.terminate()
0042     else:
0043         os.kill(proc.pid, signal.SIGTERM)
0044 
0045     # What its output was?
0046     output, _ = proc.communicate(timeout=10)
0047     print(f"Server output:\n======================================\n{output}")
0048     # proc.wait(timeout=10)
0049     # poll_result = proc.poll()
0050     # if poll_result is not None:
0051     #     print(f"Server exited(!) with code: {poll_result}")
0052 
0053 
0054 
0055 
0056     # Now should we fail with error:
0057     if page_served_ok:
0058         print("Page served ok")
0059     else:
0060         print("ERROR: Page not served ok")
0061         raise Exception(f"Test failed!")
0062 
0063 
0064 if __name__ == "__main__":
0065     test_pyrobird_serve_runs_and_responds()
0066 
0067 
0068