|
4 | 4 | This module is part of the :ref:`built-in-worker` vendor. |
5 | 5 | """ |
6 | 6 |
|
| 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 |
7 | 15 | from celery import shared_task |
8 | 16 |
|
9 | 17 |
|
| 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 ------------- |
10 | 112 | @shared_task |
11 | 113 | def ping() -> str: |
12 | 114 | """Pytest-celery internal task. |
13 | 115 |
|
14 | 116 | Used to check if the worker is up and running. |
| 117 | +
|
| 118 | + Returns: |
| 119 | + str: Always returns "pong". |
15 | 120 | """ |
16 | 121 | 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) |
0 commit comments