Skip to content
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
27 changes: 1 addition & 26 deletions backend/entityservice/tests/test_project_run_description.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
import pytest
from entityservice.tests.config import url
from entityservice.tests.util import create_project_upload_fake_data, post_run, get_run


def test_run_description_missing_run(requests, project):
_ = get_run(requests, project, 'invalid', expected_status = 403)


def test_run_description_no_data(requests, project):
run_id = post_run(requests, project, 0.95)
run = get_run(requests, project, run_id)

assert 'run_id' in run
assert 'notes' in run
assert 'threshold' in run


def test_run_description(requests, result_type_number_parties):
THRESHOLD = .98

result_type, number_parties = result_type_number_parties
project, _ = create_project_upload_fake_data(
requests, [100] * number_parties, overlap=0.5, result_type=result_type)
run_id = post_run(requests, project, THRESHOLD)
run = get_run(requests, project, run_id)

assert 'run_id' in run
assert 'notes' in run
assert run['threshold'] == THRESHOLD
_ = get_run(requests, project, 'invalid', expected_status=403)
17 changes: 1 addition & 16 deletions backend/entityservice/tests/test_project_run_status.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@

from entityservice.tests.util import post_run, get_run_status, is_run_status, \
create_project_no_data, ensure_run_progressing


def test_permutations_run_status_with_clks_2p(requests, permutations_project):
size = permutations_project['size']
ensure_run_progressing(requests, permutations_project, size)


def test_similarity_scores_run_status_with_clks_2p(requests, similarity_scores_project):
size = similarity_scores_project['size']
ensure_run_progressing(requests, similarity_scores_project, size)


def test_groups_run_status_with_clks_np(requests, groups_project):
size = groups_project['size']
ensure_run_progressing(requests, groups_project, size)
create_project_no_data


def test_run_status_without_clks(requests):
Expand Down
66 changes: 27 additions & 39 deletions backend/entityservice/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,23 @@ def wait_for_run(requests, project, run_id, ok_statuses, result_token=None, time
"""
Poll project/run_id until its status is one of the ok_statuses. Raise a
TimeoutError if we've waited more than timeout seconds.
It also checks that the status are progressing normally, using the checks implemented in
`has_progressed_validly`.
"""
start_time = time.time()
old_status = None
while True:
status = get_run_status(requests, project, run_id, result_token)
if old_status:
if not has_progressed_validly(old_status, status):
raise Exception("The run has not progressed as expected. The old status "
"update was '{}' while the new one is '{}'.".format(old_status, status))
if status['state'] in ok_statuses or status['state'] == 'error':
break
if time.time() - start_time > timeout:
raise TimeoutError('waited for {}s'.format(timeout))
time.sleep(0.5)

old_status = status
return status


Expand All @@ -256,39 +263,6 @@ def wait_while_queued(requests, project, run_id, result_token=None, timeout=10):
return wait_for_run(requests, project, run_id, not_queued_statuses, result_token, timeout)


def wait_approx_run_time(size, assumed_rate=1_000_000):
"""Calculate how long the similarity comparison stage of a project should take
using a particular comparison rate. 1 M/s is quite conservative in order to work
on slower CI systems.
"""
number_comparisons = sum(
size0 * size1 for size0, size1 in itertools.combinations(size, 2))
time.sleep(number_comparisons / assumed_rate)


def ensure_run_progressing(requests, project, size):

run_id = post_run(requests, project, 0.9)
status = get_run_status(requests, project, run_id)

is_run_status(status)

if status['state'] not in {'completed', 'error'}:

original_status = status

dt = iso8601.parse_date(status['time_added'])
assert datetime.datetime.now(tz=datetime.timezone.utc) - dt < datetime.timedelta(seconds=5)

# Wait and see if the progress changes
wait_approx_run_time(size)
status = get_run_status(requests, project, run_id)

failure_msg = "Invalid progress for run {}. Status A:\n{}\nStatus B:\n{}\n".format(run_id, original_status,
status)
assert has_not_progressed_invalidly(original_status, status), failure_msg


def post_run(requests, project, threshold):
project_id = project['project_id']
result_token = project['result_token']
Expand All @@ -298,10 +272,13 @@ def post_run(requests, project, threshold):
headers={'Authorization': result_token},
json={'threshold': threshold})
assert req.status_code == 201
return req.json()['run_id']
run_id = req.json()['run_id']
# Each time we post a new run, we also check that the run endpoint works well.
get_run(requests, project, run_id, expected_threshold=threshold)
return run_id


def get_runs(requests, project, result_token = None, expected_status = 200):
def get_runs(requests, project, result_token=None, expected_status=200):
project_id = project['project_id']
result_token = project['result_token'] if result_token is None else result_token

Expand All @@ -312,20 +289,31 @@ def get_runs(requests, project, result_token = None, expected_status = 200):
return req.json()


def get_run(requests, project, run_id, expected_status = 200):
def get_run(requests, project, run_id, expected_status=200, expected_threshold=None):
project_id = project['project_id']
result_token = project['result_token']

req = requests.get(
url + '/projects/{}/runs/{}'.format(project_id, run_id),
headers={'Authorization': result_token})

assert req.status_code == expected_status
return req.json()
run = req.json()
if expected_status == 200:
# only check these assertions if all went well.
assert 'run_id' in run
assert run['run_id'] == run_id
assert 'notes' in run
assert 'threshold' in run
if expected_threshold:
assert expected_threshold == run['threshold']
return run


def get_run_result(requests, project, run_id, result_token=None, expected_status=200, wait=True, timeout=60):
result_token = project['result_token'] if result_token is None else result_token
if wait:
# wait_for_run_completion also checks that the progress is in order.
final_status = wait_for_run_completion(requests, project, run_id, result_token, timeout)
state = final_status['state']
assert state == 'completed', "Expected: 'completed', got: '{}'".format(state)
Expand Down Expand Up @@ -362,7 +350,7 @@ def from_string(state):
return State.completed


def has_not_progressed_invalidly(status_old, status_new):
def has_progressed_validly(status_old, status_new):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not not approve of this change ;)

"""
If there happened to be progress between the two statuses we check if it was valid.
Thus, we return False if there was invalid progress, and True otherwise. (even if there was no progress)
Expand Down