Skip to content

Commit

Permalink
minor class-based namespace fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Aug 23, 2016
1 parent 5cc9199 commit 214abc8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
9 changes: 8 additions & 1 deletion socketio/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ def __init__(self, namespace=None):
self.namespace = namespace or '/'
self.server = None

def set_server(self, server):
def _set_server(self, server):
self.server = server

def trigger_event(self, event, *args):
"""Dispatch an event to the proper handler method.
In the most common usage, this method is not overloaded by subclasses,
as it performs the routing of events to methods. However, this
method can be overriden if special dispatching rules are needed, or if
having a single method that catches all events is desired.
"""
handler_name = 'on_' + event
if hasattr(self, handler_name):
return getattr(self, handler_name)(*args)
Expand Down
2 changes: 1 addition & 1 deletion socketio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def register_namespace(self, namespace_handler):
"""
if not isinstance(namespace_handler, namespace.Namespace):
raise ValueError('Not a namespace instance')
namespace_handler.set_server(self)
namespace_handler._set_server(self)
self.namespace_handlers[namespace_handler.namespace] = \
namespace_handler

Expand Down
22 changes: 11 additions & 11 deletions tests/test_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def on_connect(self, sid, environ):
result['result'] = (sid, environ)

ns = MyNamespace('/foo')
ns.set_server(mock.MagicMock())
ns._set_server(mock.MagicMock())
ns.trigger_event('connect', 'sid', {'foo': 'bar'})
self.assertEqual(result['result'], ('sid', {'foo': 'bar'}))

Expand All @@ -29,7 +29,7 @@ def on_disconnect(self, sid):
result['result'] = sid

ns = MyNamespace('/foo')
ns.set_server(mock.MagicMock())
ns._set_server(mock.MagicMock())
ns.trigger_event('disconnect', 'sid')
self.assertEqual(result['result'], 'sid')

Expand All @@ -41,7 +41,7 @@ def on_custom_message(self, sid, data):
result['result'] = (sid, data)

ns = MyNamespace('/foo')
ns.set_server(mock.MagicMock())
ns._set_server(mock.MagicMock())
ns.trigger_event('custom_message', 'sid', {'data': 'data'})
self.assertEqual(result['result'], ('sid', {'data': 'data'}))

Expand All @@ -53,13 +53,13 @@ def on_custom_message(self, sid, data):
result['result'] = (sid, data)

ns = MyNamespace('/foo')
ns.set_server(mock.MagicMock())
ns._set_server(mock.MagicMock())
ns.trigger_event('another_custom_message', 'sid', {'data': 'data'})
self.assertEqual(result, {})

def test_emit(self):
ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock())
ns._set_server(mock.MagicMock())
ns.emit('ev', data='data', room='room', skip_sid='skip',
callback='cb')
ns.server.emit.assert_called_with(
Expand All @@ -73,7 +73,7 @@ def test_emit(self):

def test_send(self):
ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock())
ns._set_server(mock.MagicMock())
ns.send(data='data', room='room', skip_sid='skip', callback='cb')
ns.server.send.assert_called_with(
'data', room='room', skip_sid='skip', namespace='/foo',
Expand All @@ -86,7 +86,7 @@ def test_send(self):

def test_enter_room(self):
ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock())
ns._set_server(mock.MagicMock())
ns.enter_room('sid', 'room')
ns.server.enter_room.assert_called_with('sid', 'room',
namespace='/foo')
Expand All @@ -96,7 +96,7 @@ def test_enter_room(self):

def test_leave_room(self):
ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock())
ns._set_server(mock.MagicMock())
ns.leave_room('sid', 'room')
ns.server.leave_room.assert_called_with('sid', 'room',
namespace='/foo')
Expand All @@ -106,23 +106,23 @@ def test_leave_room(self):

def test_close_room(self):
ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock())
ns._set_server(mock.MagicMock())
ns.close_room('room')
ns.server.close_room.assert_called_with('room', namespace='/foo')
ns.close_room('room', namespace='/bar')
ns.server.close_room.assert_called_with('room', namespace='/bar')

def test_rooms(self):
ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock())
ns._set_server(mock.MagicMock())
ns.rooms('sid')
ns.server.rooms.assert_called_with('sid', namespace='/foo')
ns.rooms('sid', namespace='/bar')
ns.server.rooms.assert_called_with('sid', namespace='/bar')

def test_disconnect(self):
ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock())
ns._set_server(mock.MagicMock())
ns.disconnect('sid')
ns.server.disconnect.assert_called_with('sid', namespace='/foo')
ns.disconnect('sid', namespace='/bar')
Expand Down

0 comments on commit 214abc8

Please sign in to comment.