Skip to content

Commit

Permalink
feat: introduce wait method to replace sleeps in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jstucke committed Dec 3, 2024
1 parent e0fbbba commit 782b922
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/test/acceptance/test_upload_analyze_delete_firmware.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import time
from pathlib import Path

import pytest

from storage.fsorganizer import FSOrganizer
from test.acceptance.conftest import test_fw_a, upload_test_firmware
from test.common_helper import wait_for_event


def _upload_firmware_get(test_client, intercom):
Expand Down Expand Up @@ -85,8 +85,7 @@ def _delete_firmware(test_client):
assert b'Deleted 4 file(s) from database' in rv.data, 'deletion success page not shown'
rv = test_client.get(f'/analysis/{test_fw_a.uid}')
assert b'File not found in database' in rv.data, 'file is still available after delete'
time.sleep(3)
assert not local_firmware_path.exists(), 'file not deleted'
assert wait_for_event(lambda: not local_firmware_path.exists()), 'file not deleted'


@pytest.mark.SchedulerTestConfig(
Expand Down
15 changes: 14 additions & 1 deletion src/test/common_helper.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

import time
from base64 import standard_b64encode
from contextlib import contextmanager
from copy import deepcopy
from http import HTTPStatus
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Callable

from helperFunctions.fileSystem import get_src_dir
from helperFunctions.tag import TagColor
Expand Down Expand Up @@ -326,3 +327,15 @@ def assert_search_result(response: TestResponse, included: list[FileObject], exc
assert f"href='/analysis/{fo.uid}'" in html, f'file {fo.uid} should be included in the result'
for fo in excluded:
assert f"href='/analysis/{fo.uid}'" not in html, f'file {fo.uid} should not be included in the result'


def wait_for_event(
expression: Callable[[], bool], timeout: float = 5.0, check_interval: float = 0.1, inverted: bool = False
) -> bool:
check: Callable[[], bool] = expression if not inverted else lambda: not expression()
start_time = time.time()
while time.time() - start_time < timeout:
if check():
return True
time.sleep(check_interval)
return check()

0 comments on commit 782b922

Please sign in to comment.