Skip to content
Merged
Show file tree
Hide file tree
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
43 changes: 35 additions & 8 deletions test/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,42 @@ def test_close(self, as_client, code, reason):
assert event.code is code
assert event.reason == reason

def test_normal_closure(self):
client, server = self.create_connection()
@pytest.mark.parametrize('swap', [
True,
False
])
def test_normal_closure(self, swap):
if swap:
completor, initiator = self.create_connection()
else:
initiator, completor = self.create_connection()

for conn in (client, server):
conn.close()
conn.receive_bytes(None)
with pytest.raises(StopIteration):
repr(next(conn.events()))
assert conn.closed
# initiator sends CLOSE to completor
initiator.close()
completor.receive_bytes(initiator.bytes_to_send())

# completor emits ConnectionClosed
assert isinstance(next(completor.events()), ConnectionClosed)

# completor enters CLOSED state
assert completor.closed
with pytest.raises(StopIteration):
next(completor.events())

# completor sends CLOSE back to initiator
initiator.receive_bytes(completor.bytes_to_send())

# initiator emits ConnectionClosed
assert isinstance(next(initiator.events()), ConnectionClosed)

# initiator enters CLOSED state
assert initiator.closed
with pytest.raises(StopIteration):
next(initiator.events())

completor.ping()
with pytest.raises(ValueError):
initiator.receive_bytes(completor.bytes_to_send())

def test_abnormal_closure(self):
client, server = self.create_connection()
Expand Down
11 changes: 6 additions & 5 deletions wsproto/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,16 @@ def receive_bytes(self, data):
self._events.append(ConnectionClosed(CloseReason.ABNORMAL_CLOSURE))
self._state = ConnectionState.CLOSED
return
elif data is None:
self._state = ConnectionState.CLOSED
return

if self._state is ConnectionState.CONNECTING:
event, data = self._process_upgrade(data)
if event is not None:
self._events.append(event)

if self._state is ConnectionState.OPEN:
if self._state in (ConnectionState.OPEN, ConnectionState.CLOSING):
self._proto.receive_bytes(data)
elif self._state is ConnectionState.CLOSED:
raise ValueError('Connection already closed.')

def _process_upgrade(self, data):
self._upgrade_connection.receive_data(data)
Expand Down Expand Up @@ -289,7 +288,9 @@ def events(self):

elif frame.opcode is Opcode.CLOSE:
code, reason = frame.payload
self.close(code, reason)
if self._state is ConnectionState.OPEN:
self.close(code, reason)
self._state = ConnectionState.CLOSED
yield ConnectionClosed(code, reason)

elif frame.opcode is Opcode.TEXT:
Expand Down
5 changes: 5 additions & 0 deletions wsproto/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def __repr__(self):


class ConnectionClosed(object):
"""
The ConnectionClosed event is fired after the connection is considered closed.

wsproto automatically emits a CLOSE frame when it receives one, to complete the close-handshake.
"""
def __init__(self, code, reason=None):
self.code = code
self.reason = reason
Expand Down