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

fix test_client connected error #1829

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/flask_socketio/test_client.py
Expand Up @@ -26,6 +26,7 @@ class SocketIOTestClient(object):
"""
queue = {}
acks = {}
connected = {}

def __init__(self, app, socketio, namespace=None, query_string=None,
headers=None, auth=None, flask_test_client=None):
Expand Down Expand Up @@ -57,7 +58,7 @@ def _mock_send_packet(eio_sid, pkt):
self.acks[eio_sid] = {'args': pkt.data,
'namespace': pkt.namespace or '/'}
elif pkt.packet_type in [packet.DISCONNECT, packet.CONNECT_ERROR]:
self.connected[pkt.namespace or '/'] = False
self.connected[eio_sid][pkt.namespace or '/'] = False

self.app = app
self.flask_test_client = flask_test_client
Expand All @@ -66,7 +67,7 @@ def _mock_send_packet(eio_sid, pkt):
self.queue[self.eio_sid] = []
self.callback_counter = 0
self.socketio = socketio
self.connected = {}
self.connected[self.eio_sid] = {}
socketio.server._send_packet = _mock_send_packet
socketio.server.environ[self.eio_sid] = {}
socketio.server.async_handlers = False # easier to test when
Expand All @@ -85,7 +86,7 @@ def is_connected(self, namespace=None):
:param namespace: The namespace to check. The global namespace is
assumed if this argument is not provided.
"""
return self.connected.get(namespace or '/', False)
return self.connected[self.eio_sid].get(namespace or '/', False)

def connect(self, namespace=None, query_string=None, headers=None,
auth=None):
Expand Down Expand Up @@ -120,7 +121,7 @@ def connect(self, namespace=None, query_string=None, headers=None,
sid = self.socketio.server.manager.sid_from_eio_sid(self.eio_sid,
namespace)
if sid:
self.connected[namespace] = True
self.connected[self.eio_sid][namespace] = True

def disconnect(self, namespace=None):
"""Disconnect the client.
Expand All @@ -132,7 +133,7 @@ def disconnect(self, namespace=None):
raise RuntimeError('not connected')
pkt = packet.Packet(packet.DISCONNECT, namespace=namespace)
self.socketio.server._handle_eio_message(self.eio_sid, pkt.encode())
del self.connected[namespace or '/']
del self.connected[self.eio_sid][namespace or '/']

def emit(self, event, *args, **kwargs):
"""Emit an event to the server.
Expand Down Expand Up @@ -203,8 +204,6 @@ def get_received(self, namespace=None):
namespace is assumed if this argument is not
provided.
"""
if not self.is_connected(namespace):
raise RuntimeError('not connected')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

namespace = namespace or '/'
r = [pkt for pkt in self.queue[self.eio_sid]
if pkt['namespace'] == namespace]
Expand Down
16 changes: 15 additions & 1 deletion test_socketio.py
Expand Up @@ -237,7 +237,7 @@ def on_json(self, data):
return data

def on_exit(self, data):
disconnect()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change seems unrelated

self.disconnect(request.sid)

def on_my_custom_event(self, data):
emit('my custom response', data)
Expand Down Expand Up @@ -672,6 +672,20 @@ def test_server_disconnected(self):
with self.assertRaises(RuntimeError):
client.emit('hello', {}, namespace='/ns')

def test_server_disconnected_2(self):
client = socketio.test_client(app, namespace='/ns')
client2 = socketio.test_client(app, namespace='/ns')
client.get_received('/ns')
client.emit('exit', {}, namespace='/ns')
self.assertFalse(client.is_connected('/ns'))
with self.assertRaises(RuntimeError):
client.emit('hello', {}, namespace='/ns')
client2.get_received('/ns')
client2.emit('exit', {}, namespace='/ns')
self.assertFalse(client2.is_connected('/ns'))
with self.assertRaises(RuntimeError):
client2.emit('hello', {}, namespace='/ns')

def test_emit_class_based(self):
client = socketio.test_client(app, namespace='/ns')
client.get_received('/ns')
Expand Down