Skip to content

Commit

Permalink
Merge pull request #55 from wildfoundry/doc-tweaks
Browse files Browse the repository at this point in the history
Fix non-graceful close
  • Loading branch information
willmcgugan committed May 14, 2018
2 parents f66845b + 4470899 commit 4fb623d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.2.3] - 2018-05-14

### Fixed non-graceful close https://github.com/wildfoundry/dataplicity-lomond/issues/54

## [0.2.2] - 2018-05-09

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion docs/guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ You can send a ping / pong packet at any time with
:meth:`~lomond.websocket.WebSocket.send_pong`.

.. note::
The server may send an pong packets *not* in response to a ping
The server may send pong packets *not* in response to a ping
packet (see https://tools.ietf.org/html/rfc6455#section-5.5.3
for details).

Expand Down
2 changes: 1 addition & 1 deletion lomond/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from __future__ import unicode_literals

__version__ = "0.2.2"
__version__ = "0.2.3"
4 changes: 3 additions & 1 deletion lomond/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,9 @@ def _regular():
for event in _regular():
yield event
else:
self._socket_fail('connection lost')
if websocket.is_active:
self._socket_fail('connection lost')
break
except _ForceDisconnect as error:
self._close_socket()
yield events.Disconnected('disconnected; {}'.format(error))
Expand Down
44 changes: 44 additions & 0 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,50 @@ def test_simple_run(monkeypatch, mocker):
assert not _events[5].graceful


@mocketize
def test_simple_run_with_close(monkeypatch, mocker):
"""Test graceful close."""
monkeypatch.setattr(
'os.urandom', b'\x00'.__mul__
)
Mocket.register(
MocketEntry(
('example.com', 80),
[(
b'HTTP/1.1 101 Switching Protocols\r\n'
b'Upgrade: websocket\r\n'
b'Connection: Upgrade\r\n'
b'Sec-WebSocket-Accept: icx+yqv66kxgm0fcwalwlflwtai=\r\n'
b'\r\n'
b'\x81\x81\x00\x00\x00\x00A\x88\x80\xba51e'
)]
)
)

session = WebsocketSession(WebSocket('ws://example.com/'))
session._selector_cls = FakeSelector
session._on_ready()

session._regular_orig = session._regular

mocker.patch(
'lomond.websocket.WebSocket._send_close')
mocker.patch.object(session.websocket, 'send_ping')
session.websocket.state.session = session

_events = list(session.run())

assert len(_events) == 7
assert isinstance(_events[0], events.Connecting)
assert isinstance(_events[1], events.Connected)
assert isinstance(_events[2], events.Ready)
assert isinstance(_events[3], events.Poll)
assert isinstance(_events[4], events.Text)
assert isinstance(_events[5], events.Closing)
assert isinstance(_events[6], events.Disconnected)
assert _events[6].graceful


@freeze_time("1994-05-01 18:40:00")
@mocketize
def test_unresponsive(monkeypatch, mocker):
Expand Down

0 comments on commit 4fb623d

Please sign in to comment.