-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from noisyboiler/refactor-wampy-websockets
Refactor wampy websockets
- Loading branch information
Showing
9 changed files
with
368 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import logging | ||
from collections import OrderedDict | ||
|
||
import pytest | ||
import gevent | ||
from gevent import Greenlet | ||
from geventwebsocket import ( | ||
WebSocketApplication, WebSocketServer, Resource, | ||
) | ||
from mock import ANY | ||
from mock import call, patch | ||
|
||
from wampy.transports.websocket.connection import WebSocket | ||
from wampy.transports.websocket.frames import Ping | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TestApplication(WebSocketApplication): | ||
pass | ||
|
||
|
||
@pytest.fixture | ||
def server(): | ||
s = WebSocketServer( | ||
('0.0.0.0', 8001), | ||
Resource(OrderedDict([('/', TestApplication)])) | ||
) | ||
s.start() | ||
thread = Greenlet.spawn(s.serve_forever) | ||
yield s | ||
s.stop() | ||
thread.kill() | ||
|
||
|
||
def test_send_ping(server): | ||
websocket = WebSocket(server_url='ws://0.0.0.0:8001') | ||
with patch.object(websocket, 'handle_ping') as mock_handle: | ||
assert websocket.connected is False | ||
|
||
websocket.connect(upgrade=False) | ||
|
||
def connection_handler(): | ||
while True: | ||
try: | ||
message = websocket.receive() | ||
except Exception: | ||
logger.execption('connection handler exploded') | ||
raise | ||
if message: | ||
logger.info('got message: %s', message) | ||
|
||
assert websocket.connected is True | ||
|
||
# the first bytes sent down the connection are the response bytes | ||
# to the TCP connection and upgrade. we receieve in this thread | ||
# because it will block all execution | ||
Greenlet.spawn(connection_handler) | ||
gevent.sleep(0.01) # enough for the upgrade to happen | ||
|
||
clients = server.clients | ||
assert len(clients) == 1 | ||
|
||
client_handler = list(clients.values())[0] | ||
socket = client_handler.ws | ||
|
||
ping_frame = Ping() | ||
socket.send(ping_frame.frame) | ||
|
||
with gevent.Timeout(5): | ||
while mock_handle.call_count != 1: | ||
gevent.sleep(0.01) | ||
|
||
assert mock_handle.call_count == 1 | ||
assert mock_handle.call_args == call(ping_frame=ANY) | ||
|
||
call_param = mock_handle.call_args[1]['ping_frame'] | ||
assert isinstance(call_param, Ping) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.