Skip to content

Commit

Permalink
some cleanups and better handling of clientsession .. however, the te…
Browse files Browse the repository at this point in the history
…sts were failing because the loop server was in a bad state
  • Loading branch information
denniswittich committed Feb 28, 2025
1 parent 83b777a commit e1d43ca
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
15 changes: 11 additions & 4 deletions learning_loop_node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def __init__(self, name: str, uuid: Optional[str] = None, node_type: str = 'node
self.previous_state: Optional[str] = None
self.repeat_loop_cycle_sec = 5

self._client_session: Optional[aiohttp.ClientSession] = None

def log_status_on_change(self, current_state_str: str, full_status: Any):
if self.previous_state != current_state_str:
self.previous_state = current_state_str
Expand Down Expand Up @@ -127,6 +129,8 @@ async def _on_shutdown(self):
await self.loop_communicator.shutdown()
if self._sio_client is not None:
await self._sio_client.disconnect()
if self._client_session is not None:
await self._client_session.close()
self.log.info('successfully disconnected from loop.')
await self.on_shutdown()

Expand Down Expand Up @@ -205,12 +209,15 @@ async def _reconnect_socketio(self):
ssl_context.verify_mode = ssl.CERT_REQUIRED
connector = TCPConnector(ssl=ssl_context)

if self._client_session is not None:
await self._client_session.close()

if self.needs_login:
self._sio_client = AsyncClient(request_timeout=20, http_session=aiohttp.ClientSession(
cookies=cookies, connector=connector))
self._client_session = aiohttp.ClientSession(cookies=cookies, connector=connector)
else:
self._sio_client = AsyncClient(request_timeout=20, http_session=aiohttp.ClientSession(
connector=connector))
self._client_session = aiohttp.ClientSession(connector=connector)

self._sio_client = AsyncClient(request_timeout=20, http_session=self._client_session)

# pylint: disable=protected-access
self._sio_client._trigger_event = ensure_socket_response(self._sio_client._trigger_event)
Expand Down
6 changes: 3 additions & 3 deletions learning_loop_node/tests/annotator/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
async def setup_test_project(): # pylint: disable=redefined-outer-name
loop_communicator = LoopCommunicator()
try:
await loop_communicator.delete("/zauberzeug/projects/pytest_nodelib_annotator?keep_images=true")
await loop_communicator.delete("/zauberzeug/projects/pytest_nodelib_annotator?keep_images=true", timeout=10)
except Exception:
logging.warning("Failed to delete project")
logging.warning("Failed to delete project pytest_nodelib_annotator")
sys.exit(1)
await asyncio.sleep(1)
project_conf = {
Expand All @@ -27,7 +27,7 @@ async def setup_test_project(): # pylint: disable=redefined-outer-name
'trainings': 1, 'box_detections': 3, 'box_annotations': 0}
assert (await loop_communicator.post("/zauberzeug/projects/generator", json=project_conf)).status_code == 200
yield
await loop_communicator.delete("/zauberzeug/projects/pytest_nodelib_annotator?keep_images=true")
await loop_communicator.delete("/zauberzeug/projects/pytest_nodelib_annotator?keep_images=true", timeout=10)
await loop_communicator.shutdown()


Expand Down
12 changes: 10 additions & 2 deletions learning_loop_node/tests/annotator/test_annotator_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

from ...annotation.annotator_logic import AnnotatorLogic
from ...annotation.annotator_node import AnnotatorNode
from ...data_classes import AnnotationData, Category, Context, Point, ToolOutput, UserInput
from ...data_classes import (
AnnotationData,
Category,
Context,
Point,
ToolOutput,
UserInput,
)
from ...enums import AnnotationEventType, CategoryType


Expand Down Expand Up @@ -37,7 +44,8 @@ def default_user_input() -> UserInput:


@pytest.mark.asyncio
async def test_image_download(setup_test_project): # pylint: disable=unused-argument
@pytest.mark.usefixtures('setup_test_project')
async def test_image_download():
image_folder = '/tmp/learning_loop_lib_data/zauberzeug/pytest_nodelib_annotator/images'

assert os.path.exists(image_folder) is False or len(os.listdir(image_folder)) == 0
Expand Down
2 changes: 1 addition & 1 deletion learning_loop_node/tests/detector/test_outbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def test_set_outbox_mode(test_outbox: Outbox):
assert await wait_for_outbox_count(test_outbox, 1), 'File was cleared even though outbox should be stopped'

await test_outbox.set_mode('continuous_upload')
assert await wait_for_outbox_count(test_outbox, 0), 'File was not cleared even though outbox should be in continuous_upload'
assert await wait_for_outbox_count(test_outbox, 0, timeout=15), 'File was not cleared even though outbox should be in continuous_upload'
assert test_outbox.upload_counter == 1


Expand Down
10 changes: 8 additions & 2 deletions learning_loop_node/tests/general/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import shutil
import sys

import pytest

Expand All @@ -15,15 +16,20 @@
async def create_project_for_module():

loop_communicator = LoopCommunicator()
await loop_communicator.delete("/zauberzeug/projects/pytest_nodelib_general?keep_images=true")
try:
await loop_communicator.delete("/zauberzeug/projects/pytest_nodelib_general", timeout=10)
except Exception:
logging.warning("Failed to delete project pytest_nodelib_general")
sys.exit(1)

await asyncio.sleep(1)
project_configuration = {
'project_name': 'pytest_nodelib_general', 'inbox': 0, 'annotate': 0, 'review': 0, 'complete': 3, 'image_style': 'beautiful',
'box_categories': 2, 'point_categories': 2, 'segmentation_categories': 2, 'thumbs': False, 'tags': 0,
'trainings': 1, 'box_detections': 3, 'box_annotations': 0}
assert (await loop_communicator.post("/zauberzeug/projects/generator", json=project_configuration)).status_code == 200
yield
await loop_communicator.delete("/zauberzeug/projects/pytest_nodelib_general?keep_images=true")
await loop_communicator.delete("/zauberzeug/projects/pytest_nodelib_general", timeout=10)
await loop_communicator.shutdown()


Expand Down

0 comments on commit e1d43ca

Please sign in to comment.