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 issue with deletion of sessions during refresh() #2055

Merged
merged 2 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/contacts/contact_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@ def __init__(self, services, log):
self.sessions = []

async def refresh(self):
for index, session in enumerate(self.sessions):
index = 0

while index < len(self.sessions):
session = self.sessions[index]

try:
session.connection.send(str.encode(' '))
except socket.error:
self.log.debug('Error occurred when refreshing session %s. Removing from session pool.', session.id)
del self.sessions[index]
else:
index += 1

async def accept(self, reader, writer):
try:
Expand Down
37 changes: 37 additions & 0 deletions tests/contacts/test_contact_tcp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging
import socket
from unittest import mock

from app.contacts.contact_tcp import TcpSessionHandler

logger = logging.getLogger(__name__)


class TestTcpSessionHandler:

def test_refresh_with_socket_errors(self, loop):
handler = TcpSessionHandler(services=None, log=logger)

session_with_socket_error = mock.Mock()
session_with_socket_error.connection.send.side_effect = socket.error()

handler.sessions = [
session_with_socket_error,
session_with_socket_error,
mock.Mock()
]

loop.run_until_complete(handler.refresh())
assert len(handler.sessions) == 1
assert all(x is not session_with_socket_error for x in handler.sessions)

def test_refresh_without_socket_errors(self, loop):
handler = TcpSessionHandler(services=None, log=logger)
handler.sessions = [
mock.Mock(),
mock.Mock(),
mock.Mock()
]

loop.run_until_complete(handler.refresh())
assert len(handler.sessions) == 3