Skip to content

Commit

Permalink
Hotfix: New default worker tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nusnus committed Mar 24, 2024
1 parent 79c2b18 commit 8ace44c
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 25 deletions.
110 changes: 110 additions & 0 deletions docs/userguide/default-tasks.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
.. _default-tasks:

================
Built-in Tasks
================

:Release: |version|
:Date: |today|

The plugin provides a list of built-in celery tasks that can be used out of the box. This page will
list all the available tasks.

.. tip::

The tasks injected into the workers that use the default volume with:

.. code-block:: python
volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME},
.. contents::
:local:
:depth: 1

add
===

.. versionadded:: 1.0.0

.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
:language: python
:caption: pytest_celery.vendors.worker.tasks
:start-after: # ------------- add -------------
:end-before: # ------------- add_replaced -------------

add_replaced
============

.. versionadded:: 1.0.0

.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
:language: python
:caption: pytest_celery.vendors.worker.tasks
:start-after: # ------------- add_replaced -------------
:end-before: # ------------- fail -------------

fail
====

.. versionadded:: 1.0.0

.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
:language: python
:caption: pytest_celery.vendors.worker.tasks
:start-after: # ------------- fail -------------
:end-before: # ------------- identity -------------

identity
========

.. versionadded:: 1.0.0

.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
:language: python
:caption: pytest_celery.vendors.worker.tasks
:start-after: # ------------- identity -------------
:end-before: # ------------- noop -------------

noop
====

.. versionadded:: 1.0.0

.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
:language: python
:caption: pytest_celery.vendors.worker.tasks
:start-after: # ------------- noop -------------
:end-before: # ------------- ping -------------

ping
====

.. versionadded:: 1.0.0

.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
:language: python
:caption: pytest_celery.vendors.worker.tasks
:start-after: # ------------- ping -------------
:end-before: # ------------- sleep -------------

sleep
=====

.. versionadded:: 1.0.0

.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
:language: python
:caption: pytest_celery.vendors.worker.tasks
:start-after: # ------------- sleep -------------
:end-before: # ------------- xsum -------------

xsum
====

.. versionadded:: 1.0.0

.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
:language: python
:caption: pytest_celery.vendors.worker.tasks
:start-after: # ------------- xsum -------------
1 change: 1 addition & 0 deletions docs/userguide/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ of the plugin before diving into the advanced features.
app-conf
utils-module
tasks
default-tasks
signals
celery-bug-report
examples/index
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_celery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
from pytest_celery.vendors.worker.fixtures import default_worker_tasks
from pytest_celery.vendors.worker.fixtures import default_worker_utils_module
from pytest_celery.vendors.worker.fixtures import default_worker_volume
from pytest_celery.vendors.worker.tasks import ping
from pytest_celery.vendors.worker.tasks import *

Check warning on line 110 in src/pytest_celery/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/__init__.py#L110

Added line #L110 was not covered by tests
from pytest_celery.vendors.worker.volume import WorkerInitialContent


Expand Down
3 changes: 2 additions & 1 deletion src/pytest_celery/vendors/worker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ ENV PYTHONDONTWRITEBYTECODE=1
RUN pip install --no-cache-dir --upgrade \
pip \
celery[redis,pymemcache]${WORKER_VERSION:+==$WORKER_VERSION} \
pytest-celery@git+https://github.com/celery/pytest-celery.git
pytest-celery@git+https://github.com/Katz-Consulting-Group/pytest-celery.git@hotfix
# pytest-celery@git+https://github.com/celery/pytest-celery.git@hotfix

# The workdir must be /app
WORKDIR /app
Expand Down
139 changes: 139 additions & 0 deletions src/pytest_celery/vendors/worker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,152 @@
This module is part of the :ref:`built-in-worker` vendor.
"""

from __future__ import annotations

Check warning on line 7 in src/pytest_celery/vendors/worker/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/worker/tasks.py#L7

Added line #L7 was not covered by tests

import time
from typing import Any
from typing import Iterable

Check warning on line 11 in src/pytest_celery/vendors/worker/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/worker/tasks.py#L9-L11

Added lines #L9 - L11 were not covered by tests

import celery.utils
from celery import Task

Check warning on line 14 in src/pytest_celery/vendors/worker/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/worker/tasks.py#L13-L14

Added lines #L13 - L14 were not covered by tests
from celery import shared_task


# ------------- add -------------
@shared_task

Check warning on line 19 in src/pytest_celery/vendors/worker/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/worker/tasks.py#L19

Added line #L19 was not covered by tests
def add(x: int | float, y: int | float, z: int | float | None = None) -> int | float:
"""Pytest-celery internal task.
This task adds two or three numbers together.
Args:
x (int | float): The first number.
y (int | float): The second number.
z (int | float | None, optional): The third number. Defaults to None.
Returns:
int | float: The sum of the numbers.
"""
if z:
return x + y + z

Check warning on line 34 in src/pytest_celery/vendors/worker/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/worker/tasks.py#L34

Added line #L34 was not covered by tests
else:
return x + y


# ------------- add_replaced -------------
@shared_task(bind=True)

Check warning on line 40 in src/pytest_celery/vendors/worker/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/worker/tasks.py#L40

Added line #L40 was not covered by tests
def add_replaced(
self: Task,
x: int | float,
y: int | float,
z: int | float | None = None,
*,
queue: str | None = None,
) -> None:
"""Pytest-celery internal task.
This task replaces itself with the add task for the given arguments.
Args:
x (int | float): The first number.
y (int | float): The second number.
z (int | float | None, optional): The third number. Defaults to None.
Raises:
Ignore: Always raises Ignore.
"""
queue = queue or "celery"
raise self.replace(add.s(x, y, z).set(queue=queue))


# ------------- fail -------------
@shared_task

Check warning on line 66 in src/pytest_celery/vendors/worker/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/worker/tasks.py#L66

Added line #L66 was not covered by tests
def fail(*args: tuple) -> None:
"""Pytest-celery internal task.
This task raises a RuntimeError with the given arguments.
Args:
*args (tuple): Arguments to pass to the RuntimeError.
Raises:
RuntimeError: Always raises a RuntimeError.
"""
args = (("Task expected to fail",) + args,)
raise RuntimeError(*args)


# ------------- identity -------------
@shared_task

Check warning on line 83 in src/pytest_celery/vendors/worker/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/worker/tasks.py#L83

Added line #L83 was not covered by tests
def identity(x: Any) -> Any:
"""Pytest-celery internal task.
This task returns the input as is.
Args:
x (Any): Any value.
Returns:
Any: The input value.
"""
return x


# ------------- noop -------------
@shared_task

Check warning on line 99 in src/pytest_celery/vendors/worker/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/worker/tasks.py#L99

Added line #L99 was not covered by tests
def noop(*args: tuple, **kwargs: dict) -> None:
"""Pytest-celery internal task.
This is a no-op task that does nothing.
Returns:
None: Always returns None.
"""
return celery.utils.noop(*args, **kwargs)


# ------------- ping -------------
@shared_task
def ping() -> str:
"""Pytest-celery internal task.
Used to check if the worker is up and running.
Returns:
str: Always returns "pong".
"""
return "pong"


# ------------- sleep -------------
@shared_task

Check warning on line 125 in src/pytest_celery/vendors/worker/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/worker/tasks.py#L125

Added line #L125 was not covered by tests
def sleep(seconds: float = 1, **kwargs: dict) -> bool:
"""Pytest-celery internal task.
This task sleeps for the given number of seconds.
Args:
seconds (float, optional): The number of seconds to sleep. Defaults to 1.
**kwargs (dict): Additional keyword arguments.
Returns:
bool: Always returns True.
"""
time.sleep(seconds, **kwargs)
return True


# ------------- xsum -------------
@shared_task

Check warning on line 143 in src/pytest_celery/vendors/worker/tasks.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/vendors/worker/tasks.py#L143

Added line #L143 was not covered by tests
def xsum(nums: Iterable) -> int:
"""Pytest-celery internal task.
This task sums a list of numbers, but also supports nested lists.
Args:
nums (Iterable): A list of numbers or nested lists.
Returns:
int: The sum of the numbers.
"""
return sum(sum(num) if isinstance(num, Iterable) else num for num in nums)
49 changes: 49 additions & 0 deletions tests/integration/vendors/test_default_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest

from pytest_celery import RESULT_TIMEOUT
from pytest_celery import CeleryTestSetup
from pytest_celery import add
from pytest_celery import add_replaced
from pytest_celery import fail
from pytest_celery import identity
from pytest_celery import noop
from pytest_celery import ping
from pytest_celery import sleep
from pytest_celery import xsum


class test_default_tasks:
def test_add(self, celery_setup: CeleryTestSetup):
assert add.s(1, 2).apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) == 3

def test_add_replaced(self, celery_setup: CeleryTestSetup):
queue = celery_setup.worker.worker_queue
add_replaced.s(1, 2, queue=queue).apply_async(queue=queue)
celery_setup.worker.assert_log_exists("ignored")

def test_fail(self, celery_setup: CeleryTestSetup):
with pytest.raises(RuntimeError):
fail.s().apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT)

def test_identity(self, celery_setup: CeleryTestSetup):
assert identity.s(1).apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) == 1

def test_noop(self, celery_setup: CeleryTestSetup):
assert noop.s().apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) is None

def test_ping(self, celery_setup: CeleryTestSetup):
assert ping.s().apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) == "pong"

def test_sleep(self, celery_setup: CeleryTestSetup):
assert sleep.s().apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) is True

def test_xsum(self, celery_setup: CeleryTestSetup):
assert xsum.s([1, 2, 3]).apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) == 6

def test_xsum_nested_list(self, celery_setup: CeleryTestSetup):
assert (
xsum.s([[1, 2], [3, 4], [5, 6]])
.apply_async(queue=celery_setup.worker.worker_queue)
.get(timeout=RESULT_TIMEOUT)
== 21
)
24 changes: 1 addition & 23 deletions tests/tasks.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
import time

import celery.utils
from celery import Task
from celery import shared_task
from celery import signature
from celery.canvas import Signature


@shared_task
def noop(*args, **kwargs) -> None:
return celery.utils.noop(*args, **kwargs)


@shared_task
def identity(x):
return x


@shared_task
def sleep(seconds: float = 1, **kwargs) -> True:
time.sleep(seconds, **kwargs)
return True


@shared_task
def add(x, y):
return x + y
from pytest_celery.vendors.worker.tasks import * # noqa


@shared_task
Expand Down
Loading

0 comments on commit 8ace44c

Please sign in to comment.