|
| 1 | +from utils import session, client_session, server |
| 2 | +from time import sleep |
| 3 | + |
| 4 | +import websocket |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +def test_send_close(client_session): |
| 9 | + """ |
| 10 | + Ensure client stops receiving data once we send_close (socket is still open) |
| 11 | + """ |
| 12 | + client, server = client_session |
| 13 | + assert client.received_messages == [] |
| 14 | + |
| 15 | + server.send_message_to_all("test1") |
| 16 | + sleep(0.5) |
| 17 | + assert client.received_messages == ["test1"] |
| 18 | + |
| 19 | + # After CLOSE, client should not be receiving any messages |
| 20 | + server.clients[-1]["handler"].send_close() |
| 21 | + sleep(0.5) |
| 22 | + server.send_message_to_all("test2") |
| 23 | + sleep(0.5) |
| 24 | + assert client.received_messages == ["test1"] |
| 25 | + |
| 26 | + |
| 27 | +def test_shutdown_gracefully(client_session): |
| 28 | + client, server = client_session |
| 29 | + assert client.ws.sock and client.ws.sock.connected |
| 30 | + assert server.socket.fileno() > 0 |
| 31 | + |
| 32 | + server.shutdown_gracefully() |
| 33 | + sleep(0.5) |
| 34 | + |
| 35 | + # Ensure all parties disconnected |
| 36 | + assert not client.ws.sock |
| 37 | + assert server.socket.fileno() == -1 |
| 38 | + assert not server.clients |
| 39 | + |
| 40 | + |
| 41 | +def test_shutdown_abruptly(client_session): |
| 42 | + client, server = client_session |
| 43 | + assert client.ws.sock and client.ws.sock.connected |
| 44 | + assert server.socket.fileno() > 0 |
| 45 | + |
| 46 | + server.shutdown_abruptly() |
| 47 | + sleep(0.5) |
| 48 | + |
| 49 | + # Ensure server socket died |
| 50 | + assert server.socket.fileno() == -1 |
| 51 | + |
| 52 | + # Ensure client handler terminated |
| 53 | + assert server.received_messages == [] |
| 54 | + assert client.errors == [] |
| 55 | + client.ws.send("1st msg after server shutdown") |
| 56 | + sleep(0.5) |
| 57 | + |
| 58 | + # Note the message is received since the client handler |
| 59 | + # will terminate only once it has received the last message |
| 60 | + # and break out of the keep_alive loop. Any consecutive messages |
| 61 | + # will not be received though. |
| 62 | + assert server.received_messages == ["1st msg after server shutdown"] |
| 63 | + assert len(client.errors) == 1 |
| 64 | + assert isinstance(client.errors[0], websocket._exceptions.WebSocketConnectionClosedException) |
| 65 | + |
| 66 | + # Try to send 2nd message |
| 67 | + with pytest.raises(websocket._exceptions.WebSocketConnectionClosedException): |
| 68 | + client.ws.send("2nd msg after server shutdown") |
| 69 | + |
| 70 | + |
| 71 | +def test_client_closes_gracefully(session): |
| 72 | + client, server = session |
| 73 | + assert client.connected |
| 74 | + assert server.clients |
| 75 | + old_client_handler = server.clients[0]["handler"] |
| 76 | + client.close() |
| 77 | + assert not client.connected |
| 78 | + |
| 79 | + # Ensure server closed connection. |
| 80 | + # We test this by having the server trying to send |
| 81 | + # data to the client |
| 82 | + assert not server.clients |
| 83 | + with pytest.raises(BrokenPipeError): |
| 84 | + old_client_handler.connection.send(b"test") |
0 commit comments