Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to simulate lost connection on server #758

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/protocols/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,34 @@ async def get_subprotocol(url):
accepted_subprotocol = loop.run_until_complete(get_subprotocol(url))
assert accepted_subprotocol == subprotocol
loop.close()


@pytest.mark.parametrize("protocol_cls", WS_PROTOCOLS)
def test_server_lost_connection(protocol_cls):
class App(WebSocketResponse):
async def websocket_connect(self, message):
await self.send({"type": "websocket.accept"})
await self.receive()

# Simulate a lost connection
# without receiving a close frame
self.send.__self__.connection_lost(None)

with pytest.raises(Exception) as exc:
await self.send({"type": "websocket.send", "text": "123"})

assert exc.value.code == 1006

async def websocket_session(url):
async with websockets.connect(url) as websocket:
await websocket.ping()
await websocket.send("abc")
# Delay exiting context manager
# to avoid sending a close frame before server attempts send
await asyncio.sleep(1)

if protocol_cls is WebSocketProtocol:
with run_server(App, protocol_cls=protocol_cls) as url:
loop = asyncio.new_event_loop()
loop.run_until_complete(websocket_session(url))
loop.close()