Skip to content
Myr

Allow graceful interruption of testserver.Server

Before adoption: this commit came in with the history the project adopted, so no landing records it.

So that failing tests don't cause the server thread to hang indefinitely, waiting for connections that will never come. Rationale for suppressing error/traceback from interrupted _accept_connection in testserver.Server: https://gist.github.com/brettdh/b6e741227b2297f19d2118077f14dfa5 * Move server socket close to just before join This way it handles the no-connections, no-exceptions case as well as the exception case. If the server thread doesn't exit by itself within 5 seconds of the context manager exit, the accept will be interrupted. * Address feedback - pytest.raises rather than except:pass - Move socket create/bind back to run() - Timeout on accepting connections

authored by Brett Higgins · committed by Ian Cordasco · parent cd4e6b9 · Browse files · 2 files+52 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+52 added−6 removed

  • tests:+52 added−6 removed

2 files+52 added−6 removed

modified:tests/testserver/server.py+29 added-6 removed
58 unmodified lines
59 )6061 def run(self):62 try:63 sock = self._create_socket_and_bind()63 self.server_sock = self._create_socket_and_bind()64 # in case self.port = 065 self.port = sock.getsockname()[1]65 self.port = self.server_sock.getsockname()[1]66 self.ready_event.set()67 self._handle_requests(sock)67 self._handle_requests()6869 if self.wait_to_close_event:70 self.wait_to_close_event.wait(self.WAIT_EVENT_TIMEOUT)71 finally:72 self.ready_event.set() # just in case of exception73 sock.close()73 self._close_server_sock_ignore_errors()74 self.stop_event.set()7576 def _create_socket_and_bind(self):77 sock = socket.socket()78 sock.bind((self.host, self.port))79 sock.listen(0)80 return sock8182 def _close_server_sock_ignore_errors(self):83 try:84 self.server_sock.close()85 except IOError:86 pass8782 def _handle_requests(self, server_sock):88 def _handle_requests(self):89 for _ in range(self.requests_to_handle):84 sock = server_sock.accept()[0]90 sock = self._accept_connection()91 if not sock:92 break9394 handler_result = self.handler(sock)9596 self.handler_results.append(handler_result)9798 def _accept_connection(self):99 try:100 ready, _, _ = select.select([self.server_sock], [], [], self.WAIT_EVENT_TIMEOUT)101 if not ready:102 return None103104 return self.server_sock.accept()[0]105 except (select.error, socket.error):106 return None107108 def __enter__(self):109 self.start()110 self.ready_event.wait(self.WAIT_EVENT_TIMEOUT)111 return self.host, self.port
5 unmodified lines
117 if self.wait_to_close_event:118 # avoid server from waiting for event timeouts119 # if an exception is found in the main thread120 self.wait_to_close_event.set()121122 # ensure server thread doesn't get stuck waiting for connections123 self._close_server_sock_ignore_errors()124 self.join()125 return False # allow exceptions to propagate
modified:tests/test_testserver.py+23 added
133 unmodified lines
134 sock.sendall(data)135 sock.close()136 137 assert server.handler_results[0] == data138139 def test_server_finishes_on_error(self):140 """the server thread exits even if an exception exits the context manager"""141 server = Server.basic_response_server()142 with pytest.raises(Exception):143 with server:144 raise Exception()145146 assert len(server.handler_results) == 0147148 # if the server thread fails to finish, the test suite will hang149 # and get killed by the jenkins timeout.150151 def test_server_finishes_when_no_connections(self):152 """the server thread exits even if there are no connections"""153 server = Server.basic_response_server()154 with server:155 pass156157 assert len(server.handler_results) == 0158159 # if the server thread fails to finish, the test suite will hang160 # and get killed by the jenkins timeout.