Skip to content

Commit abfcb9e

Browse files
chore: update 'wait_for_sidekiq' function & use it more
Make the `timeout` argument in `wait_for_sidekiq` be approximately the seconds desired. Previously it was an iteration count so depending on what the `step` argument was set to the time before the function would time-out would vary. Simpler for users to treat `timeout` as the (approximate) number of seconds before `wait_for_sidekiq` will timeout. Return new `WaitSidekiq` object which contains statistics from the `wait_for_sidekiq` run. Use `wait_for_sidekiq` in our fixtures defined in `conftest.py` after they delete resources. This will help increase the odds that we don't have stray resources left-around when the next test starts. On my local system the run-time of py_func_v4 went from 7m50s to 7m59s.
1 parent d65ce36 commit abfcb9e

File tree

4 files changed

+79
-19
lines changed

4 files changed

+79
-19
lines changed

tests/functional/api/test_merge_requests.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ def test_merge_request_should_remove_source_branch(
113113

114114
mr.merge(should_remove_source_branch=True)
115115

116-
result = wait_for_sidekiq(timeout=60)
117-
assert result is True, "sidekiq process should have terminated but did not"
116+
result = wait_for_sidekiq()
117+
assert (
118+
result.success is True
119+
), f"sidekiq process should have terminated but did not: {result}"
118120

119121
# Wait until it is merged
120122
mr_iid = mr.iid
@@ -125,8 +127,10 @@ def test_merge_request_should_remove_source_branch(
125127
time.sleep(0.5)
126128
assert mr.merged_at is not None
127129
time.sleep(0.5)
128-
result = wait_for_sidekiq(timeout=60)
129-
assert result is True, "sidekiq process should have terminated but did not"
130+
result = wait_for_sidekiq()
131+
assert (
132+
result.success is True
133+
), f"sidekiq process should have terminated but did not: {result}"
130134

131135
# Ensure we can NOT get the MR branch
132136
with pytest.raises(gitlab.exceptions.GitlabGetError):
@@ -156,8 +160,10 @@ def test_merge_request_large_commit_message(
156160

157161
mr.merge(merge_commit_message=merge_commit_message)
158162

159-
result = wait_for_sidekiq(timeout=60)
160-
assert result is True, "sidekiq process should have terminated but did not"
163+
result = wait_for_sidekiq()
164+
assert (
165+
result.success is True
166+
), f"sidekiq process should have terminated but did not: {result}"
161167

162168
# Wait until it is merged
163169
mr_iid = mr.iid
@@ -196,8 +202,10 @@ def test_merge_request_merge_ref_should_fail(
196202
"commit_message": "Another commit in main branch",
197203
}
198204
)
199-
result = wait_for_sidekiq(timeout=60)
200-
assert result is True, "sidekiq process should have terminated but did not"
205+
result = wait_for_sidekiq()
206+
assert (
207+
result.success is True
208+
), f"sidekiq process should have terminated but did not: {result}"
201209

202210
# Check for non-existing merge_ref for MR with conflicts
203211
with pytest.raises(gitlab.exceptions.GitlabGetError):

tests/functional/api/test_projects.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,5 @@ def test_project_groups_list(gl, group):
329329
groups = project.groups.list()
330330
group_ids = set([x.id for x in groups])
331331
assert set((group.id, group2.id)) == group_ids
332+
# NOTE: `group` will automatically be deleted by the fixture, but not `group2`
333+
group2.delete()

tests/functional/api/test_users.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ def test_delete_user(gl, wait_for_sidekiq):
4747
)
4848

4949
new_user.delete()
50-
result = wait_for_sidekiq(timeout=60)
51-
assert result is True, "sidekiq process should have terminated but did not"
50+
result = wait_for_sidekiq()
51+
assert (
52+
result.success is True
53+
), f"sidekiq process should have terminated but did not: {result}"
5254

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

tests/functional/conftest.py

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import dataclasses
12
import tempfile
23
import time
34
import uuid
@@ -105,25 +106,52 @@ def _check(container):
105106
return _check
106107

107108

109+
@dataclasses.dataclass
110+
class WaitSidekiq:
111+
iterations: int
112+
step: float
113+
elapsed_time: float
114+
success: bool
115+
116+
108117
@pytest.fixture
109-
def wait_for_sidekiq(gl):
118+
def wait_for_sidekiq(gl: gitlab.Gitlab):
110119
"""
111120
Return a helper function to wait until there are no busy sidekiq processes.
112121
113122
Use this with asserts for slow tasks (group/project/user creation/deletion).
114123
"""
124+
return _wait_for_sidekiq(gl=gl)
125+
126+
127+
def _wait_for_sidekiq(gl: gitlab.Gitlab):
128+
"""
129+
Return a helper function to wait until there are no busy sidekiq processes.
115130
116-
def _wait(timeout=30, step=0.5):
117-
for _ in range(timeout):
131+
Use this with asserts for slow tasks (group/project/user creation/deletion).
132+
"""
133+
134+
def _wait(timeout: int = 60, step: float = 0.1) -> WaitSidekiq:
135+
# timeout is approximately the timeout in seconds
136+
max_iterations = int(timeout / step)
137+
start_time = time.perf_counter()
138+
success = False
139+
for count in range(max_iterations):
118140
time.sleep(step)
119141
busy = False
120142
processes = gl.sidekiq.process_metrics()["processes"]
121143
for process in processes:
122144
if process["busy"]:
123145
busy = True
124146
if not busy:
125-
return True
126-
return False
147+
success = True
148+
break
149+
return WaitSidekiq(
150+
iterations=count,
151+
step=step,
152+
elapsed_time=time.perf_counter() - start_time,
153+
success=success,
154+
)
127155

128156
return _wait
129157

@@ -210,6 +238,10 @@ def group(gl):
210238
group.delete()
211239
except gitlab.exceptions.GitlabDeleteError as e:
212240
print(f"Group already deleted: {e}")
241+
result = _wait_for_sidekiq(gl=gl)()
242+
assert (
243+
result.success is True
244+
), f"sidekiq process should have terminated but did not: {result}"
213245

214246

215247
@pytest.fixture(scope="module")
@@ -226,6 +258,10 @@ def project(gl):
226258
project.delete()
227259
except gitlab.exceptions.GitlabDeleteError as e:
228260
print(f"Project already deleted: {e}")
261+
result = _wait_for_sidekiq(gl=gl)()
262+
assert (
263+
result.success is True
264+
), f"sidekiq process should have terminated but did not: {result}"
229265

230266

231267
@pytest.fixture(scope="function")
@@ -249,8 +285,10 @@ def _merge_request(*, source_branch: str):
249285
# NOTE(jlvillal): Sometimes the CI would give a "500 Internal Server
250286
# Error". Hoping that waiting until all other processes are done will
251287
# help with that.
252-
result = wait_for_sidekiq(timeout=60)
253-
assert result is True, "sidekiq process should have terminated but did not"
288+
result = wait_for_sidekiq()
289+
assert (
290+
result.success is True
291+
), f"sidekiq process should have terminated but did not: {result}"
254292

255293
project.refresh() # Gets us the current default branch
256294
project.branches.create(
@@ -274,8 +312,10 @@ def _merge_request(*, source_branch: str):
274312
"remove_source_branch": True,
275313
}
276314
)
277-
result = wait_for_sidekiq(timeout=60)
278-
assert result is True, "sidekiq process should have terminated but did not"
315+
result = wait_for_sidekiq()
316+
assert (
317+
result.success is True
318+
), f"sidekiq process should have terminated but did not: {result}"
279319

280320
mr_iid = mr.iid
281321
for _ in range(60):
@@ -297,6 +337,10 @@ def _merge_request(*, source_branch: str):
297337
except gitlab.exceptions.GitlabDeleteError:
298338
# Ignore if branch was already deleted
299339
pass
340+
result = wait_for_sidekiq()
341+
assert (
342+
result.success is True
343+
), f"sidekiq process should have terminated but did not: {result}"
300344

301345

302346
@pytest.fixture(scope="module")
@@ -349,6 +393,10 @@ def user(gl):
349393
user.delete()
350394
except gitlab.exceptions.GitlabDeleteError as e:
351395
print(f"User already deleted: {e}")
396+
result = _wait_for_sidekiq(gl=gl)()
397+
assert (
398+
result.success is True
399+
), f"sidekiq process should have terminated but did not: {result}"
352400

353401

354402
@pytest.fixture(scope="module")

0 commit comments

Comments
 (0)