| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2ef6452 commit 4e75be3
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | 6 | import gitlab | |
| 7 | 7 | import gitlab.v4.objects | |
| 8 | + import tests.functional.helpers | ||
| 8 | 9 | ||
| 9 | 10 | ||
| 10 | 11 | def test_merge_requests(project): | |
@@ -229,11 +230,16 @@ def test_merge_request_should_remove_source_branch(project, merge_request) -> No | |||
| 229 | 230 | # Wait until it is merged | |
| 230 | 231 | mr = None | |
| 231 | 232 | mr_iid = merge_request.iid | |
| 232 | - for _ in range(60): | ||
| 233 | + | ||
| 234 | + def _merge_completed() -> bool: | ||
| 235 | + nonlocal mr | ||
| 233 | 236 | mr = project.mergerequests.get(mr_iid) | |
| 234 | - if mr.merged_at is not None: | ||
| 235 | - break | ||
| 236 | - time.sleep(0.5) | ||
| 237 | + return mr.merged_at is not None | ||
| 238 | + | ||
| 239 | + tests.functional.helpers.poll_until( | ||
| 240 | + condition=_merge_completed, | ||
| 241 | + description=f"merge request {mr_iid} merged_at to be set", | ||
| 242 | + ) | ||
| 237 | 243 | ||
| 238 | 244 | assert mr is not None | |
| 239 | 245 | assert mr.merged_at is not None | |
@@ -271,11 +277,16 @@ def test_merge_request_large_commit_message(project, merge_request) -> None: | |||
| 271 | 277 | # Wait until it is merged | |
| 272 | 278 | mr = None | |
| 273 | 279 | mr_iid = merge_request.iid | |
| 274 | - for _ in range(60): | ||
| 280 | + | ||
| 281 | + def _merge_completed() -> bool: | ||
| 282 | + nonlocal mr | ||
| 275 | 283 | mr = project.mergerequests.get(mr_iid) | |
| 276 | - if mr.merged_at is not None: | ||
| 277 | - break | ||
| 278 | - time.sleep(0.5) | ||
| 284 | + return mr.merged_at is not None | ||
| 285 | + | ||
| 286 | + tests.functional.helpers.poll_until( | ||
| 287 | + condition=_merge_completed, | ||
| 288 | + description=f"merge request {mr_iid} merged_at to be set", | ||
| 289 | + ) | ||
| 279 | 290 | ||
| 280 | 291 | assert mr is not None | |
| 281 | 292 | assert mr.merged_at is not None | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | import datetime | |
| 2 | 2 | import os | |
| 3 | - import time | ||
| 3 | + | ||
| 4 | + import tests.functional.helpers | ||
| 4 | 5 | ||
| 5 | 6 | branch = "BRANCH-cli-v4" | |
| 6 | 7 | ||
@@ -240,8 +241,18 @@ def test_accept_request_merge(gitlab_cli, project): | |||
| 240 | 241 | "commit_message": "chore: test-cli-v4 change", | |
| 241 | 242 | } | |
| 242 | 243 | project.files.create(file_data) | |
| 243 | - # Pause to let GL catch up (happens on hosted too, sometimes takes a while for server to be ready to merge) | ||
| 244 | - time.sleep(30) | ||
| 244 | + | ||
| 245 | + def _merge_status_settled() -> bool: | ||
| 246 | + nonlocal mr | ||
| 247 | + mr = project.mergerequests.get(mr.iid) | ||
| 248 | + return mr.detailed_merge_status not in ("checking", "unchecked") | ||
| 249 | + | ||
| 250 | + # Wait until GitLab has finished evaluating mergeability instead of a | ||
| 251 | + # blind sleep, so the test fails fast if it never becomes mergeable. | ||
| 252 | + tests.functional.helpers.poll_until( | ||
| 253 | + condition=_merge_status_settled, | ||
| 254 | + description=f"merge request {mr.iid} detailed_merge_status to settle", | ||
| 255 | + ) | ||
| 245 | 256 | ||
| 246 | 257 | approve_cmd = [ | |
| 247 | 258 | "project-merge-request", | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -413,19 +413,17 @@ def _make_merge_request(*, source_branch: str, create_pipeline: bool = False): | |||
| 413 | 413 | } | |
| 414 | 414 | ) | |
| 415 | 415 | ||
| 416 | - # Pause to let GL catch up (happens on hosted too, sometimes takes a while for server to be ready to merge) | ||
| 417 | - time.sleep(5) | ||
| 418 | - | ||
| 419 | 416 | mr_iid = mr.iid | |
| 420 | - for _ in range(60): | ||
| 417 | + | ||
| 418 | + def _merge_status_settled() -> bool: | ||
| 419 | + nonlocal mr | ||
| 421 | 420 | mr = project.mergerequests.get(mr_iid) | |
| 422 | - if ( | ||
| 423 | - mr.detailed_merge_status == "checking" | ||
| 424 | - or mr.detailed_merge_status == "unchecked" | ||
| 425 | - ): | ||
| 426 | - time.sleep(0.5) | ||
| 427 | - else: | ||
| 428 | - break | ||
| 421 | + return mr.detailed_merge_status not in ("checking", "unchecked") | ||
| 422 | + | ||
| 423 | + helpers.poll_until( | ||
| 424 | + condition=_merge_status_settled, | ||
| 425 | + description=f"merge request {mr_iid} detailed_merge_status to settle", | ||
| 426 | + ) | ||
| 429 | 427 | ||
| 430 | 428 | assert mr.detailed_merge_status != "checking" | |
| 431 | 429 | assert mr.detailed_merge_status != "unchecked" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | 3 | import logging | |
| 4 | 4 | import time | |
| 5 | - from typing import TYPE_CHECKING | ||
| 5 | + from typing import Callable, TYPE_CHECKING | ||
| 6 | 6 | ||
| 7 | 7 | import pytest | |
| 8 | 8 | ||
@@ -29,6 +29,29 @@ def get_gitlab_plan(gl: gitlab.Gitlab) -> str | None: | |||
| 29 | 29 | return license["plan"] | |
| 30 | 30 | ||
| 31 | 31 | ||
| 32 | + def poll_until( | ||
| 33 | + *, | ||
| 34 | + condition: Callable[[], bool], | ||
| 35 | + description: str, | ||
| 36 | + timeout: float = TIMEOUT, | ||
| 37 | + interval: float = SLEEP_INTERVAL, | ||
| 38 | + ) -> None: | ||
| 39 | + """Repeatedly call `condition` until it returns truthy, sleeping `interval` | ||
| 40 | + seconds between attempts. Fails the test via `pytest.fail` if `timeout` | ||
| 41 | + seconds elapse first, so callers can replace a blind `time.sleep()` "let | ||
| 42 | + GitLab catch up" pause with a check that fails fast when something is | ||
| 43 | + actually wrong instead of always waiting the full duration. | ||
| 44 | + """ | ||
| 45 | + # Use a monotonic deadline rather than counting iterations so the timeout | ||
| 46 | + # is accurate even if `condition()` itself is slow (e.g. a slow API call). | ||
| 47 | + deadline = time.monotonic() + timeout | ||
| 48 | + while time.monotonic() < deadline: | ||
| 49 | + if condition(): | ||
| 50 | + return | ||
| 51 | + time.sleep(interval) | ||
| 52 | + pytest.fail(f"Timed out after {timeout}s waiting for: {description}") | ||
| 53 | + | ||
| 54 | + | ||
| 32 | 55 | def safe_delete(object: gitlab.base.RESTObject) -> None: | |
| 33 | 56 | """Ensure the object specified can not be retrieved. If object still exists after | |
| 34 | 57 | timeout period, fail the test""" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments