|
3 | 3 | import pytest |
4 | 4 |
|
5 | 5 | from taskbadger import StatusEnum |
6 | | -from taskbadger.celery import Task |
| 6 | +from taskbadger._error_context import capture_error_data |
| 7 | +from taskbadger.celery import ( |
| 8 | + TB_ERROR_CTX_TOKEN, |
| 9 | + TB_TASK_ID, |
| 10 | + Task, |
| 11 | + task_failure_handler, |
| 12 | + task_postrun_handler, |
| 13 | + task_prerun_handler, |
| 14 | +) |
| 15 | +from taskbadger.context_providers import ContextProvider |
7 | 16 | from taskbadger.mug import Badger |
8 | 17 | from tests.utils import task_for_test |
9 | 18 |
|
@@ -41,3 +50,103 @@ def add_error(self, a, b): |
41 | 50 | data_kwarg = update.call_args_list[1][1]["data"] |
42 | 51 | assert "Traceback" in data_kwarg["exception"] |
43 | 52 | assert Badger.current.session().client is None |
| 53 | + |
| 54 | + |
| 55 | +class _FakeRequest(dict): |
| 56 | + """Minimal stand-in for Celery's request `Context`: dict-like plus a |
| 57 | + `.headers` attribute, which is all `_get_taskbadger_task_id` needs.""" |
| 58 | + |
| 59 | + headers = None |
| 60 | + |
| 61 | + |
| 62 | +class _FakeEinfo: |
| 63 | + """Minimal stand-in for Celery's `ExceptionInfo`: wraps the exception and |
| 64 | + renders a traceback-shaped string, mirroring what `task_failure`/`task_retry` |
| 65 | + signals actually pass to `_update_task`.""" |
| 66 | + |
| 67 | + def __init__(self, exc): |
| 68 | + self.exception = exc |
| 69 | + |
| 70 | + def __str__(self): |
| 71 | + return f"Traceback (most recent call last):\n{self.exception!r}" |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.usefixtures("_bind_settings") |
| 75 | +def test_signal_handlers_wire_context_provider_snapshot_to_failure(): |
| 76 | + """`task_prerun_handler` snapshots providers and stashes the token on the |
| 77 | + request; `task_failure_handler` should see that exact snapshot when |
| 78 | + building error data; `task_postrun_handler` then resets it.""" |
| 79 | + |
| 80 | + seen_snapshots = [] |
| 81 | + |
| 82 | + class TrackingProvider(ContextProvider): |
| 83 | + identifier = "tracking" |
| 84 | + |
| 85 | + def snapshot(self): |
| 86 | + return "baseline" |
| 87 | + |
| 88 | + def capture_error_context(self, exception, snapshot=None): |
| 89 | + seen_snapshots.append(snapshot) |
| 90 | + return {"snapshot": snapshot} |
| 91 | + |
| 92 | + Badger.current.settings.context_providers = [TrackingProvider()] |
| 93 | + |
| 94 | + sender = mock.Mock() |
| 95 | + sender.request = _FakeRequest({TB_TASK_ID: "tb-1"}) |
| 96 | + task = task_for_test(id="tb-1", status=StatusEnum.PROCESSING) |
| 97 | + sender.taskbadger_task = task |
| 98 | + |
| 99 | + with ( |
| 100 | + mock.patch("taskbadger.celery.safe_get_task", return_value=task), |
| 101 | + mock.patch("taskbadger.celery.update_task_safe", return_value=task) as update, |
| 102 | + mock.patch("taskbadger.celery.enter_session"), |
| 103 | + mock.patch("taskbadger.celery.exit_session"), |
| 104 | + ): |
| 105 | + task_prerun_handler(sender=sender) |
| 106 | + assert TB_ERROR_CTX_TOKEN in sender.request |
| 107 | + |
| 108 | + task_failure_handler(sender=sender, einfo=_FakeEinfo(ValueError("boom"))) |
| 109 | + task_postrun_handler(sender=sender) |
| 110 | + |
| 111 | + # The snapshot seen at failure time is the one taken at prerun, not a |
| 112 | + # missing/None one -- proving the token round-trips through the request. |
| 113 | + assert seen_snapshots == ["baseline"] |
| 114 | + data_kwarg = update.call_args.kwargs["data"] |
| 115 | + assert data_kwarg["tracking"] == {"snapshot": "baseline"} |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.usefixtures("_bind_settings") |
| 119 | +def test_postrun_resets_context_after_error(): |
| 120 | + """Once `task_postrun_handler` runs, a later error in the same thread with |
| 121 | + no provider snapshot taken shouldn't see a stale one left over from the |
| 122 | + previous task.""" |
| 123 | + |
| 124 | + class TrackingProvider(ContextProvider): |
| 125 | + identifier = "tracking" |
| 126 | + |
| 127 | + def snapshot(self): |
| 128 | + return "baseline" |
| 129 | + |
| 130 | + def capture_error_context(self, exception, snapshot=None): |
| 131 | + return {"snapshot": snapshot} |
| 132 | + |
| 133 | + Badger.current.settings.context_providers = [TrackingProvider()] |
| 134 | + |
| 135 | + task = task_for_test(id="tb-2", status=StatusEnum.PROCESSING) |
| 136 | + sender = mock.Mock() |
| 137 | + sender.request = _FakeRequest({TB_TASK_ID: "tb-2"}) |
| 138 | + sender.taskbadger_task = task |
| 139 | + |
| 140 | + with ( |
| 141 | + mock.patch("taskbadger.celery.safe_get_task", return_value=task), |
| 142 | + mock.patch("taskbadger.celery.update_task_safe", return_value=task), |
| 143 | + mock.patch("taskbadger.celery.enter_session"), |
| 144 | + mock.patch("taskbadger.celery.exit_session"), |
| 145 | + ): |
| 146 | + task_prerun_handler(sender=sender) |
| 147 | + task_postrun_handler(sender=sender) |
| 148 | + |
| 149 | + # A failure reported outside of any tracked task's prerun/postrun window |
| 150 | + # (e.g. directly via Task.error) has no snapshot to compare against. |
| 151 | + data = capture_error_data(ValueError("boom")) |
| 152 | + assert data["tracking"] == {"snapshot": None} |
0 commit comments