|
1 |
| -from utils import session, client_session, server |
| 1 | +from utils import session, client_session, threaded_server |
2 | 2 | from time import sleep
|
| 3 | +import threading |
| 4 | + |
| 5 | +from websocket_server import WebsocketServer |
3 | 6 |
|
4 | 7 | import websocket
|
5 | 8 | import pytest
|
6 | 9 |
|
7 | 10 |
|
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") |
| 11 | +class TestServerThreadedWithoutClient(): |
| 12 | + def test_run_forever(self, threaded_server): |
| 13 | + assert threaded_server.thread |
| 14 | + assert not isinstance(threaded_server.thread, threading._MainThread) |
| 15 | + assert threaded_server.thread.is_alive() |
| 16 | + |
| 17 | + def test_shutdown(self, threaded_server): |
| 18 | + assert threaded_server.thread.is_alive() |
| 19 | + |
| 20 | + # Shutdown de-facto way |
| 21 | + # REF: https://docs.python.org/3/library/socketserver.html |
| 22 | + # "Tell the serve_forever() loop to stop and |
| 23 | + # wait until it does. shutdown() must be called while serve_forever() |
| 24 | + # is running in a different thread otherwise it will deadlock." |
| 25 | + threaded_server.shutdown() |
| 26 | + assert not threaded_server.thread.is_alive() |
| 27 | + |
| 28 | + def test_shutdown_gracefully_without_clients(self, threaded_server): |
| 29 | + assert threaded_server.thread.is_alive() |
| 30 | + threaded_server.shutdown_gracefully() |
| 31 | + assert not threaded_server.thread.is_alive() |
| 32 | + assert threaded_server.socket.fileno() <= 0 |
| 33 | + |
| 34 | + def test_shutdown_abruptly_without_clients(self, threaded_server): |
| 35 | + assert threaded_server.thread.is_alive() |
| 36 | + threaded_server.shutdown_abruptly() |
| 37 | + assert not threaded_server.thread.is_alive() |
| 38 | + assert threaded_server.socket.fileno() <= 0 |
| 39 | + |
| 40 | + |
| 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