Skip to content

Commit b0ebbea

Browse files
committed
refactor flaky test
1 parent 2344e97 commit b0ebbea

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

t/unit/test_schedulers.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,45 @@ def test_constructor(self):
550550
def test_all_as_schedule(self):
551551
sched = self.s.schedule
552552
assert sched
553-
assert len(sched) == 9
554-
assert 'celery.backend_cleanup' in sched
553+
554+
# Check for presence of standard tasks
555+
expected_task_names = {
556+
self.m1.name, # interval task
557+
self.m2.name, # interval task
558+
self.m3.name, # crontab task
559+
self.m4.name, # solar task
560+
self.m6.name, # clocked task (near future)
561+
self.m8.name, # crontab task (current hour)
562+
self.m9.name, # crontab task (current hour + 1)
563+
self.m10.name, # crontab task (current hour - 1)
564+
'celery.backend_cleanup' # auto-added by system
565+
}
566+
567+
# The distant future crontab task (hour + 3) should be excluded
568+
distant_task_name = self.m11.name
569+
570+
# But if it's hour is 4 (or converts to 4 after timezone adjustment),
571+
# it would be included because of the special handling for hour=4
572+
current_hour = timezone.localtime(timezone.now()).hour
573+
is_hour_four_task = False
574+
575+
# Check if the task would have hour 4.
576+
if self.m11.crontab.hour == '4'\
577+
or (current_hour + 3) % 24 == 4:
578+
is_hour_four_task = True
579+
580+
# Add to expected tasks if it's an hour=4 task
581+
if is_hour_four_task:
582+
expected_task_names.add(distant_task_name)
583+
584+
# Verify all expected tasks are present in the schedule
585+
schedule_task_names = set(sched.keys())
586+
assert schedule_task_names == expected_task_names, (
587+
f"Task mismatch. Expected: {expected_task_names}, "
588+
f"Got: {schedule_task_names}"
589+
)
590+
591+
# Verify all entries are the right type
555592
for n, e in sched.items():
556593
assert isinstance(e, self.s.Entry)
557594

@@ -1219,7 +1256,7 @@ def test_SolarSchedule_schedule(self):
12191256
isdue, nextcheck = s.schedule.is_due(dt_lastrun)
12201257
assert isdue is False # False means task isn't due, but keep checking.
12211258
assert (nextcheck > 0) and (isdue is False) or \
1222-
(nextcheck == s.max_interval) and (isdue is True)
1259+
(nextcheck == s.max_interval) and (isdue is True)
12231260

12241261
s2 = SolarSchedule(event='solar_noon', latitude=48.06, longitude=12.86)
12251262
dt2 = datetime(day=26, month=7, year=2000, hour=1, minute=0)
@@ -1229,7 +1266,7 @@ def test_SolarSchedule_schedule(self):
12291266
isdue2, nextcheck2 = s2.schedule.is_due(dt2_lastrun)
12301267
assert isdue2 is True # True means task is due and should run.
12311268
assert (nextcheck2 > 0) and (isdue2 is True) or \
1232-
(nextcheck2 == s2.max_interval) and (isdue2 is False)
1269+
(nextcheck2 == s2.max_interval) and (isdue2 is False)
12331270

12341271
def test_ClockedSchedule_schedule(self):
12351272
due_datetime = make_aware(datetime(
@@ -1248,7 +1285,7 @@ def test_ClockedSchedule_schedule(self):
12481285
# False means task isn't due, but keep checking.
12491286
assert isdue is False
12501287
assert (nextcheck > 0) and (isdue is False) or \
1251-
(nextcheck == s.max_interval) and (isdue is True)
1288+
(nextcheck == s.max_interval) and (isdue is True)
12521289

12531290
due_datetime = make_aware(datetime.now())
12541291
s = ClockedSchedule(clocked_time=due_datetime)

0 commit comments

Comments
 (0)