Skip to content

Commit

Permalink
Improve schedule test
Browse files Browse the repository at this point in the history
[noissue]
  • Loading branch information
mdellweg committed Jul 27, 2023
1 parent 69d8c02 commit 33ed9a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
8 changes: 6 additions & 2 deletions pulpcore/app/serializers/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,13 @@ class TaskScheduleSerializer(ModelSerializer):
pulp_href = IdentityField(view_name="task-schedules-detail")
name = serializers.CharField(help_text=_("The name of the task schedule."), allow_blank=False)
task_name = serializers.CharField(help_text=_("The name of the task to be scheduled."))
dispatch_interval = serializers.DurationField(help_text=_("Periodicity of the schedule."))
dispatch_interval = serializers.DurationField(
help_text=_("Periodicity of the schedule."), allow_null=True
)
next_dispatch = serializers.DateTimeField(
help_text=_("Timestamp of the next time the task will be dispatched."), read_only=True
help_text=_("Timestamp of the next time the task will be dispatched."),
read_only=True,
allow_null=True,
)
last_task = RelatedField(
help_text=_("The last task dispatched by this schedule."),
Expand Down
30 changes: 19 additions & 11 deletions pulpcore/tests/functional/api/test_workers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests related to the workers."""
import pytest
import subprocess
import uuid
from datetime import datetime, timedelta
from random import choice
from time import sleep
Expand Down Expand Up @@ -67,16 +68,17 @@ def test_worker_actions(workers_api_client):
workers_api_client.delete(chosen_worker.pulp_href)


@pytest.fixture
def task_schedule():
name = "test_schedule"
@pytest.fixture(params=[None, 100])
def task_schedule(request):
interval = request.param
name = str(uuid.uuid4())
task_name = "pulpcore.app.tasks.test.dummy_task"
schedule_commands = (
"from django.utils.timezone import now;"
"from datetime import timedelta;"
"from pulpcore.app.models import TaskSchedule;"
"dispatch_interval = timedelta(seconds=4);"
"next_dispatch = now() + dispatch_interval;"
f"dispatch_interval = {interval} and timedelta({interval});"
"next_dispatch = now() + timedelta(seconds=5);"
"TaskSchedule("
f" name='{name}', task_name='{task_name}', "
" dispatch_interval=dispatch_interval, next_dispatch=next_dispatch"
Expand All @@ -85,7 +87,7 @@ def task_schedule():
process = subprocess.run(["pulpcore-manager", "shell", "-c", schedule_commands])
assert process.returncode == 0

yield {"name": name, "task_name": task_name}
yield {"name": name, "task_name": task_name, "interval": interval}

unschedule_commands = (
"from pulpcore.app.models import TaskSchedule;"
Expand All @@ -99,19 +101,25 @@ def task_schedule():
def test_task_schedule(task_schedule, task_schedules_api_client):
"""Test that a worker will schedule a task roughly at a given time."""
# Worker TTL is configured to 30s, therefore they will have a heartbeat each 10s (6 bpm). The
# task is scheduled 4s in the future to give us time to invesitgate the state before and after.
# 15s later we can be sure it was scheduled (as long as at least one worker is running).
# task is scheduled 5s in the future to give us time to invesitgate the state before and after.
# 16s later we can be sure it was scheduled (as long as at least one worker is running).

result = task_schedules_api_client.list(name=task_schedule["name"])
assert result.count == 1
ts = task_schedules_api_client.read(task_schedule_href=result.results[0].pulp_href)
ts = result.results[0]
assert ts.name == task_schedule["name"]
assert ts.task_name == task_schedule["task_name"]
assert ts.last_task is None
# At least a worker heartbeat is needed
for i in range(15):
for i in range(16):
sleep(1)
ts = task_schedules_api_client.read(task_schedule_href=result.results[0].pulp_href)
ts = task_schedules_api_client.read(task_schedule_href=ts.pulp_href)
if ts.last_task is not None:
break
assert ts.last_task is not None
if task_schedule["interval"] is None:
assert ts.dispatch_interval is None
assert ts.next_dispatch is None
else:
assert ts.dispatch_interval is not None
assert ts.next_dispatch is not None

0 comments on commit 33ed9a7

Please sign in to comment.