|
8 | 8 | import pytest |
9 | 9 |
|
10 | 10 |
|
11 | | -def test_send_close(client_session): |
12 | | - """ |
13 | | - Ensure client stops receiving data once we send_close (socket is still open) |
14 | | - """ |
15 | | - client, server = client_session |
16 | | - assert client.received_messages == [] |
17 | | - |
18 | | - server.send_message_to_all("test1") |
19 | | - sleep(0.5) |
20 | | - assert client.received_messages == ["test1"] |
21 | | - |
22 | | - # After CLOSE, client should not be receiving any messages |
23 | | - server.clients[-1]["handler"].send_close() |
24 | | - sleep(0.5) |
25 | | - server.send_message_to_all("test2") |
26 | | - sleep(0.5) |
27 | | - assert client.received_messages == ["test1"] |
28 | | - |
29 | | - |
30 | | -def test_shutdown_gracefully(client_session): |
31 | | - client, server = client_session |
32 | | - assert client.ws.sock and client.ws.sock.connected |
33 | | - assert server.socket.fileno() > 0 |
34 | | - |
35 | | - server.shutdown_gracefully() |
36 | | - sleep(0.5) |
37 | | - |
38 | | - # Ensure all parties disconnected |
39 | | - assert not client.ws.sock |
40 | | - assert server.socket.fileno() == -1 |
41 | | - assert not server.clients |
42 | | - |
43 | | - |
44 | 11 | class TestServerThreadedWithoutClient(): |
45 | 12 | def test_run_forever(self, threaded_server): |
46 | 13 | assert threaded_server.thread |
@@ -71,47 +38,78 @@ def test_shutdown_abruptly_without_clients(self, threaded_server): |
71 | 38 | assert threaded_server.socket.fileno() <= 0 |
72 | 39 |
|
73 | 40 |
|
74 | | -def test_shutdown_abruptly(client_session): |
75 | | - client, server = client_session |
76 | | - assert client.ws.sock and client.ws.sock.connected |
77 | | - assert server.socket.fileno() > 0 |
78 | | - |
79 | | - server.shutdown_abruptly() |
80 | | - sleep(0.5) |
81 | | - |
82 | | - # Ensure server socket died |
83 | | - assert server.socket.fileno() == -1 |
84 | | - |
85 | | - # Ensure client handler terminated |
86 | | - assert server.received_messages == [] |
87 | | - assert client.errors == [] |
88 | | - client.ws.send("1st msg after server shutdown") |
89 | | - sleep(0.5) |
90 | | - |
91 | | - # Note the message is received since the client handler |
92 | | - # will terminate only once it has received the last message |
93 | | - # and break out of the keep_alive loop. Any consecutive messages |
94 | | - # will not be received though. |
95 | | - assert server.received_messages == ["1st msg after server shutdown"] |
96 | | - assert len(client.errors) == 1 |
97 | | - assert isinstance(client.errors[0], websocket._exceptions.WebSocketConnectionClosedException) |
98 | | - |
99 | | - # Try to send 2nd message |
100 | | - with pytest.raises(websocket._exceptions.WebSocketConnectionClosedException): |
101 | | - client.ws.send("2nd msg after server shutdown") |
102 | | - |
103 | | - |
104 | | -def test_client_closes_gracefully(session): |
105 | | - client, server = session |
106 | | - assert client.connected |
107 | | - assert server.clients |
108 | | - old_client_handler = server.clients[0]["handler"] |
109 | | - client.close() |
110 | | - assert not client.connected |
111 | | - |
112 | | - # Ensure server closed connection. |
113 | | - # We test this by having the server trying to send |
114 | | - # data to the client |
115 | | - assert not server.clients |
116 | | - with pytest.raises(BrokenPipeError): |
117 | | - old_client_handler.connection.send(b"test") |
| 41 | +class TestServerThreadedWithClient(): |
| 42 | + def test_send_close(self, client_session): |
| 43 | + """ |
| 44 | + Ensure client stops receiving data once we send_close (socket is still open) |
| 45 | + """ |
| 46 | + client, server = client_session |
| 47 | + assert client.received_messages == [] |
| 48 | + |
| 49 | + server.send_message_to_all("test1") |
| 50 | + sleep(0.5) |
| 51 | + assert client.received_messages == ["test1"] |
| 52 | + |
| 53 | + # After CLOSE, client should not be receiving any messages |
| 54 | + server.clients[-1]["handler"].send_close() |
| 55 | + sleep(0.5) |
| 56 | + server.send_message_to_all("test2") |
| 57 | + sleep(0.5) |
| 58 | + assert client.received_messages == ["test1"] |
| 59 | + |
| 60 | + def test_shutdown_gracefully(self, client_session): |
| 61 | + client, server = client_session |
| 62 | + assert client.ws.sock and client.ws.sock.connected |
| 63 | + assert server.socket.fileno() > 0 |
| 64 | + |
| 65 | + server.shutdown_gracefully() |
| 66 | + sleep(0.5) |
| 67 | + |
| 68 | + # Ensure all parties disconnected |
| 69 | + assert not client.ws.sock |
| 70 | + assert server.socket.fileno() == -1 |
| 71 | + assert not server.clients |
| 72 | + |
| 73 | + def test_shutdown_abruptly(self, client_session): |
| 74 | + client, server = client_session |
| 75 | + assert client.ws.sock and client.ws.sock.connected |
| 76 | + assert server.socket.fileno() > 0 |
| 77 | + |
| 78 | + server.shutdown_abruptly() |
| 79 | + sleep(0.5) |
| 80 | + |
| 81 | + # Ensure server socket died |
| 82 | + assert server.socket.fileno() == -1 |
| 83 | + |
| 84 | + # Ensure client handler terminated |
| 85 | + assert server.received_messages == [] |
| 86 | + assert client.errors == [] |
| 87 | + client.ws.send("1st msg after server shutdown") |
| 88 | + sleep(0.5) |
| 89 | + |
| 90 | + # Note the message is received since the client handler |
| 91 | + # will terminate only once it has received the last message |
| 92 | + # and break out of the keep_alive loop. Any consecutive messages |
| 93 | + # will not be received though. |
| 94 | + assert server.received_messages == ["1st msg after server shutdown"] |
| 95 | + assert len(client.errors) == 1 |
| 96 | + assert isinstance(client.errors[0], websocket._exceptions.WebSocketConnectionClosedException) |
| 97 | + |
| 98 | + # Try to send 2nd message |
| 99 | + with pytest.raises(websocket._exceptions.WebSocketConnectionClosedException): |
| 100 | + client.ws.send("2nd msg after server shutdown") |
| 101 | + |
| 102 | + def test_client_closes_gracefully(self, session): |
| 103 | + client, server = session |
| 104 | + assert client.connected |
| 105 | + assert server.clients |
| 106 | + old_client_handler = server.clients[0]["handler"] |
| 107 | + client.close() |
| 108 | + assert not client.connected |
| 109 | + |
| 110 | + # Ensure server closed connection. |
| 111 | + # We test this by having the server trying to send |
| 112 | + # data to the client |
| 113 | + assert not server.clients |
| 114 | + with pytest.raises(BrokenPipeError): |
| 115 | + old_client_handler.connection.send(b"test") |
0 commit comments