Added a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Braulio Valdivielso Martínez · committed by Braulio Valdivielso Martínez · parent 578b032 · Browse files · 2 files·+49 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+49 added−6 removed
- other:+49 added−6 removed
Provenance
- Commit
3eeea37- Tree
885b02f- Author
Braulio Valdivielso Martínez <brlvldvlsmrtnz@gmail.com>- Committer
Braulio Valdivielso Martínez <brlvldvlsmrtnz@gmail.com>- Parent
578b032
2 files changed+49 added−6 removed
modified:testserver/server.py+28 added-5 removed
1 #!/usr/bin/python2 3 import threading 4 import socket5+import select6 6-def consume_socket(sock, chunks=65536):7- while not sock.recv(chunks).endswith(b'\r\n\r\n'):8- pass7 8+def consume_socket_content(sock, chunks=65536, timeout=0.5):9+ content = ""10+ more_to_read = select.select([sock], [], [], timeout)[0]11 12+ while more_to_read:13+ new_content = sock.recv(chunks).decode("utf-8")14+15+ if len(new_content) == 0:16+ more_to_read = False # empty recv means the socket disconnected17+18+ else:19+ content += new_content 20+ # stop reading if no new data is received for a while 21+ more_to_read = select.select([sock], [], [], timeout)[0] 22+23+ return content24+25 class Server(threading.Thread):26 """ Dummy server using for unit testing """27 28 def __init__(self, handler, host='localhost', port=0, requests_to_handle=1, wait_to_close_event=None):29 threading.Thread.__init__(self)30+31 self.handler = handler32+ self.handler_results = []33+34 self.host = host35 self.port = port36 self.requests_to_handle = requests_to_handle37 1 unmodified line39 self.ready_event = threading.Event()40 self.stop_event = threading.Event()41 42 @classmethod26- def text_response_server(cls, text, **kwargs):43+ def text_response_server(cls, text, request_timeout=0.5, **kwargs):44 def text_response_handler(sock):45+ request_content = consume_socket_content(sock, timeout=request_timeout)46 sock.send(text.encode())47 48+ return request_content49+50+51 server = Server(text_response_handler, **kwargs)52 53 return server54 26 unmodified lines81 82 def _handle_requests_and_close_server(self, server_sock):83 for _ in range(self.requests_to_handle):84 sock = server_sock.accept()[0]64- self.handler(sock)85+ handler_result = self.handler(sock)86+87+ self.handler_results.append(handler_result)88 89 if self.wait_to_close_event:90 self.wait_to_close_event.wait()91 15 unmodified linesmodified:test_requests.py+21 added-1 removed
1777 unmodified lines1778 def test_server_closes(self):1779 with Server.basic_response_server() as (host, port):1780 sock = socket.socket()1781 sock.connect((host, port))1782+1783 sock.close()1784 1785 with pytest.raises(socket.error):1786 new_sock = socket.socket()10 unmodified lines1797 r = requests.get('http://{}:{}'.format(host, port))1798 1799 assert r.status_code == 2001800 assert r.text == 'roflol'1800- assert r.headers['Content-Length'] == '6'1801+ assert r.headers['Content-Length'] == '6' 1802 1803 def test_basic_response(self):1804 with Server.basic_response_server() as (host, port):1805 r = requests.get('http://{}:{}'.format(host, port))26 unmodified lines1832 # the (n+1)th request fails1833 with pytest.raises(requests.exceptions.ConnectionError):1834 r = requests.get(server_url)1835 1836+ def test_request_recovery(self):1837+ server = Server.basic_response_server(requests_to_handle=2)1838+ first_request = "put your hands up in the air"1839+ second_request = "put your hand down in the floor"1840 1841+ with server as address:1842+ sock1 = socket.socket()1843+ sock2 = socket.socket()1844+ 1845+ sock1.connect(address)1846+ sock1.send(first_request.encode())1847+ sock1.close()1848 1849+ sock2.connect(address)1850+ sock2.send(second_request.encode())1851+ sock2.close()1852+1853+ assert server.handler_results[0] == first_request1854+ assert server.handler_results[1] == second_request1855+1856+1857 if __name__ == '__main__':1858 unittest.main()