|
3 | 3 | import http
|
4 | 4 | import logging
|
5 | 5 | import socket
|
6 |
| -import threading |
| 6 | +import time |
7 | 7 | import unittest
|
8 | 8 |
|
9 | 9 | from websockets.exceptions import (
|
@@ -289,50 +289,19 @@ def test_timeout_during_handshake(self):
|
289 | 289 | def test_connection_closed_during_handshake(self):
|
290 | 290 | """Server reads EOF before receiving handshake request from client."""
|
291 | 291 | with run_server() as server:
|
292 |
| - # Patch handler to record a reference to the thread running it. |
293 |
| - server_thread = None |
294 |
| - conn_received = threading.Event() |
295 |
| - original_handler = server.handler |
296 |
| - |
297 |
| - def handler(sock, addr): |
298 |
| - nonlocal server_thread |
299 |
| - server_thread = threading.current_thread() |
300 |
| - nonlocal conn_received |
301 |
| - conn_received.set() |
302 |
| - original_handler(sock, addr) |
303 |
| - |
304 |
| - server.handler = handler |
305 |
| - |
306 | 292 | with socket.create_connection(server.socket.getsockname()):
|
307 | 293 | # Wait for the server to receive the connection, then close it.
|
308 |
| - conn_received.wait() |
309 |
| - |
310 |
| - # Wait for the server thread to terminate. |
311 |
| - server_thread.join() |
| 294 | + time.sleep(MS) |
312 | 295 |
|
313 | 296 | def test_junk_handshake(self):
|
314 | 297 | """Server closes the connection when receiving non-HTTP request from client."""
|
315 | 298 | with self.assertLogs("websockets.server", logging.ERROR) as logs:
|
316 | 299 | with run_server() as server:
|
317 |
| - # Patch handler to record a reference to the thread running it. |
318 |
| - server_thread = None |
319 |
| - original_handler = server.handler |
320 |
| - |
321 |
| - def handler(sock, addr): |
322 |
| - nonlocal server_thread |
323 |
| - server_thread = threading.current_thread() |
324 |
| - original_handler(sock, addr) |
325 |
| - |
326 |
| - server.handler = handler |
327 |
| - |
328 | 300 | with socket.create_connection(server.socket.getsockname()) as sock:
|
329 | 301 | sock.send(b"HELO relay.invalid\r\n")
|
330 | 302 | # Wait for the server to close the connection.
|
331 | 303 | self.assertEqual(sock.recv(4096), b"")
|
332 | 304 |
|
333 |
| - # Wait for the server thread to terminate. |
334 |
| - server_thread.join() |
335 |
| - |
336 | 305 | self.assertEqual(
|
337 | 306 | [record.getMessage() for record in logs.records],
|
338 | 307 | ["opening handshake failed"],
|
@@ -360,26 +329,9 @@ def test_timeout_during_tls_handshake(self):
|
360 | 329 | def test_connection_closed_during_tls_handshake(self):
|
361 | 330 | """Server reads EOF before receiving TLS handshake request from client."""
|
362 | 331 | with run_server(ssl=SERVER_CONTEXT) as server:
|
363 |
| - # Patch handler to record a reference to the thread running it. |
364 |
| - server_thread = None |
365 |
| - conn_received = threading.Event() |
366 |
| - original_handler = server.handler |
367 |
| - |
368 |
| - def handler(sock, addr): |
369 |
| - nonlocal server_thread |
370 |
| - server_thread = threading.current_thread() |
371 |
| - nonlocal conn_received |
372 |
| - conn_received.set() |
373 |
| - original_handler(sock, addr) |
374 |
| - |
375 |
| - server.handler = handler |
376 |
| - |
377 | 332 | with socket.create_connection(server.socket.getsockname()):
|
378 | 333 | # Wait for the server to receive the connection, then close it.
|
379 |
| - conn_received.wait() |
380 |
| - |
381 |
| - # Wait for the server thread to terminate. |
382 |
| - server_thread.join() |
| 334 | + time.sleep(MS) |
383 | 335 |
|
384 | 336 |
|
385 | 337 | @unittest.skipUnless(hasattr(socket, "AF_UNIX"), "this test requires Unix sockets")
|
|
0 commit comments