1
+ import dataclasses
1
2
import tempfile
2
3
import time
3
4
import uuid
@@ -105,25 +106,52 @@ def _check(container):
105
106
return _check
106
107
107
108
109
+ @dataclasses .dataclass
110
+ class WaitSidekiq :
111
+ iterations : int
112
+ step : float
113
+ elapsed_time : float
114
+ success : bool
115
+
116
+
108
117
@pytest .fixture
109
- def wait_for_sidekiq (gl ):
118
+ def wait_for_sidekiq (gl : gitlab . Gitlab ):
110
119
"""
111
120
Return a helper function to wait until there are no busy sidekiq processes.
112
121
113
122
Use this with asserts for slow tasks (group/project/user creation/deletion).
114
123
"""
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.
115
130
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 ):
118
140
time .sleep (step )
119
141
busy = False
120
142
processes = gl .sidekiq .process_metrics ()["processes" ]
121
143
for process in processes :
122
144
if process ["busy" ]:
123
145
busy = True
124
146
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
+ )
127
155
128
156
return _wait
129
157
@@ -210,6 +238,10 @@ def group(gl):
210
238
group .delete ()
211
239
except gitlab .exceptions .GitlabDeleteError as e :
212
240
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 } "
213
245
214
246
215
247
@pytest .fixture (scope = "module" )
@@ -226,6 +258,10 @@ def project(gl):
226
258
project .delete ()
227
259
except gitlab .exceptions .GitlabDeleteError as e :
228
260
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 } "
229
265
230
266
231
267
@pytest .fixture (scope = "function" )
@@ -249,8 +285,10 @@ def _merge_request(*, source_branch: str):
249
285
# NOTE(jlvillal): Sometimes the CI would give a "500 Internal Server
250
286
# Error". Hoping that waiting until all other processes are done will
251
287
# 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 } "
254
292
255
293
project .refresh () # Gets us the current default branch
256
294
project .branches .create (
@@ -274,8 +312,10 @@ def _merge_request(*, source_branch: str):
274
312
"remove_source_branch" : True ,
275
313
}
276
314
)
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 } "
279
319
280
320
mr_iid = mr .iid
281
321
for _ in range (60 ):
@@ -297,6 +337,10 @@ def _merge_request(*, source_branch: str):
297
337
except gitlab .exceptions .GitlabDeleteError :
298
338
# Ignore if branch was already deleted
299
339
pass
340
+ result = wait_for_sidekiq ()
341
+ assert (
342
+ result .success is True
343
+ ), f"sidekiq process should have terminated but did not: { result } "
300
344
301
345
302
346
@pytest .fixture (scope = "module" )
@@ -349,6 +393,10 @@ def user(gl):
349
393
user .delete ()
350
394
except gitlab .exceptions .GitlabDeleteError as e :
351
395
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 } "
352
400
353
401
354
402
@pytest .fixture (scope = "module" )
0 commit comments