Skip to content

Commit

Permalink
chore: simplify wait_for_sidekiq usage
Browse files Browse the repository at this point in the history
Simplify usage of `wait_for_sidekiq` by putting the assert if it timed
out inside the function rather than after calling it.
  • Loading branch information
JohnVillalovos authored and nejch committed Oct 5, 2022
1 parent 6627a60 commit 196538b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 13 deletions.
3 changes: 1 addition & 2 deletions tests/functional/api/test_lazy_objects.py
Expand Up @@ -29,8 +29,7 @@ def test_save_after_lazy_get_with_path(project, lazy_project):

def test_delete_after_lazy_get_with_path(gl, group, wait_for_sidekiq):
project = gl.projects.create({"name": "lazy_project", "namespace_id": group.id})
result = wait_for_sidekiq(timeout=60)
assert result is True, "sidekiq process should have terminated but did not"
wait_for_sidekiq(timeout=60)
lazy_project = gl.projects.get(project.path_with_namespace, lazy=True)
lazy_project.delete()

Expand Down
12 changes: 4 additions & 8 deletions tests/functional/api/test_merge_requests.py
Expand Up @@ -150,8 +150,7 @@ def test_merge_request_should_remove_source_branch(

mr.merge(should_remove_source_branch=True)

result = wait_for_sidekiq(timeout=60)
assert result is True, "sidekiq process should have terminated but did not"
wait_for_sidekiq(timeout=60)

# Wait until it is merged
mr_iid = mr.iid
Expand All @@ -162,8 +161,7 @@ def test_merge_request_should_remove_source_branch(
time.sleep(0.5)
assert mr.merged_at is not None
time.sleep(0.5)
result = wait_for_sidekiq(timeout=60)
assert result is True, "sidekiq process should have terminated but did not"
wait_for_sidekiq(timeout=60)

# Ensure we can NOT get the MR branch
with pytest.raises(gitlab.exceptions.GitlabGetError):
Expand Down Expand Up @@ -195,8 +193,7 @@ def test_merge_request_large_commit_message(
merge_commit_message=merge_commit_message, should_remove_source_branch=False
)

result = wait_for_sidekiq(timeout=60)
assert result is True, "sidekiq process should have terminated but did not"
wait_for_sidekiq(timeout=60)

# Wait until it is merged
mr_iid = mr.iid
Expand Down Expand Up @@ -235,8 +232,7 @@ def test_merge_request_merge_ref_should_fail(
"commit_message": "Another commit in main branch",
}
)
result = wait_for_sidekiq(timeout=60)
assert result is True, "sidekiq process should have terminated but did not"
wait_for_sidekiq(timeout=60)

# Check for non-existing merge_ref for MR with conflicts
with pytest.raises(gitlab.exceptions.GitlabGetError):
Expand Down
3 changes: 1 addition & 2 deletions tests/functional/api/test_users.py
Expand Up @@ -70,8 +70,7 @@ def test_delete_user(gl, wait_for_sidekiq):
)

new_user.delete()
result = wait_for_sidekiq(timeout=60)
assert result is True, "sidekiq process should have terminated but did not"
wait_for_sidekiq(timeout=60)

assert new_user.id not in [user.id for user in gl.users.list()]

Expand Down
3 changes: 2 additions & 1 deletion tests/functional/conftest.py
Expand Up @@ -224,7 +224,7 @@ def wait_for_sidekiq(gl):
Use this with asserts for slow tasks (group/project/user creation/deletion).
"""

def _wait(timeout=30, step=0.5):
def _wait(timeout: int = 30, step: float = 0.5, allow_fail: bool = False) -> bool:
for count in range(timeout):
time.sleep(step)
busy = False
Expand All @@ -235,6 +235,7 @@ def _wait(timeout=30, step=0.5):
if not busy:
return True
logging.info(f"sidekiq busy {count} of {timeout}")
assert allow_fail, "sidekiq process should have terminated but did not."
return False

return _wait
Expand Down

0 comments on commit 196538b

Please sign in to comment.