Skip to content

Commit

Permalink
Fix issue with deletion of sessions during refresh() (#2055)
Browse files Browse the repository at this point in the history
* Fix issue with deletion of sessions during refresh().

* Remove use of IsolatedAsyncioTestCase.

It's not available in all versions of Python we support.

Co-authored-by: Bryan Worrell <[email protected]>
  • Loading branch information
bworrell and Bryan Worrell committed Feb 19, 2021
1 parent 09fee12 commit 2b6c935
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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

0 comments on commit 2b6c935

Please sign in to comment.