Skip to content

Commit a5d633e

Browse files
authored
Hotfix: New default worker tasks (#259)
* Updated pypi classifiers * Hotfix: New default worker tasks
1 parent 546e9c1 commit a5d633e

File tree

9 files changed

+349
-26
lines changed

9 files changed

+349
-26
lines changed

docs/userguide/default-tasks.rst

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
.. _default-tasks:
2+
3+
================
4+
Built-in Tasks
5+
================
6+
7+
:Release: |version|
8+
:Date: |today|
9+
10+
The plugin provides a list of built-in celery tasks that can be used out of the box. This page will
11+
list all the available tasks.
12+
13+
.. tip::
14+
15+
The tasks injected into the workers that use the default volume with:
16+
17+
.. code-block:: python
18+
19+
volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME},
20+
21+
.. contents::
22+
:local:
23+
:depth: 1
24+
25+
add
26+
===
27+
28+
.. versionadded:: 1.0.0
29+
30+
.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
31+
:language: python
32+
:caption: pytest_celery.vendors.worker.tasks
33+
:start-after: # ------------- add -------------
34+
:end-before: # ------------- add_replaced -------------
35+
36+
add_replaced
37+
============
38+
39+
.. versionadded:: 1.0.0
40+
41+
.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
42+
:language: python
43+
:caption: pytest_celery.vendors.worker.tasks
44+
:start-after: # ------------- add_replaced -------------
45+
:end-before: # ------------- fail -------------
46+
47+
fail
48+
====
49+
50+
.. versionadded:: 1.0.0
51+
52+
.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
53+
:language: python
54+
:caption: pytest_celery.vendors.worker.tasks
55+
:start-after: # ------------- fail -------------
56+
:end-before: # ------------- identity -------------
57+
58+
identity
59+
========
60+
61+
.. versionadded:: 1.0.0
62+
63+
.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
64+
:language: python
65+
:caption: pytest_celery.vendors.worker.tasks
66+
:start-after: # ------------- identity -------------
67+
:end-before: # ------------- noop -------------
68+
69+
noop
70+
====
71+
72+
.. versionadded:: 1.0.0
73+
74+
.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
75+
:language: python
76+
:caption: pytest_celery.vendors.worker.tasks
77+
:start-after: # ------------- noop -------------
78+
:end-before: # ------------- ping -------------
79+
80+
ping
81+
====
82+
83+
.. versionadded:: 1.0.0
84+
85+
.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
86+
:language: python
87+
:caption: pytest_celery.vendors.worker.tasks
88+
:start-after: # ------------- ping -------------
89+
:end-before: # ------------- sleep -------------
90+
91+
sleep
92+
=====
93+
94+
.. versionadded:: 1.0.0
95+
96+
.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
97+
:language: python
98+
:caption: pytest_celery.vendors.worker.tasks
99+
:start-after: # ------------- sleep -------------
100+
:end-before: # ------------- xsum -------------
101+
102+
xsum
103+
====
104+
105+
.. versionadded:: 1.0.0
106+
107+
.. literalinclude:: ../../src/pytest_celery/vendors/worker/tasks.py
108+
:language: python
109+
:caption: pytest_celery.vendors.worker.tasks
110+
:start-after: # ------------- xsum -------------

docs/userguide/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ of the plugin before diving into the advanced features.
1919
app-conf
2020
utils-module
2121
tasks
22+
default-tasks
2223
signals
2324
celery-bug-report
2425
examples/index

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ authors = [
4242
"Thomas Grainger <[email protected]>",
4343
]
4444
classifiers = [
45-
"Development Status :: 3 - Alpha",
45+
"Development Status :: 4 - Beta",
4646
"License :: OSI Approved :: BSD License",
4747
"Topic :: Software Development :: Testing",
4848
"Framework :: Celery",

src/pytest_celery/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
from pytest_celery.vendors.worker.fixtures import default_worker_tasks
108108
from pytest_celery.vendors.worker.fixtures import default_worker_utils_module
109109
from pytest_celery.vendors.worker.fixtures import default_worker_volume
110-
from pytest_celery.vendors.worker.tasks import ping
110+
from pytest_celery.vendors.worker.tasks import *
111111
from pytest_celery.vendors.worker.volume import WorkerInitialContent
112112

113113

src/pytest_celery/vendors/worker/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ ENV PYTHONDONTWRITEBYTECODE=1
2323
RUN pip install --no-cache-dir --upgrade \
2424
pip \
2525
celery[redis,pymemcache]${WORKER_VERSION:+==$WORKER_VERSION} \
26-
pytest-celery@git+https://github.com/celery/pytest-celery.git
26+
pytest-celery@git+https://github.com/Katz-Consulting-Group/pytest-celery.git@hotfix
27+
# pytest-celery@git+https://github.com/celery/pytest-celery.git@hotfix
2728

2829
# The workdir must be /app
2930
WORKDIR /app

src/pytest_celery/vendors/worker/tasks.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,152 @@
44
This module is part of the :ref:`built-in-worker` vendor.
55
"""
66

7+
from __future__ import annotations
8+
9+
import time
10+
from typing import Any
11+
from typing import Iterable
12+
13+
import celery.utils
14+
from celery import Task
715
from celery import shared_task
816

917

18+
# ------------- add -------------
19+
@shared_task
20+
def add(x: int | float, y: int | float, z: int | float | None = None) -> int | float:
21+
"""Pytest-celery internal task.
22+
23+
This task adds two or three numbers together.
24+
25+
Args:
26+
x (int | float): The first number.
27+
y (int | float): The second number.
28+
z (int | float | None, optional): The third number. Defaults to None.
29+
30+
Returns:
31+
int | float: The sum of the numbers.
32+
"""
33+
if z:
34+
return x + y + z
35+
else:
36+
return x + y
37+
38+
39+
# ------------- add_replaced -------------
40+
@shared_task(bind=True)
41+
def add_replaced(
42+
self: Task,
43+
x: int | float,
44+
y: int | float,
45+
z: int | float | None = None,
46+
*,
47+
queue: str | None = None,
48+
) -> None:
49+
"""Pytest-celery internal task.
50+
51+
This task replaces itself with the add task for the given arguments.
52+
53+
Args:
54+
x (int | float): The first number.
55+
y (int | float): The second number.
56+
z (int | float | None, optional): The third number. Defaults to None.
57+
58+
Raises:
59+
Ignore: Always raises Ignore.
60+
"""
61+
queue = queue or "celery"
62+
raise self.replace(add.s(x, y, z).set(queue=queue))
63+
64+
65+
# ------------- fail -------------
66+
@shared_task
67+
def fail(*args: tuple) -> None:
68+
"""Pytest-celery internal task.
69+
70+
This task raises a RuntimeError with the given arguments.
71+
72+
Args:
73+
*args (tuple): Arguments to pass to the RuntimeError.
74+
75+
Raises:
76+
RuntimeError: Always raises a RuntimeError.
77+
"""
78+
args = (("Task expected to fail",) + args,)
79+
raise RuntimeError(*args)
80+
81+
82+
# ------------- identity -------------
83+
@shared_task
84+
def identity(x: Any) -> Any:
85+
"""Pytest-celery internal task.
86+
87+
This task returns the input as is.
88+
89+
Args:
90+
x (Any): Any value.
91+
92+
Returns:
93+
Any: The input value.
94+
"""
95+
return x
96+
97+
98+
# ------------- noop -------------
99+
@shared_task
100+
def noop(*args: tuple, **kwargs: dict) -> None:
101+
"""Pytest-celery internal task.
102+
103+
This is a no-op task that does nothing.
104+
105+
Returns:
106+
None: Always returns None.
107+
"""
108+
return celery.utils.noop(*args, **kwargs)
109+
110+
111+
# ------------- ping -------------
10112
@shared_task
11113
def ping() -> str:
12114
"""Pytest-celery internal task.
13115
14116
Used to check if the worker is up and running.
117+
118+
Returns:
119+
str: Always returns "pong".
15120
"""
16121
return "pong"
122+
123+
124+
# ------------- sleep -------------
125+
@shared_task
126+
def sleep(seconds: float = 1, **kwargs: dict) -> bool:
127+
"""Pytest-celery internal task.
128+
129+
This task sleeps for the given number of seconds.
130+
131+
Args:
132+
seconds (float, optional): The number of seconds to sleep. Defaults to 1.
133+
**kwargs (dict): Additional keyword arguments.
134+
135+
Returns:
136+
bool: Always returns True.
137+
"""
138+
time.sleep(seconds, **kwargs)
139+
return True
140+
141+
142+
# ------------- xsum -------------
143+
@shared_task
144+
def xsum(nums: Iterable) -> int:
145+
"""Pytest-celery internal task.
146+
147+
This task sums a list of numbers, but also supports nested lists.
148+
149+
Args:
150+
nums (Iterable): A list of numbers or nested lists.
151+
152+
Returns:
153+
int: The sum of the numbers.
154+
"""
155+
return sum(sum(num) if isinstance(num, Iterable) else num for num in nums)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pytest
2+
3+
from pytest_celery import RESULT_TIMEOUT
4+
from pytest_celery import CeleryTestSetup
5+
from pytest_celery import add
6+
from pytest_celery import add_replaced
7+
from pytest_celery import fail
8+
from pytest_celery import identity
9+
from pytest_celery import noop
10+
from pytest_celery import ping
11+
from pytest_celery import sleep
12+
from pytest_celery import xsum
13+
14+
15+
class test_default_tasks:
16+
def test_add(self, celery_setup: CeleryTestSetup):
17+
assert add.s(1, 2).apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) == 3
18+
19+
def test_add_replaced(self, celery_setup: CeleryTestSetup):
20+
queue = celery_setup.worker.worker_queue
21+
add_replaced.s(1, 2, queue=queue).apply_async(queue=queue)
22+
celery_setup.worker.assert_log_exists("ignored")
23+
24+
def test_fail(self, celery_setup: CeleryTestSetup):
25+
with pytest.raises(RuntimeError):
26+
fail.s().apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT)
27+
28+
def test_identity(self, celery_setup: CeleryTestSetup):
29+
assert identity.s(1).apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) == 1
30+
31+
def test_noop(self, celery_setup: CeleryTestSetup):
32+
assert noop.s().apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) is None
33+
34+
def test_ping(self, celery_setup: CeleryTestSetup):
35+
assert ping.s().apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) == "pong"
36+
37+
def test_sleep(self, celery_setup: CeleryTestSetup):
38+
assert sleep.s().apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) is True
39+
40+
def test_xsum(self, celery_setup: CeleryTestSetup):
41+
assert xsum.s([1, 2, 3]).apply_async(queue=celery_setup.worker.worker_queue).get(timeout=RESULT_TIMEOUT) == 6
42+
43+
def test_xsum_nested_list(self, celery_setup: CeleryTestSetup):
44+
assert (
45+
xsum.s([[1, 2], [3, 4], [5, 6]])
46+
.apply_async(queue=celery_setup.worker.worker_queue)
47+
.get(timeout=RESULT_TIMEOUT)
48+
== 21
49+
)

tests/tasks.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,9 @@
1-
import time
2-
3-
import celery.utils
41
from celery import Task
52
from celery import shared_task
63
from celery import signature
74
from celery.canvas import Signature
85

9-
10-
@shared_task
11-
def noop(*args, **kwargs) -> None:
12-
return celery.utils.noop(*args, **kwargs)
13-
14-
15-
@shared_task
16-
def identity(x):
17-
return x
18-
19-
20-
@shared_task
21-
def sleep(seconds: float = 1, **kwargs) -> True:
22-
time.sleep(seconds, **kwargs)
23-
return True
24-
25-
26-
@shared_task
27-
def add(x, y):
28-
return x + y
6+
from pytest_celery.vendors.worker.tasks import * # noqa
297

308

319
@shared_task

0 commit comments

Comments
 (0)