ipc: Handle None-socket in slots correctly.

This commit is contained in:
Florian Bruhin 2015-09-03 20:48:51 +02:00
parent 27ee09a7a1
commit b40fb4dd43
2 changed files with 24 additions and 4 deletions

View File

@ -141,8 +141,8 @@ class IPCServer(QObject):
def on_error(self, err):
"""Convenience method which calls _socket_error on an error."""
if self._socket is None:
# Sometimes this gets called from stale sockets, especially in
# tests.
log.ipc.warn("In on_error with None socket!")
# Sometimes this gets called from stale sockets.
return
self._timer.stop()
log.ipc.debug("Socket error {}: {}".format(
@ -185,7 +185,9 @@ class IPCServer(QObject):
"""Clean up socket when the client disconnected."""
log.ipc.debug("Client disconnected.")
self._timer.stop()
if self._socket is not None:
if self._socket is None:
log.ipc.warn("In on_disconnected with None socket!")
else:
self._socket.deleteLater()
self._socket = None
# Maybe another connection is waiting.
@ -194,7 +196,7 @@ class IPCServer(QObject):
@pyqtSlot()
def on_ready_read(self):
"""Read json data from the client."""
if self._socket is None: # pragma: no cover
if self._socket is None:
# This happens when doing a connection while another one is already
# active for some reason.
log.ipc.warn("In on_ready_read with None socket!")

View File

@ -368,3 +368,21 @@ def test_timeout(qtbot, caplog, qlocalsocket, ipc_server):
pass
assert caplog.records()[-1].message == "IPC connection timed out."
@pytest.mark.parametrize('method, args', [
('on_error', [0]),
('on_disconnected', []),
('on_ready_read', []),
])
def test_ipcserver_socket_none(ipc_server, caplog, method, args):
func = getattr(ipc_server, method)
assert ipc_server._socket is None
with caplog.atLevel(logging.WARNING):
func(*args)
records = caplog.records()
assert len(records) == 1
msg = "In {} with None socket!".format(method)
assert records[0].message == msg