|  |  |  | @ -1,6 +1,4 @@ | 
			
		
	
		
			
				
					|  |  |  |  | import http.server | 
			
		
	
		
			
				
					|  |  |  |  | import multiprocessing | 
			
		
	
		
			
				
					|  |  |  |  | import queue | 
			
		
	
		
			
				
					|  |  |  |  | import random | 
			
		
	
		
			
				
					|  |  |  |  | import requests | 
			
		
	
		
			
				
					|  |  |  |  | import socket | 
			
		
	
	
		
			
				
					|  |  |  | @ -8,6 +6,7 @@ import time | 
			
		
	
		
			
				
					|  |  |  |  | from functools import wraps | 
			
		
	
		
			
				
					|  |  |  |  | from multiprocessing import Process | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | from common.timeout import Timeout | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | class EchoSocket(): | 
			
		
	
		
			
				
					|  |  |  |  |   def __init__(self, port): | 
			
		
	
	
		
			
				
					|  |  |  | @ -79,39 +78,25 @@ class HTTPRequestHandler(http.server.SimpleHTTPRequestHandler): | 
			
		
	
		
			
				
					|  |  |  |  |     self.end_headers() | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | def http_server(port_queue, **kwargs): | 
			
		
	
		
			
				
					|  |  |  |  |   while 1: | 
			
		
	
		
			
				
					|  |  |  |  |     try: | 
			
		
	
		
			
				
					|  |  |  |  |       port = random.randrange(40000, 50000) | 
			
		
	
		
			
				
					|  |  |  |  |       port_queue.put(port) | 
			
		
	
		
			
				
					|  |  |  |  |       http.server.test(**kwargs, port=port) | 
			
		
	
		
			
				
					|  |  |  |  |     except OSError as e: | 
			
		
	
		
			
				
					|  |  |  |  |       if e.errno == 98: | 
			
		
	
		
			
				
					|  |  |  |  |         continue | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | def with_http_server(func): | 
			
		
	
		
			
				
					|  |  |  |  |   @wraps(func) | 
			
		
	
		
			
				
					|  |  |  |  |   def inner(*args, **kwargs): | 
			
		
	
		
			
				
					|  |  |  |  |     port_queue = multiprocessing.Queue() | 
			
		
	
		
			
				
					|  |  |  |  |     with Timeout(2, 'HTTP Server did not start'): | 
			
		
	
		
			
				
					|  |  |  |  |       p = None | 
			
		
	
		
			
				
					|  |  |  |  |       host = '127.0.0.1' | 
			
		
	
		
			
				
					|  |  |  |  |     p = Process(target=http_server, | 
			
		
	
		
			
				
					|  |  |  |  |                 args=(port_queue,), | 
			
		
	
		
			
				
					|  |  |  |  |                 kwargs={ | 
			
		
	
		
			
				
					|  |  |  |  |                   'HandlerClass': HTTPRequestHandler, | 
			
		
	
		
			
				
					|  |  |  |  |                   'bind': host}) | 
			
		
	
		
			
				
					|  |  |  |  |       while p is None or p.exitcode is not None: | 
			
		
	
		
			
				
					|  |  |  |  |         port = random.randrange(40000, 50000) | 
			
		
	
		
			
				
					|  |  |  |  |         p = Process(target=http.server.test, | 
			
		
	
		
			
				
					|  |  |  |  |                     kwargs={'port': port, 'HandlerClass': HTTPRequestHandler, 'bind': host}) | 
			
		
	
		
			
				
					|  |  |  |  |         p.start() | 
			
		
	
		
			
				
					|  |  |  |  |     start = time.monotonic() | 
			
		
	
		
			
				
					|  |  |  |  |     port = None | 
			
		
	
		
			
				
					|  |  |  |  |     while 1: | 
			
		
	
		
			
				
					|  |  |  |  |       if time.monotonic() - start > 10: | 
			
		
	
		
			
				
					|  |  |  |  |         raise Exception('HTTP Server did not start') | 
			
		
	
		
			
				
					|  |  |  |  |         time.sleep(0.1) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     with Timeout(2): | 
			
		
	
		
			
				
					|  |  |  |  |       while True: | 
			
		
	
		
			
				
					|  |  |  |  |         try: | 
			
		
	
		
			
				
					|  |  |  |  |         port = port_queue.get(timeout=0.1) | 
			
		
	
		
			
				
					|  |  |  |  |           requests.put(f'http://{host}:{port}/qlog.bz2', data='') | 
			
		
	
		
			
				
					|  |  |  |  |           break | 
			
		
	
		
			
				
					|  |  |  |  |       except (requests.exceptions.ConnectionError, queue.Empty): | 
			
		
	
		
			
				
					|  |  |  |  |         except requests.exceptions.ConnectionError: | 
			
		
	
		
			
				
					|  |  |  |  |           time.sleep(0.1) | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     try: | 
			
		
	
	
		
			
				
					|  |  |  | 
 |