| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
import asyncio |
| 29 |
import logging |
| 30 |
from unittest import mock |
| 31 |
|
| 32 |
import pytest |
| 33 |
|
| 34 |
from apprise import Apprise, AppriseAsset |
| 35 |
from apprise.common import ( |
| 36 |
APPRISE_MAX_SERVICE_RETRY, |
| 37 |
APPRISE_MAX_SERVICE_WAIT, |
| 38 |
MATCH_ALL_TAG, |
| 39 |
) |
| 40 |
from apprise.config.base import ConfigBase |
| 41 |
from apprise.manager_plugins import NotificationManager |
| 42 |
from apprise.plugins import NotifyBase |
| 43 |
from apprise.tag import AppriseTag |
| 44 |
|
| 45 |
logging.disable(logging.CRITICAL) |
| 46 |
|
| 47 |
N_MGR = NotificationManager() |
| 48 |
|
| 49 |
|
| 50 |
class _TestNotify(NotifyBase): |
| 51 |
"""Test notification plugin -- controlled pass/fail via side_effect.""" |
| 52 |
|
| 53 |
app_id = "TestApp" |
| 54 |
app_desc = "Test" |
| 55 |
notify_url = "test://" |
| 56 |
|
| 57 |
title_maxlen = 250 |
| 58 |
body_maxlen = 32768 |
| 59 |
|
| 60 |
|
| 61 |
def url(self, privacy=False, *args, **kwargs): |
| 62 |
return "test://localhost" |
| 63 |
|
| 64 |
def send(self, **kwargs): |
| 65 |
return True |
| 66 |
|
| 67 |
@staticmethod |
| 68 |
def parse_url(url): |
| 69 |
results = NotifyBase.parse_url(url, verify_host=False) |
| 70 |
return results |
| 71 |
|
| 72 |
|
| 73 |
class TestWaitValidation: |
| 74 |
"""Tests for the wait= parameter accepted by NotifyBase.""" |
| 75 |
|
| 76 |
def _make(self, **kwargs): |
| 77 |
return _TestNotify(host="localhost", **kwargs) |
| 78 |
|
| 79 |
@pytest.mark.parametrize( |
| 80 |
"value,expected", |
| 81 |
[ |
| 82 |
(0, 0.0), |
| 83 |
(0.0, 0.0), |
| 84 |
(0.5, 0.5), |
| 85 |
(1, 1.0), |
| 86 |
(1.0, 1.0), |
| 87 |
("0.5", 0.5), |
| 88 |
("1", 1.0), |
| 89 |
("1.4", 1.4), |
| 90 |
("20", 20.0), |
| 91 |
(APPRISE_MAX_SERVICE_WAIT, APPRISE_MAX_SERVICE_WAIT), |
| 92 |
], |
| 93 |
) |
| 94 |
def test_valid_wait(self, value, expected): |
| 95 |
nb = self._make(wait=value) |
| 96 |
assert nb.wait == pytest.approx(expected) |
| 97 |
|
| 98 |
def test_wait_clamped_to_max(self): |
| 99 |
|
| 100 |
nb = self._make(wait=APPRISE_MAX_SERVICE_WAIT + 9999) |
| 101 |
assert nb.wait == pytest.approx(APPRISE_MAX_SERVICE_WAIT) |
| 102 |
|
| 103 |
@pytest.mark.parametrize( |
| 104 |
"bad_value", |
| 105 |
[ |
| 106 |
"-3", |
| 107 |
"-0.1", |
| 108 |
"1.3.3.", |
| 109 |
"abc", |
| 110 |
"inf", |
| 111 |
"-inf", |
| 112 |
"nan", |
| 113 |
None, |
| 114 |
], |
| 115 |
) |
| 116 |
def test_invalid_wait_uses_asset_default(self, bad_value): |
| 117 |
if bad_value is None: |
| 118 |
|
| 119 |
asset = AppriseAsset(default_service_wait=1.25) |
| 120 |
nb = _TestNotify(host="localhost", asset=asset) |
| 121 |
assert nb.wait == pytest.approx(1.25) |
| 122 |
return |
| 123 |
|
| 124 |
asset = AppriseAsset(default_service_wait=0.75) |
| 125 |
nb = self._make(wait=bad_value, asset=asset) |
| 126 |
|
| 127 |
assert nb.wait == pytest.approx(0.75) |
| 128 |
|
| 129 |
def test_negative_wait_falls_back(self): |
| 130 |
asset = AppriseAsset(default_service_wait=2.0) |
| 131 |
nb = self._make(wait=-1.0, asset=asset) |
| 132 |
assert nb.wait == pytest.approx(2.0) |
| 133 |
|
| 134 |
def test_asset_default_zero(self): |
| 135 |
asset = AppriseAsset(default_service_wait=0.0) |
| 136 |
nb = _TestNotify(host="localhost", asset=asset) |
| 137 |
assert nb.wait == pytest.approx(0.0) |
| 138 |
|
| 139 |
def test_asset_default_clamped_to_max(self): |
| 140 |
asset = AppriseAsset(default_service_wait=APPRISE_MAX_SERVICE_WAIT + 1) |
| 141 |
nb = _TestNotify(host="localhost", asset=asset) |
| 142 |
assert nb.wait == pytest.approx(APPRISE_MAX_SERVICE_WAIT) |
| 143 |
|
| 144 |
|
| 145 |
class TestRetryValidation: |
| 146 |
"""Sanity checks for the retry= parameter.""" |
| 147 |
|
| 148 |
def _make(self, **kwargs): |
| 149 |
return _TestNotify(host="localhost", **kwargs) |
| 150 |
|
| 151 |
@pytest.mark.parametrize( |
| 152 |
"value,expected", |
| 153 |
[ |
| 154 |
(0, 0), |
| 155 |
(1, 1), |
| 156 |
("3", 3), |
| 157 |
(APPRISE_MAX_SERVICE_RETRY, APPRISE_MAX_SERVICE_RETRY), |
| 158 |
], |
| 159 |
) |
| 160 |
def test_valid_retry(self, value, expected): |
| 161 |
nb = self._make(retry=value) |
| 162 |
assert nb.retry == expected |
| 163 |
|
| 164 |
def test_retry_clamped_to_max(self): |
| 165 |
nb = self._make(retry=APPRISE_MAX_SERVICE_RETRY + 999) |
| 166 |
assert nb.retry == APPRISE_MAX_SERVICE_RETRY |
| 167 |
|
| 168 |
@pytest.mark.parametrize("bad", ["-1", "abc", "1.3.3."]) |
| 169 |
def test_invalid_retry_uses_asset_default(self, bad): |
| 170 |
asset = AppriseAsset(default_service_retry=2) |
| 171 |
nb = self._make(retry=bad, asset=asset) |
| 172 |
assert nb.retry == 2 |
| 173 |
|
| 174 |
|
| 175 |
class TestUrlRoundTrip: |
| 176 |
def test_wait_in_url_parameters(self): |
| 177 |
nb = _TestNotify(host="localhost", wait=1.5, retry=2) |
| 178 |
params = nb.url_parameters() |
| 179 |
assert "wait" in params |
| 180 |
assert params["wait"] == "1.5" |
| 181 |
assert "retry" in params |
| 182 |
assert params["retry"] == "2" |
| 183 |
|
| 184 |
def test_wait_zero_always_in_url_parameters(self): |
| 185 |
"""retry= and wait= are always present, even at their zero defaults.""" |
| 186 |
nb = _TestNotify(host="localhost", wait=0.0, retry=0) |
| 187 |
params = nb.url_parameters() |
| 188 |
assert "wait" in params |
| 189 |
assert params["wait"] == "0.0" |
| 190 |
assert "retry" in params |
| 191 |
assert params["retry"] == "0" |
| 192 |
|
| 193 |
def test_parse_url_extracts_wait(self): |
| 194 |
results = _TestNotify.parse_url("test://localhost?wait=2.5&retry=3") |
| 195 |
assert results is not None |
| 196 |
assert results.get("wait") == "2.5" |
| 197 |
assert results.get("retry") == "3" |
| 198 |
|
| 199 |
def test_parse_url_no_wait_key_absent(self): |
| 200 |
results = _TestNotify.parse_url("test://localhost") |
| 201 |
assert results is not None |
| 202 |
assert "wait" not in results |
| 203 |
|
| 204 |
|
| 205 |
class _FailThenSucceedNotify(NotifyBase): |
| 206 |
"""Fails the first N calls, then succeeds.""" |
| 207 |
|
| 208 |
app_id = "FailThenSucceed" |
| 209 |
app_desc = "Test" |
| 210 |
notify_url = "failpass://" |
| 211 |
title_maxlen = 250 |
| 212 |
body_maxlen = 32768 |
| 213 |
|
| 214 |
def __init__(self, fail_times=1, **kwargs): |
| 215 |
super().__init__(**kwargs) |
| 216 |
self._fail_times = fail_times |
| 217 |
self._calls = 0 |
| 218 |
|
| 219 |
def url(self, *args, **kwargs): |
| 220 |
return "failpass://localhost" |
| 221 |
|
| 222 |
def send(self, **kwargs): |
| 223 |
self._calls += 1 |
| 224 |
return not self._calls <= self._fail_times |
| 225 |
|
| 226 |
async def async_notify(self, **kwargs): |
| 227 |
return self.send(**kwargs) |
| 228 |
|
| 229 |
@staticmethod |
| 230 |
def parse_url(url): |
| 231 |
return NotifyBase.parse_url(url, verify_host=False) |
| 232 |
|
| 233 |
|
| 234 |
class TestSequentialSleep: |
| 235 |
"""time.sleep is called between retries in sequential dispatch.""" |
| 236 |
|
| 237 |
def test_sleep_called_on_retry(self): |
| 238 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 239 |
|
| 240 |
try: |
| 241 |
asset = AppriseAsset(async_mode=False) |
| 242 |
a = Apprise(asset=asset) |
| 243 |
|
| 244 |
server = _FailThenSucceedNotify( |
| 245 |
host="localhost", |
| 246 |
asset=asset, |
| 247 |
retry=2, |
| 248 |
wait=0.3, |
| 249 |
) |
| 250 |
a.add(server) |
| 251 |
|
| 252 |
with mock.patch("apprise.apprise.time.sleep") as mock_sleep: |
| 253 |
result = a.notify(body="test") |
| 254 |
|
| 255 |
assert result is True |
| 256 |
|
| 257 |
mock_sleep.assert_called_once_with(pytest.approx(0.3)) |
| 258 |
finally: |
| 259 |
N_MGR.unload_modules() |
| 260 |
|
| 261 |
def test_sleep_not_called_when_wait_zero(self): |
| 262 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 263 |
|
| 264 |
try: |
| 265 |
asset = AppriseAsset(async_mode=False) |
| 266 |
a = Apprise(asset=asset) |
| 267 |
|
| 268 |
server = _FailThenSucceedNotify( |
| 269 |
host="localhost", |
| 270 |
asset=asset, |
| 271 |
retry=2, |
| 272 |
wait=0.0, |
| 273 |
) |
| 274 |
a.add(server) |
| 275 |
|
| 276 |
with mock.patch("apprise.apprise.time.sleep") as mock_sleep: |
| 277 |
result = a.notify(body="test") |
| 278 |
|
| 279 |
assert result is True |
| 280 |
mock_sleep.assert_not_called() |
| 281 |
finally: |
| 282 |
N_MGR.unload_modules() |
| 283 |
|
| 284 |
def test_sleep_not_called_when_no_retry(self): |
| 285 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 286 |
|
| 287 |
try: |
| 288 |
asset = AppriseAsset(async_mode=False) |
| 289 |
a = Apprise(asset=asset) |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
server = _FailThenSucceedNotify( |
| 294 |
host="localhost", |
| 295 |
asset=asset, |
| 296 |
retry=0, |
| 297 |
wait=1.0, |
| 298 |
fail_times=0, |
| 299 |
) |
| 300 |
a.add(server) |
| 301 |
|
| 302 |
with mock.patch("apprise.apprise.time.sleep") as mock_sleep: |
| 303 |
result = a.notify(body="test") |
| 304 |
|
| 305 |
assert result is True |
| 306 |
mock_sleep.assert_not_called() |
| 307 |
finally: |
| 308 |
N_MGR.unload_modules() |
| 309 |
|
| 310 |
def test_sleep_called_multiple_times(self): |
| 311 |
"""Fails twice, succeeds on third attempt -> sleep called twice.""" |
| 312 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 313 |
|
| 314 |
try: |
| 315 |
asset = AppriseAsset(async_mode=False) |
| 316 |
a = Apprise(asset=asset) |
| 317 |
|
| 318 |
server = _FailThenSucceedNotify( |
| 319 |
host="localhost", |
| 320 |
asset=asset, |
| 321 |
retry=3, |
| 322 |
wait=0.5, |
| 323 |
fail_times=2, |
| 324 |
) |
| 325 |
a.add(server) |
| 326 |
|
| 327 |
with mock.patch("apprise.apprise.time.sleep") as mock_sleep: |
| 328 |
result = a.notify(body="test") |
| 329 |
|
| 330 |
assert result is True |
| 331 |
assert mock_sleep.call_count == 2 |
| 332 |
for call in mock_sleep.call_args_list: |
| 333 |
assert call == mock.call(pytest.approx(0.5)) |
| 334 |
finally: |
| 335 |
N_MGR.unload_modules() |
| 336 |
|
| 337 |
|
| 338 |
class TestThreadPoolSleep: |
| 339 |
"""time.sleep is called between retries in threadpool dispatch.""" |
| 340 |
|
| 341 |
def test_sleep_called_in_threadpool(self): |
| 342 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 343 |
|
| 344 |
try: |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
asset = AppriseAsset(async_mode=True) |
| 349 |
|
| 350 |
|
| 351 |
s1 = _FailThenSucceedNotify( |
| 352 |
host="localhost", |
| 353 |
asset=asset, |
| 354 |
retry=2, |
| 355 |
wait=0.2, |
| 356 |
fail_times=1, |
| 357 |
) |
| 358 |
s2 = _FailThenSucceedNotify( |
| 359 |
host="localhost2", |
| 360 |
asset=asset, |
| 361 |
retry=0, |
| 362 |
wait=0.0, |
| 363 |
fail_times=0, |
| 364 |
) |
| 365 |
|
| 366 |
a = Apprise(asset=asset) |
| 367 |
a.add(s1) |
| 368 |
a.add(s2) |
| 369 |
|
| 370 |
with mock.patch("apprise.apprise.time.sleep") as mock_sleep: |
| 371 |
result = a.notify(body="test") |
| 372 |
|
| 373 |
assert result is True |
| 374 |
|
| 375 |
mock_sleep.assert_called_once_with(pytest.approx(0.2)) |
| 376 |
finally: |
| 377 |
N_MGR.unload_modules() |
| 378 |
|
| 379 |
def test_sleep_not_called_in_threadpool_when_wait_zero(self): |
| 380 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 381 |
|
| 382 |
try: |
| 383 |
asset = AppriseAsset(async_mode=True) |
| 384 |
|
| 385 |
s1 = _FailThenSucceedNotify( |
| 386 |
host="localhost", |
| 387 |
asset=asset, |
| 388 |
retry=2, |
| 389 |
wait=0.0, |
| 390 |
fail_times=1, |
| 391 |
) |
| 392 |
s2 = _FailThenSucceedNotify( |
| 393 |
host="localhost2", |
| 394 |
asset=asset, |
| 395 |
retry=0, |
| 396 |
wait=0.0, |
| 397 |
fail_times=0, |
| 398 |
) |
| 399 |
|
| 400 |
a = Apprise(asset=asset) |
| 401 |
a.add(s1) |
| 402 |
a.add(s2) |
| 403 |
|
| 404 |
with mock.patch("apprise.apprise.time.sleep") as mock_sleep: |
| 405 |
result = a.notify(body="test") |
| 406 |
|
| 407 |
assert result is True |
| 408 |
mock_sleep.assert_not_called() |
| 409 |
finally: |
| 410 |
N_MGR.unload_modules() |
| 411 |
|
| 412 |
|
| 413 |
class TestAsyncioSleep: |
| 414 |
"""asyncio.sleep is called between retries in async dispatch.""" |
| 415 |
|
| 416 |
def test_asyncio_sleep_called(self): |
| 417 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 418 |
|
| 419 |
try: |
| 420 |
asset = AppriseAsset() |
| 421 |
|
| 422 |
server = _FailThenSucceedNotify( |
| 423 |
host="localhost", |
| 424 |
asset=asset, |
| 425 |
retry=2, |
| 426 |
wait=0.4, |
| 427 |
fail_times=1, |
| 428 |
) |
| 429 |
|
| 430 |
a = Apprise(asset=asset) |
| 431 |
a.add(server) |
| 432 |
|
| 433 |
async def run(): |
| 434 |
with mock.patch("apprise.apprise.asyncio.sleep") as mock_sleep: |
| 435 |
mock_sleep.return_value = None |
| 436 |
result = await a.async_notify(body="test") |
| 437 |
return result, mock_sleep |
| 438 |
|
| 439 |
result, mock_sleep = asyncio.run(run()) |
| 440 |
|
| 441 |
assert result is True |
| 442 |
mock_sleep.assert_called_once_with(pytest.approx(0.4)) |
| 443 |
finally: |
| 444 |
N_MGR.unload_modules() |
| 445 |
|
| 446 |
def test_asyncio_sleep_not_called_when_wait_zero(self): |
| 447 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 448 |
|
| 449 |
try: |
| 450 |
asset = AppriseAsset() |
| 451 |
|
| 452 |
server = _FailThenSucceedNotify( |
| 453 |
host="localhost", |
| 454 |
asset=asset, |
| 455 |
retry=2, |
| 456 |
wait=0.0, |
| 457 |
fail_times=1, |
| 458 |
) |
| 459 |
|
| 460 |
a = Apprise(asset=asset) |
| 461 |
a.add(server) |
| 462 |
|
| 463 |
async def run(): |
| 464 |
with mock.patch("apprise.apprise.asyncio.sleep") as mock_sleep: |
| 465 |
mock_sleep.return_value = None |
| 466 |
result = await a.async_notify(body="test") |
| 467 |
return result, mock_sleep |
| 468 |
|
| 469 |
result, mock_sleep = asyncio.run(run()) |
| 470 |
|
| 471 |
assert result is True |
| 472 |
mock_sleep.assert_not_called() |
| 473 |
finally: |
| 474 |
N_MGR.unload_modules() |
| 475 |
|
| 476 |
|
| 477 |
class TestAssetDefault: |
| 478 |
def test_asset_wait_default_used(self): |
| 479 |
asset = AppriseAsset(default_service_wait=1.5) |
| 480 |
nb = _TestNotify(host="localhost", asset=asset) |
| 481 |
assert nb.wait == pytest.approx(1.5) |
| 482 |
|
| 483 |
def test_explicit_wait_overrides_asset(self): |
| 484 |
asset = AppriseAsset(default_service_wait=1.5) |
| 485 |
nb = _TestNotify(host="localhost", asset=asset, wait=0.2) |
| 486 |
assert nb.wait == pytest.approx(0.2) |
| 487 |
|
| 488 |
def test_asset_retry_default_used(self): |
| 489 |
asset = AppriseAsset(default_service_retry=3) |
| 490 |
nb = _TestNotify(host="localhost", asset=asset) |
| 491 |
assert nb.retry == 3 |
| 492 |
|
| 493 |
def test_explicit_retry_overrides_asset(self): |
| 494 |
asset = AppriseAsset(default_service_retry=3) |
| 495 |
nb = _TestNotify(host="localhost", asset=asset, retry=1) |
| 496 |
assert nb.retry == 1 |
| 497 |
|
| 498 |
|
| 499 |
class _RaisingNotify(NotifyBase): |
| 500 |
"""Plugin that raises a bare Exception from notify().""" |
| 501 |
|
| 502 |
app_id = "RaisingApp" |
| 503 |
app_desc = "Test" |
| 504 |
notify_url = "raise://" |
| 505 |
title_maxlen = 250 |
| 506 |
body_maxlen = 32768 |
| 507 |
|
| 508 |
def url(self, *args, **kwargs): |
| 509 |
return "raise://localhost" |
| 510 |
|
| 511 |
def send(self, **kwargs): |
| 512 |
raise RuntimeError("plugin exploded") |
| 513 |
|
| 514 |
async def async_notify(self, **kwargs): |
| 515 |
raise RuntimeError("plugin exploded async") |
| 516 |
|
| 517 |
@staticmethod |
| 518 |
def parse_url(url): |
| 519 |
return NotifyBase.parse_url(url, verify_host=False) |
| 520 |
|
| 521 |
|
| 522 |
class TestAppriseTag: |
| 523 |
"""Full branch coverage for AppriseTag.""" |
| 524 |
|
| 525 |
def test_parse_returns_same_instance(self): |
| 526 |
"""parse() short-circuits when the input is already an AppriseTag.""" |
| 527 |
t = AppriseTag("alerts", priority=2) |
| 528 |
|
| 529 |
assert AppriseTag.parse(t) is t |
| 530 |
|
| 531 |
def test_parse_fallback_for_unrecognised_input(self): |
| 532 |
"""parse() falls back to a raw-name tag when the regex has no match.""" |
| 533 |
|
| 534 |
|
| 535 |
t = AppriseTag.parse("@invalid") |
| 536 |
assert str(t) == "@invalid" |
| 537 |
assert t.priority == 0 |
| 538 |
assert t.retry is None |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
def test_repr_plain(self): |
| 543 |
"""repr() with default priority and no retry is minimal.""" |
| 544 |
assert repr(AppriseTag("simple")) == "AppriseTag('simple')" |
| 545 |
|
| 546 |
def test_repr_with_priority(self): |
| 547 |
"""repr() includes priority= when non-zero (tag.py lines 121-122).""" |
| 548 |
r = repr(AppriseTag("test", priority=3)) |
| 549 |
assert "priority=3" in r |
| 550 |
|
| 551 |
def test_repr_with_retry(self): |
| 552 |
"""repr() includes retry= when set (tag.py lines 123-125).""" |
| 553 |
r = repr(AppriseTag("test", retry=5)) |
| 554 |
assert "retry=5" in r |
| 555 |
|
| 556 |
def test_repr_with_priority_and_retry(self): |
| 557 |
"""repr() includes both fields when both are non-default.""" |
| 558 |
r = repr(AppriseTag("test", priority=2, retry=3)) |
| 559 |
assert "priority=2" in r |
| 560 |
assert "retry=3" in r |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
def test_eq_two_apprisetags_same_name(self): |
| 565 |
"""Two AppriseTag objects are equal when their names match, |
| 566 |
regardless of priority or retry.""" |
| 567 |
assert AppriseTag("abc") == AppriseTag("abc", priority=5) |
| 568 |
assert AppriseTag("ABC") == AppriseTag("abc") |
| 569 |
|
| 570 |
def test_eq_with_string(self): |
| 571 |
"""An AppriseTag equals a plain str by name.""" |
| 572 |
assert AppriseTag("hello") == "hello" |
| 573 |
assert AppriseTag("HELLO") == "hello" |
| 574 |
|
| 575 |
def test_eq_not_equal_different_name(self): |
| 576 |
assert AppriseTag("abc") != AppriseTag("xyz") |
| 577 |
assert AppriseTag("abc") != "xyz" |
| 578 |
|
| 579 |
def test_eq_returns_notimplemented_for_other_types(self): |
| 580 |
"""Comparing to a non-str/AppriseTag returns NotImplemented""" |
| 581 |
result = AppriseTag("abc").__eq__(42) |
| 582 |
assert result is NotImplemented |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
def test_lt_two_apprisetags(self): |
| 587 |
"""__lt__ sorts by tag name (tag.py lines 159-160).""" |
| 588 |
assert AppriseTag("abc") < AppriseTag("def") |
| 589 |
assert not (AppriseTag("def") < AppriseTag("abc")) |
| 590 |
|
| 591 |
def test_lt_with_string(self): |
| 592 |
"""AppriseTag can be less-than compared to a plain string |
| 593 |
(tag.py lines 161-162).""" |
| 594 |
assert AppriseTag("abc") < "def" |
| 595 |
assert not (AppriseTag("zzz") < "aaa") |
| 596 |
|
| 597 |
def test_lt_returns_notimplemented_for_other_types(self): |
| 598 |
"""__lt__ with non-str/AppriseTag returns NotImplemented""" |
| 599 |
result = AppriseTag("abc").__lt__(42) |
| 600 |
assert result is NotImplemented |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
def test_bool_empty_tag_is_false(self): |
| 605 |
"""An AppriseTag with an empty name is falsy""" |
| 606 |
assert not AppriseTag("") |
| 607 |
assert bool(AppriseTag("")) is False |
| 608 |
|
| 609 |
def test_bool_nonempty_tag_is_true(self): |
| 610 |
assert bool(AppriseTag("something")) is True |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
def test_apprisetag_in_set_with_plain_string(self): |
| 615 |
"""AppriseTag in a set is found by plain string membership test.""" |
| 616 |
s = {AppriseTag("endpoint")} |
| 617 |
assert "endpoint" in s |
| 618 |
|
| 619 |
def test_plain_string_in_set_with_apprisetag(self): |
| 620 |
"""A plain string is found in a set containing AppriseTag.""" |
| 621 |
s = {"endpoint"} |
| 622 |
assert AppriseTag("endpoint") in s |
| 623 |
|
| 624 |
|
| 625 |
class TestStaticHelpers: |
| 626 |
"""Tests for _extract_filter_retry and _filter_has_explicit_priority |
| 627 |
covering the nested-list branches not reached by integration tests.""" |
| 628 |
|
| 629 |
def test_extract_retry_from_nested_list(self): |
| 630 |
"""Retry suffix found inside a nested list entry (apprise.py:413).""" |
| 631 |
|
| 632 |
result = Apprise._extract_filter_retry([["alerts:3"]]) |
| 633 |
assert result == 3 |
| 634 |
|
| 635 |
def test_extract_retry_from_list_of_strings(self): |
| 636 |
"""Retry suffix found in a plain-string list entry (apprise.py:417).""" |
| 637 |
result = Apprise._extract_filter_retry(["alerts:3"]) |
| 638 |
assert result == 3 |
| 639 |
|
| 640 |
def test_extract_retry_none_when_absent(self): |
| 641 |
"""Returns None when no retry suffix is present.""" |
| 642 |
assert Apprise._extract_filter_retry("alerts") is None |
| 643 |
assert Apprise._extract_filter_retry(["alerts"]) is None |
| 644 |
|
| 645 |
def test_extract_retry_none_from_nested_list_no_suffix(self): |
| 646 |
"""Nested list without retry suffix returns None. |
| 647 |
|
| 648 |
Covers the 'if ft.retry is not None' False branch and the |
| 649 |
inner for-loop completing without returning. |
| 650 |
""" |
| 651 |
result = Apprise._extract_filter_retry([["alerts"]]) |
| 652 |
assert result is None |
| 653 |
|
| 654 |
def test_server_priority_for_tag_name_absent(self): |
| 655 |
"""Returns 0 when no tag in server.tags matches tag_name.""" |
| 656 |
server = mock.Mock() |
| 657 |
server.tags = {AppriseTag("alerts", priority=3, has_priority=True)} |
| 658 |
assert Apprise._server_priority_for_tag_name(server, "backup") == 0 |
| 659 |
|
| 660 |
def test_match_service_retry_plain_string_tag_backward_compat(self): |
| 661 |
"""Plain string in server.tags matched by a priority-prefixed token. |
| 662 |
|
| 663 |
Covers the else/str fallback inside _match_service_retry when stag is |
| 664 |
not an AppriseTag instance and the name matches (lines 497-498). |
| 665 |
""" |
| 666 |
server = mock.Mock() |
| 667 |
server.tags = {"alerts"} |
| 668 |
|
| 669 |
result = Apprise._match_service_retry(server, "3:alerts:2") |
| 670 |
assert result == 2 |
| 671 |
|
| 672 |
def test_match_service_retry_plain_string_tag_no_match(self): |
| 673 |
"""Plain string in server.tags that does not match returns None.""" |
| 674 |
server = mock.Mock() |
| 675 |
server.tags = {"other"} |
| 676 |
result = Apprise._match_service_retry(server, "3:alerts:2") |
| 677 |
assert result is None |
| 678 |
|
| 679 |
def test_extract_retry_none_for_match_all(self): |
| 680 |
assert Apprise._extract_filter_retry(MATCH_ALL_TAG) is None |
| 681 |
assert Apprise._extract_filter_retry(None) is None |
| 682 |
|
| 683 |
def test_has_priority_nested_list(self): |
| 684 |
"""Priority prefix found inside a nested list (apprise.py:438).""" |
| 685 |
assert Apprise._filter_has_explicit_priority([["2:alerts"]]) |
| 686 |
|
| 687 |
def test_has_priority_list_of_strings(self): |
| 688 |
"""Priority prefix found in a plain-string list (apprise.py:441).""" |
| 689 |
assert Apprise._filter_has_explicit_priority(["2:alerts"]) |
| 690 |
|
| 691 |
def test_has_priority_false_no_prefix(self): |
| 692 |
assert not Apprise._filter_has_explicit_priority("alerts") |
| 693 |
assert not Apprise._filter_has_explicit_priority(["alerts"]) |
| 694 |
|
| 695 |
def test_has_priority_false_for_match_all(self): |
| 696 |
assert not Apprise._filter_has_explicit_priority(MATCH_ALL_TAG) |
| 697 |
assert not Apprise._filter_has_explicit_priority(None) |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
def test_threadpool_zero_servers_returns_true(self): |
| 702 |
"""Empty server list returns True immediately (apprise.py:940).""" |
| 703 |
assert Apprise._notify_parallel_threadpool() is True |
| 704 |
|
| 705 |
def test_asyncio_zero_servers_returns_true(self): |
| 706 |
"""Empty server list returns True immediately (apprise.py:1031).""" |
| 707 |
result = asyncio.run(Apprise._notify_parallel_asyncio()) |
| 708 |
assert result is True |
| 709 |
|
| 710 |
|
| 711 |
class TestDispatchIntegration: |
| 712 |
"""End-to-end notify() tests covering the new priority/retry dispatch.""" |
| 713 |
|
| 714 |
def _make_server(self, host, priority, asset, fail_times=0): |
| 715 |
"""Create a _FailThenSucceedNotify with an AppriseTag in its tags.""" |
| 716 |
s = _FailThenSucceedNotify( |
| 717 |
host=host, asset=asset, fail_times=fail_times |
| 718 |
) |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
s.tags = {AppriseTag("alerts", priority=priority, has_priority=True)} |
| 725 |
return s |
| 726 |
|
| 727 |
def test_filter_retry_override_via_tag_suffix(self): |
| 728 |
"""tag suffix ':N' overrides per-server retry for this call only.""" |
| 729 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 730 |
|
| 731 |
try: |
| 732 |
asset = AppriseAsset(async_mode=False) |
| 733 |
|
| 734 |
server = _FailThenSucceedNotify( |
| 735 |
host="localhost", |
| 736 |
asset=asset, |
| 737 |
retry=0, |
| 738 |
wait=0.0, |
| 739 |
fail_times=2, |
| 740 |
) |
| 741 |
|
| 742 |
|
| 743 |
server.tags = {"failpass"} |
| 744 |
a = Apprise(asset=asset) |
| 745 |
a.add(server) |
| 746 |
|
| 747 |
|
| 748 |
result = a.notify(body="test", tag="failpass:2") |
| 749 |
assert result is True |
| 750 |
assert server._calls == 3 |
| 751 |
finally: |
| 752 |
N_MGR.unload_modules() |
| 753 |
|
| 754 |
def test_exclusive_priority_dispatch_skips_other_priorities(self): |
| 755 |
"""Explicit priority prefix dispatches only matching-priority services. |
| 756 |
Covers apprise.py lines 556-572.""" |
| 757 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 758 |
|
| 759 |
try: |
| 760 |
asset = AppriseAsset(async_mode=False) |
| 761 |
s1 = self._make_server("host1", priority=1, asset=asset) |
| 762 |
s2 = self._make_server("host2", priority=2, asset=asset) |
| 763 |
|
| 764 |
a = Apprise(asset=asset) |
| 765 |
a.add(s1) |
| 766 |
a.add(s2) |
| 767 |
|
| 768 |
|
| 769 |
result = a.notify(body="test", tag="2:alerts") |
| 770 |
assert result is True |
| 771 |
assert s1._calls == 0 |
| 772 |
assert s2._calls == 1 |
| 773 |
finally: |
| 774 |
N_MGR.unload_modules() |
| 775 |
|
| 776 |
def test_escalation_stops_when_highest_priority_group_succeeds(self): |
| 777 |
"""If priority-1 group all succeed, priority-2 group is not run.""" |
| 778 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 779 |
|
| 780 |
try: |
| 781 |
asset = AppriseAsset(async_mode=False) |
| 782 |
s1 = self._make_server("host1", priority=1, asset=asset) |
| 783 |
s2 = self._make_server("host2", priority=2, asset=asset) |
| 784 |
|
| 785 |
a = Apprise(asset=asset) |
| 786 |
a.add(s1) |
| 787 |
a.add(s2) |
| 788 |
|
| 789 |
result = a.notify(body="test", tag="alerts") |
| 790 |
assert result is True |
| 791 |
assert s1._calls == 1 |
| 792 |
assert s2._calls == 0 |
| 793 |
finally: |
| 794 |
N_MGR.unload_modules() |
| 795 |
|
| 796 |
def test_escalation_triggers_on_priority_group_failure(self): |
| 797 |
"""Priority-1 group failure escalates to priority-2 group.""" |
| 798 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 799 |
|
| 800 |
try: |
| 801 |
asset = AppriseAsset(async_mode=False) |
| 802 |
|
| 803 |
s1 = self._make_server( |
| 804 |
"host1", priority=1, asset=asset, fail_times=999 |
| 805 |
) |
| 806 |
|
| 807 |
s2 = self._make_server("host2", priority=2, asset=asset) |
| 808 |
|
| 809 |
a = Apprise(asset=asset) |
| 810 |
a.add(s1) |
| 811 |
a.add(s2) |
| 812 |
|
| 813 |
result = a.notify(body="test", tag="alerts") |
| 814 |
assert result is True |
| 815 |
assert s1._calls == 1 |
| 816 |
assert s2._calls == 1 |
| 817 |
finally: |
| 818 |
N_MGR.unload_modules() |
| 819 |
|
| 820 |
def test_async_notify_filter_retry_and_explicit_priority(self): |
| 821 |
"""async_notify() covers the same escalation/exclusive paths. |
| 822 |
Covers apprise.py lines 630, 637-649.""" |
| 823 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 824 |
|
| 825 |
try: |
| 826 |
asset = AppriseAsset() |
| 827 |
s1 = self._make_server("host1", priority=1, asset=asset) |
| 828 |
s2 = self._make_server("host2", priority=2, asset=asset) |
| 829 |
|
| 830 |
a = Apprise(asset=asset) |
| 831 |
a.add(s1) |
| 832 |
a.add(s2) |
| 833 |
|
| 834 |
async def run_exclusive(): |
| 835 |
return await a.async_notify(body="test", tag="2:alerts") |
| 836 |
|
| 837 |
async def run_escalation(): |
| 838 |
return await a.async_notify(body="test", tag="alerts") |
| 839 |
|
| 840 |
|
| 841 |
r = asyncio.run(run_exclusive()) |
| 842 |
assert r is True |
| 843 |
assert s1._calls == 0 |
| 844 |
assert s2._calls == 1 |
| 845 |
|
| 846 |
|
| 847 |
s1._calls = 0 |
| 848 |
s2._calls = 0 |
| 849 |
r = asyncio.run(run_escalation()) |
| 850 |
assert r is True |
| 851 |
assert s1._calls == 1 |
| 852 |
assert s2._calls == 0 |
| 853 |
finally: |
| 854 |
N_MGR.unload_modules() |
| 855 |
|
| 856 |
def test_plain_and_zero_prefix_are_equivalent(self): |
| 857 |
"""'family' and '0:family' are identical at priority 0. |
| 858 |
A filter of '0:family' must match services tagged with plain |
| 859 |
'family', and both forms must land in the same escalation group.""" |
| 860 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 861 |
|
| 862 |
try: |
| 863 |
asset = AppriseAsset(async_mode=False) |
| 864 |
|
| 865 |
|
| 866 |
s1 = _FailThenSucceedNotify( |
| 867 |
host="host1", asset=asset, fail_times=0 |
| 868 |
) |
| 869 |
s1.tags = {AppriseTag.parse("family")} |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
s2 = _FailThenSucceedNotify( |
| 874 |
host="host2", asset=asset, fail_times=0 |
| 875 |
) |
| 876 |
s2.tags = {AppriseTag.parse("0:family")} |
| 877 |
|
| 878 |
|
| 879 |
s3 = _FailThenSucceedNotify( |
| 880 |
host="host3", asset=asset, fail_times=0 |
| 881 |
) |
| 882 |
s3.tags = {AppriseTag.parse("1:family")} |
| 883 |
|
| 884 |
a = Apprise(asset=asset) |
| 885 |
a.add(s1) |
| 886 |
a.add(s2) |
| 887 |
a.add(s3) |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
result = a.notify(body="test", tag="0:family") |
| 892 |
assert result is True |
| 893 |
assert s1._calls == 1 |
| 894 |
assert s2._calls == 1 |
| 895 |
assert s3._calls == 0 |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
s1._calls = s2._calls = s3._calls = 0 |
| 900 |
result = a.notify(body="test", tag="family") |
| 901 |
assert result is True |
| 902 |
assert s1._calls == 1 |
| 903 |
assert s2._calls == 1 |
| 904 |
assert s3._calls == 0 |
| 905 |
finally: |
| 906 |
N_MGR.unload_modules() |
| 907 |
|
| 908 |
|
| 909 |
class TestExceptionHandling: |
| 910 |
"""Verify that plugin exceptions are caught and retried, not propagated.""" |
| 911 |
|
| 912 |
def test_sequential_exception_treated_as_failure(self): |
| 913 |
"""A plugin raising Exception is caught; delivery is marked failed.""" |
| 914 |
N_MGR["raise"] = _RaisingNotify |
| 915 |
|
| 916 |
try: |
| 917 |
asset = AppriseAsset(async_mode=False) |
| 918 |
server = _RaisingNotify(host="localhost", asset=asset, retry=0) |
| 919 |
a = Apprise(asset=asset) |
| 920 |
a.add(server) |
| 921 |
|
| 922 |
result = a.notify(body="test") |
| 923 |
assert result is False |
| 924 |
finally: |
| 925 |
N_MGR.unload_modules() |
| 926 |
|
| 927 |
def test_sequential_exception_retried(self): |
| 928 |
"""Exception on first attempt is retried; later success counts.""" |
| 929 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 930 |
N_MGR["raise"] = _RaisingNotify |
| 931 |
|
| 932 |
try: |
| 933 |
asset = AppriseAsset(async_mode=False) |
| 934 |
|
| 935 |
server = _FailThenSucceedNotify( |
| 936 |
host="localhost", asset=asset, retry=1, wait=0.0, fail_times=1 |
| 937 |
) |
| 938 |
|
| 939 |
call_count = [0] |
| 940 |
|
| 941 |
def raising_then_ok(**kwargs): |
| 942 |
call_count[0] += 1 |
| 943 |
if call_count[0] == 1: |
| 944 |
raise RuntimeError("transient error") |
| 945 |
return True |
| 946 |
|
| 947 |
server.send = raising_then_ok |
| 948 |
a = Apprise(asset=asset) |
| 949 |
a.add(server) |
| 950 |
|
| 951 |
result = a.notify(body="test") |
| 952 |
assert result is True |
| 953 |
assert call_count[0] == 2 |
| 954 |
finally: |
| 955 |
N_MGR.unload_modules() |
| 956 |
|
| 957 |
def test_threadpool_future_exception_caught(self): |
| 958 |
"""An exception escaping a thread future is caught gracefully. |
| 959 |
Covers apprise.py lines 1004-1006.""" |
| 960 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 961 |
|
| 962 |
try: |
| 963 |
asset = AppriseAsset(async_mode=True) |
| 964 |
|
| 965 |
s1 = _FailThenSucceedNotify( |
| 966 |
host="host1", asset=asset, fail_times=0 |
| 967 |
) |
| 968 |
s2 = _FailThenSucceedNotify( |
| 969 |
host="host2", asset=asset, fail_times=0 |
| 970 |
) |
| 971 |
a = Apprise(asset=asset) |
| 972 |
a.add(s1) |
| 973 |
a.add(s2) |
| 974 |
|
| 975 |
|
| 976 |
import concurrent.futures as cf |
| 977 |
|
| 978 |
real_as_completed = cf.as_completed |
| 979 |
|
| 980 |
def patched_as_completed(futures, **kw): |
| 981 |
for i, f in enumerate(real_as_completed(futures, **kw)): |
| 982 |
if i == 0: |
| 983 |
|
| 984 |
m = mock.Mock() |
| 985 |
m.result.side_effect = RuntimeError("future boom") |
| 986 |
yield m |
| 987 |
else: |
| 988 |
yield f |
| 989 |
|
| 990 |
with mock.patch( |
| 991 |
"apprise.apprise.cf.as_completed", patched_as_completed |
| 992 |
): |
| 993 |
result = a.notify(body="test") |
| 994 |
|
| 995 |
|
| 996 |
assert result is False |
| 997 |
finally: |
| 998 |
N_MGR.unload_modules() |
| 999 |
|
| 1000 |
def test_asyncio_exception_in_do_call_treated_as_failure(self): |
| 1001 |
"""An exception inside do_call's retry loop is caught and the |
| 1002 |
service is marked as failed (covers the new try/except in do_call).""" |
| 1003 |
N_MGR["raise"] = _RaisingNotify |
| 1004 |
|
| 1005 |
try: |
| 1006 |
asset = AppriseAsset() |
| 1007 |
server = _RaisingNotify(host="localhost", asset=asset, retry=0) |
| 1008 |
a = Apprise(asset=asset) |
| 1009 |
a.add(server) |
| 1010 |
|
| 1011 |
async def run(): |
| 1012 |
return await a.async_notify(body="test") |
| 1013 |
|
| 1014 |
result = asyncio.run(run()) |
| 1015 |
assert result is False |
| 1016 |
finally: |
| 1017 |
N_MGR.unload_modules() |
| 1018 |
|
| 1019 |
def test_asyncio_gather_unhandled_exception(self): |
| 1020 |
"""An Exception escaping the outer gather() round is caught as a |
| 1021 |
safety net and treated as a batch failure.""" |
| 1022 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1023 |
|
| 1024 |
try: |
| 1025 |
asset = AppriseAsset() |
| 1026 |
server = _FailThenSucceedNotify( |
| 1027 |
host="localhost", asset=asset, fail_times=0 |
| 1028 |
) |
| 1029 |
a = Apprise(asset=asset) |
| 1030 |
a.add(server) |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
async def fake_gather(*args, **kw): |
| 1036 |
for arg in args: |
| 1037 |
if asyncio.iscoroutine(arg): |
| 1038 |
arg.close() |
| 1039 |
return [RuntimeError("escaped")] |
| 1040 |
|
| 1041 |
async def run(): |
| 1042 |
with mock.patch("apprise.apprise.asyncio.gather", fake_gather): |
| 1043 |
return await a.async_notify(body="test") |
| 1044 |
|
| 1045 |
result = asyncio.run(run()) |
| 1046 |
assert result is False |
| 1047 |
finally: |
| 1048 |
N_MGR.unload_modules() |
| 1049 |
|
| 1050 |
def test_asyncio_gather_type_error(self): |
| 1051 |
"""A TypeError in gather results is caught and returns False.""" |
| 1052 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1053 |
|
| 1054 |
try: |
| 1055 |
asset = AppriseAsset() |
| 1056 |
server = _FailThenSucceedNotify( |
| 1057 |
host="localhost", asset=asset, fail_times=0 |
| 1058 |
) |
| 1059 |
a = Apprise(asset=asset) |
| 1060 |
a.add(server) |
| 1061 |
|
| 1062 |
async def fake_gather(*args, **kw): |
| 1063 |
for arg in args: |
| 1064 |
if asyncio.iscoroutine(arg): |
| 1065 |
arg.close() |
| 1066 |
return [TypeError("validation")] |
| 1067 |
|
| 1068 |
async def run(): |
| 1069 |
with mock.patch("apprise.apprise.asyncio.gather", fake_gather): |
| 1070 |
return await a.async_notify(body="test") |
| 1071 |
|
| 1072 |
result = asyncio.run(run()) |
| 1073 |
assert result is False |
| 1074 |
finally: |
| 1075 |
N_MGR.unload_modules() |
| 1076 |
|
| 1077 |
|
| 1078 |
class TestConfigTagRetry: |
| 1079 |
"""Config parsers must reject a retry suffix in service tag definitions. |
| 1080 |
|
| 1081 |
A tag like 'alerts:3' is only valid at call-time (notify() filter / CLI). |
| 1082 |
Putting it in a config file creates ambiguity with the URL ?retry= param |
| 1083 |
and the YAML retry: key, so both parsers must warn and skip the entry. |
| 1084 |
""" |
| 1085 |
|
| 1086 |
def test_text_rejects_retry_suffix(self): |
| 1087 |
"""'alerts:3=json://...' is rejected; no server loaded.""" |
| 1088 |
servers, _ = ConfigBase.config_parse_text("alerts:3=json://localhost") |
| 1089 |
assert servers == [] |
| 1090 |
|
| 1091 |
def test_text_rejects_priority_and_retry_in_tag(self): |
| 1092 |
"""'1:alerts:3=json://...' is rejected (priority + retry suffix).""" |
| 1093 |
servers, _ = ConfigBase.config_parse_text( |
| 1094 |
"1:alerts:3=json://localhost" |
| 1095 |
) |
| 1096 |
assert servers == [] |
| 1097 |
|
| 1098 |
def test_text_accepts_priority_tag_without_retry(self): |
| 1099 |
"""'1:alerts=json://...' loads normally (priority prefix, no retry).""" |
| 1100 |
servers, _ = ConfigBase.config_parse_text("1:alerts=json://localhost") |
| 1101 |
assert len(servers) == 1 |
| 1102 |
|
| 1103 |
def test_text_accepts_plain_tag(self): |
| 1104 |
"""'alerts=json://...' loads normally (no prefix, no retry).""" |
| 1105 |
servers, _ = ConfigBase.config_parse_text("alerts=json://localhost") |
| 1106 |
assert len(servers) == 1 |
| 1107 |
|
| 1108 |
def test_yaml_rejects_retry_suffix(self): |
| 1109 |
"""YAML 'tag: alerts:3' is rejected; no server loaded.""" |
| 1110 |
servers, _ = ConfigBase.config_parse_yaml( |
| 1111 |
"version: 1\nurls:\n - json://localhost:\n tag: alerts:3\n" |
| 1112 |
) |
| 1113 |
assert servers == [] |
| 1114 |
|
| 1115 |
def test_yaml_rejects_priority_and_retry_in_tag(self): |
| 1116 |
"""YAML 'tag: 1:alerts:3' is rejected (priority + retry suffix).""" |
| 1117 |
servers, _ = ConfigBase.config_parse_yaml( |
| 1118 |
"version: 1\nurls:\n - json://localhost:\n tag: 1:alerts:3\n" |
| 1119 |
) |
| 1120 |
assert servers == [] |
| 1121 |
|
| 1122 |
def test_yaml_accepts_priority_tag_without_retry(self): |
| 1123 |
"""YAML 'tag: 1:alerts' loads normally (priority prefix, no retry).""" |
| 1124 |
servers, _ = ConfigBase.config_parse_yaml( |
| 1125 |
"version: 1\nurls:\n - json://localhost:\n tag: 1:alerts\n" |
| 1126 |
) |
| 1127 |
assert len(servers) == 1 |
| 1128 |
|
| 1129 |
def test_yaml_accepts_plain_tag(self): |
| 1130 |
"""YAML 'tag: alerts' loads normally (no prefix, no retry).""" |
| 1131 |
servers, _ = ConfigBase.config_parse_yaml( |
| 1132 |
"version: 1\nurls:\n - json://localhost:\n tag: alerts\n" |
| 1133 |
) |
| 1134 |
assert len(servers) == 1 |
| 1135 |
|
| 1136 |
def test_text_valid_entry_after_rejected_one_still_loads(self): |
| 1137 |
"""A valid entry that follows a rejected one is still loaded.""" |
| 1138 |
servers, _ = ConfigBase.config_parse_text( |
| 1139 |
"alerts:3=json://localhost\nalerts=json://localhost\n" |
| 1140 |
) |
| 1141 |
assert len(servers) == 1 |
| 1142 |
|
| 1143 |
def test_yaml_valid_entry_after_rejected_one_still_loads(self): |
| 1144 |
"""A valid entry that follows a rejected one is still loaded.""" |
| 1145 |
servers, _ = ConfigBase.config_parse_yaml( |
| 1146 |
"version: 1\n" |
| 1147 |
"urls:\n" |
| 1148 |
" - json://localhost:\n" |
| 1149 |
" tag: alerts:3\n" |
| 1150 |
" - json://localhost:\n" |
| 1151 |
" tag: alerts\n" |
| 1152 |
) |
| 1153 |
assert len(servers) == 1 |
| 1154 |
|
| 1155 |
|
| 1156 |
class TestMultiTagDispatch: |
| 1157 |
"""Per-tag independent escalation and per-service retry for multi-tag OR |
| 1158 |
filters. |
| 1159 |
|
| 1160 |
When notify() receives a filter with multiple OR tokens (e.g. |
| 1161 |
'devops management' or ['devops:3', 'management:2']), each token forms an |
| 1162 |
independent escalation chain and each service gets the retry value from the |
| 1163 |
token that matched it -- not a single global retry applied to all services. |
| 1164 |
""" |
| 1165 |
|
| 1166 |
def _make_tagged(self, tag_str, asset, fail_times=0): |
| 1167 |
"""Create a _FailThenSucceedNotify tagged with a single AppriseTag.""" |
| 1168 |
s = _FailThenSucceedNotify( |
| 1169 |
host=tag_str, asset=asset, fail_times=fail_times |
| 1170 |
) |
| 1171 |
|
| 1172 |
s.tags = {AppriseTag.parse(tag_str)} |
| 1173 |
return s |
| 1174 |
|
| 1175 |
def _make_priority_tagged(self, tag_str, asset, fail_times=0): |
| 1176 |
"""Create a server whose tag is an explicit-priority AppriseTag.""" |
| 1177 |
ft = AppriseTag.parse(tag_str) |
| 1178 |
s = _FailThenSucceedNotify( |
| 1179 |
host=str(ft), asset=asset, fail_times=fail_times |
| 1180 |
) |
| 1181 |
s.tags = {ft} |
| 1182 |
return s |
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
def test_per_service_retry_space_separated_string(self): |
| 1189 |
"""'devops:3 management:2' applies retry=3 to devops services and |
| 1190 |
retry=2 to management services independently.""" |
| 1191 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1192 |
|
| 1193 |
try: |
| 1194 |
asset = AppriseAsset(async_mode=False) |
| 1195 |
|
| 1196 |
|
| 1197 |
s_devops = _FailThenSucceedNotify( |
| 1198 |
host="devops", asset=asset, fail_times=3 |
| 1199 |
) |
| 1200 |
s_devops.tags = {"devops"} |
| 1201 |
|
| 1202 |
|
| 1203 |
s_mgmt = _FailThenSucceedNotify( |
| 1204 |
host="mgmt", asset=asset, fail_times=2 |
| 1205 |
) |
| 1206 |
s_mgmt.tags = {"management"} |
| 1207 |
|
| 1208 |
a = Apprise(asset=asset) |
| 1209 |
a.add(s_devops) |
| 1210 |
a.add(s_mgmt) |
| 1211 |
|
| 1212 |
result = a.notify(body="test", tag="devops:3 management:2") |
| 1213 |
assert result is True |
| 1214 |
|
| 1215 |
assert s_devops._calls == 4 |
| 1216 |
|
| 1217 |
assert s_mgmt._calls == 3 |
| 1218 |
finally: |
| 1219 |
N_MGR.unload_modules() |
| 1220 |
|
| 1221 |
def test_per_service_retry_list_of_tokens(self): |
| 1222 |
"""['devops:3', 'management:2'] gives the same per-service retry |
| 1223 |
result as a space-separated string.""" |
| 1224 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1225 |
|
| 1226 |
try: |
| 1227 |
asset = AppriseAsset(async_mode=False) |
| 1228 |
|
| 1229 |
s_devops = _FailThenSucceedNotify( |
| 1230 |
host="devops", asset=asset, fail_times=3 |
| 1231 |
) |
| 1232 |
s_devops.tags = {"devops"} |
| 1233 |
|
| 1234 |
s_mgmt = _FailThenSucceedNotify( |
| 1235 |
host="mgmt", asset=asset, fail_times=2 |
| 1236 |
) |
| 1237 |
s_mgmt.tags = {"management"} |
| 1238 |
|
| 1239 |
a = Apprise(asset=asset) |
| 1240 |
a.add(s_devops) |
| 1241 |
a.add(s_mgmt) |
| 1242 |
|
| 1243 |
result = a.notify(body="test", tag=["devops:3", "management:2"]) |
| 1244 |
assert result is True |
| 1245 |
assert s_devops._calls == 4 |
| 1246 |
assert s_mgmt._calls == 3 |
| 1247 |
finally: |
| 1248 |
N_MGR.unload_modules() |
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
def test_independent_chains_devops_success_does_not_skip_management(self): |
| 1255 |
"""When devops chain succeeds at priority-1, management chain still |
| 1256 |
dispatches independently -- its priority-1 failure escalates to its |
| 1257 |
priority-2 server.""" |
| 1258 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1259 |
|
| 1260 |
try: |
| 1261 |
asset = AppriseAsset(async_mode=False) |
| 1262 |
|
| 1263 |
|
| 1264 |
s_devops_p1 = _FailThenSucceedNotify( |
| 1265 |
host="d1", asset=asset, fail_times=0 |
| 1266 |
) |
| 1267 |
s_devops_p1.tags = { |
| 1268 |
AppriseTag("devops", priority=1, has_priority=True) |
| 1269 |
} |
| 1270 |
s_devops_p2 = _FailThenSucceedNotify( |
| 1271 |
host="d2", asset=asset, fail_times=0 |
| 1272 |
) |
| 1273 |
s_devops_p2.tags = { |
| 1274 |
AppriseTag("devops", priority=2, has_priority=True) |
| 1275 |
} |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
s_mgmt_p1 = _FailThenSucceedNotify( |
| 1280 |
host="m1", asset=asset, fail_times=999 |
| 1281 |
) |
| 1282 |
s_mgmt_p1.tags = { |
| 1283 |
AppriseTag("management", priority=1, has_priority=True) |
| 1284 |
} |
| 1285 |
s_mgmt_p2 = _FailThenSucceedNotify( |
| 1286 |
host="m2", asset=asset, fail_times=0 |
| 1287 |
) |
| 1288 |
s_mgmt_p2.tags = { |
| 1289 |
AppriseTag("management", priority=2, has_priority=True) |
| 1290 |
} |
| 1291 |
|
| 1292 |
a = Apprise(asset=asset) |
| 1293 |
a.add(s_devops_p1) |
| 1294 |
a.add(s_devops_p2) |
| 1295 |
a.add(s_mgmt_p1) |
| 1296 |
a.add(s_mgmt_p2) |
| 1297 |
|
| 1298 |
result = a.notify(body="test", tag="devops management") |
| 1299 |
assert result is True |
| 1300 |
assert s_devops_p1._calls == 1 |
| 1301 |
assert s_devops_p2._calls == 0 |
| 1302 |
assert s_mgmt_p1._calls == 1 |
| 1303 |
assert s_mgmt_p2._calls == 1 |
| 1304 |
finally: |
| 1305 |
N_MGR.unload_modules() |
| 1306 |
|
| 1307 |
def test_both_chains_must_succeed_for_true(self): |
| 1308 |
"""notify() returns False when one chain exhausts all priority groups |
| 1309 |
without any group fully succeeding.""" |
| 1310 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1311 |
|
| 1312 |
try: |
| 1313 |
asset = AppriseAsset(async_mode=False) |
| 1314 |
|
| 1315 |
|
| 1316 |
s_devops = _FailThenSucceedNotify( |
| 1317 |
host="d", asset=asset, fail_times=0 |
| 1318 |
) |
| 1319 |
s_devops.tags = {"devops"} |
| 1320 |
|
| 1321 |
|
| 1322 |
s_mgmt = _FailThenSucceedNotify( |
| 1323 |
host="m", asset=asset, fail_times=999 |
| 1324 |
) |
| 1325 |
s_mgmt.tags = {"management"} |
| 1326 |
|
| 1327 |
a = Apprise(asset=asset) |
| 1328 |
a.add(s_devops) |
| 1329 |
a.add(s_mgmt) |
| 1330 |
|
| 1331 |
result = a.notify(body="test", tag="devops management") |
| 1332 |
assert result is False |
| 1333 |
assert s_devops._calls == 1 |
| 1334 |
assert s_mgmt._calls == 1 |
| 1335 |
finally: |
| 1336 |
N_MGR.unload_modules() |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
def test_explicit_priority_list_per_service_retry(self): |
| 1343 |
"""['1:tag:2', '2:tag:0'] flat-dispatches both servers; each gets its |
| 1344 |
own retry: the priority-1 server gets retry=2 and the priority-2 |
| 1345 |
server gets retry=0 (one attempt only).""" |
| 1346 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1347 |
|
| 1348 |
try: |
| 1349 |
asset = AppriseAsset(async_mode=False) |
| 1350 |
|
| 1351 |
|
| 1352 |
s1 = _FailThenSucceedNotify(host="h1", asset=asset, fail_times=2) |
| 1353 |
s1.tags = {AppriseTag("tag", priority=1, has_priority=True)} |
| 1354 |
|
| 1355 |
|
| 1356 |
s2 = _FailThenSucceedNotify(host="h2", asset=asset, fail_times=0) |
| 1357 |
s2.tags = {AppriseTag("tag", priority=2, has_priority=True)} |
| 1358 |
|
| 1359 |
a = Apprise(asset=asset) |
| 1360 |
a.add(s1) |
| 1361 |
a.add(s2) |
| 1362 |
|
| 1363 |
result = a.notify(body="test", tag=["1:tag:2", "2:tag:0"]) |
| 1364 |
assert result is True |
| 1365 |
|
| 1366 |
assert s1._calls == 3 |
| 1367 |
|
| 1368 |
assert s2._calls == 1 |
| 1369 |
finally: |
| 1370 |
N_MGR.unload_modules() |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
def test_cli_or_format_independent_chains(self): |
| 1377 |
"""CLI --tag 'devops:3' --tag 'management:2' generates |
| 1378 |
[['devops:3'], ['management:2']] -- single-element inner lists that |
| 1379 |
must be treated as independent OR chains, not AND groups.""" |
| 1380 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1381 |
|
| 1382 |
try: |
| 1383 |
asset = AppriseAsset(async_mode=False) |
| 1384 |
|
| 1385 |
s_devops = _FailThenSucceedNotify( |
| 1386 |
host="devops", asset=asset, fail_times=3 |
| 1387 |
) |
| 1388 |
s_devops.tags = {"devops"} |
| 1389 |
|
| 1390 |
s_mgmt = _FailThenSucceedNotify( |
| 1391 |
host="mgmt", asset=asset, fail_times=2 |
| 1392 |
) |
| 1393 |
s_mgmt.tags = {"management"} |
| 1394 |
|
| 1395 |
a = Apprise(asset=asset) |
| 1396 |
a.add(s_devops) |
| 1397 |
a.add(s_mgmt) |
| 1398 |
|
| 1399 |
|
| 1400 |
cli_tag = [["devops:3"], ["management:2"]] |
| 1401 |
result = a.notify(body="test", tag=cli_tag) |
| 1402 |
assert result is True |
| 1403 |
assert s_devops._calls == 4 |
| 1404 |
assert s_mgmt._calls == 3 |
| 1405 |
finally: |
| 1406 |
N_MGR.unload_modules() |
| 1407 |
|
| 1408 |
def test_cli_and_format_shared_chain(self): |
| 1409 |
"""CLI --tag 'devops, management' generates [['devops', 'management']] |
| 1410 |
-- a multi-element inner list that is an AND condition and uses a |
| 1411 |
single shared escalation chain.""" |
| 1412 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1413 |
|
| 1414 |
try: |
| 1415 |
asset = AppriseAsset(async_mode=False) |
| 1416 |
|
| 1417 |
|
| 1418 |
s_both = _FailThenSucceedNotify( |
| 1419 |
host="both", asset=asset, fail_times=0 |
| 1420 |
) |
| 1421 |
s_both.tags = {"devops", "management"} |
| 1422 |
|
| 1423 |
|
| 1424 |
s_devonly = _FailThenSucceedNotify( |
| 1425 |
host="devonly", asset=asset, fail_times=0 |
| 1426 |
) |
| 1427 |
s_devonly.tags = {"devops"} |
| 1428 |
|
| 1429 |
a = Apprise(asset=asset) |
| 1430 |
a.add(s_both) |
| 1431 |
a.add(s_devonly) |
| 1432 |
|
| 1433 |
|
| 1434 |
cli_tag = [["devops", "management"]] |
| 1435 |
result = a.notify(body="test", tag=cli_tag) |
| 1436 |
assert result is True |
| 1437 |
assert s_both._calls == 1 |
| 1438 |
assert s_devonly._calls == 0 |
| 1439 |
finally: |
| 1440 |
N_MGR.unload_modules() |
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
def test_and_filter_explicit_priority_no_match_returns_none(self): |
| 1447 |
"""AND filter with an explicit priority that no server satisfies |
| 1448 |
returns None (no services dispatched at all).""" |
| 1449 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1450 |
|
| 1451 |
try: |
| 1452 |
asset = AppriseAsset(async_mode=False) |
| 1453 |
|
| 1454 |
|
| 1455 |
s = _FailThenSucceedNotify(host="s", asset=asset, fail_times=0) |
| 1456 |
s.tags = {AppriseTag("alerts", priority=2, has_priority=True)} |
| 1457 |
|
| 1458 |
a = Apprise(asset=asset) |
| 1459 |
a.add(s) |
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
result = a.notify(body="test", tag=[["1:alerts", "backup"]]) |
| 1464 |
assert result is None |
| 1465 |
assert s._calls == 0 |
| 1466 |
finally: |
| 1467 |
N_MGR.unload_modules() |
| 1468 |
|
| 1469 |
def test_three_or_chains_all_succeed(self): |
| 1470 |
"""Three independent OR chains each succeed at their first priority |
| 1471 |
group; notify() returns True and each service is called once.""" |
| 1472 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1473 |
|
| 1474 |
try: |
| 1475 |
asset = AppriseAsset(async_mode=False) |
| 1476 |
|
| 1477 |
s_a = _FailThenSucceedNotify(host="a", asset=asset, fail_times=0) |
| 1478 |
s_a.tags = {"alpha"} |
| 1479 |
|
| 1480 |
s_b = _FailThenSucceedNotify(host="b", asset=asset, fail_times=0) |
| 1481 |
s_b.tags = {"beta"} |
| 1482 |
|
| 1483 |
s_c = _FailThenSucceedNotify(host="c", asset=asset, fail_times=0) |
| 1484 |
s_c.tags = {"gamma"} |
| 1485 |
|
| 1486 |
a = Apprise(asset=asset) |
| 1487 |
a.add(s_a) |
| 1488 |
a.add(s_b) |
| 1489 |
a.add(s_c) |
| 1490 |
|
| 1491 |
result = a.notify(body="test", tag=["alpha", "beta", "gamma"]) |
| 1492 |
assert result is True |
| 1493 |
assert s_a._calls == 1 |
| 1494 |
assert s_b._calls == 1 |
| 1495 |
assert s_c._calls == 1 |
| 1496 |
finally: |
| 1497 |
N_MGR.unload_modules() |
| 1498 |
|
| 1499 |
def test_three_or_chains_one_fails_overall_false(self): |
| 1500 |
"""Three OR chains: two succeed, one exhausts all priority groups. |
| 1501 |
notify() returns False even though the other chains succeeded.""" |
| 1502 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1503 |
|
| 1504 |
try: |
| 1505 |
asset = AppriseAsset(async_mode=False) |
| 1506 |
|
| 1507 |
s_a = _FailThenSucceedNotify(host="a", asset=asset, fail_times=0) |
| 1508 |
s_a.tags = {"alpha"} |
| 1509 |
|
| 1510 |
|
| 1511 |
s_b = _FailThenSucceedNotify(host="b", asset=asset, fail_times=99) |
| 1512 |
s_b.tags = {"beta"} |
| 1513 |
|
| 1514 |
s_c = _FailThenSucceedNotify(host="c", asset=asset, fail_times=0) |
| 1515 |
s_c.tags = {"gamma"} |
| 1516 |
|
| 1517 |
a = Apprise(asset=asset) |
| 1518 |
a.add(s_a) |
| 1519 |
a.add(s_b) |
| 1520 |
a.add(s_c) |
| 1521 |
|
| 1522 |
result = a.notify(body="test", tag=["alpha", "beta", "gamma"]) |
| 1523 |
assert result is False |
| 1524 |
assert s_a._calls == 1 |
| 1525 |
assert s_c._calls == 1 |
| 1526 |
finally: |
| 1527 |
N_MGR.unload_modules() |
| 1528 |
|
| 1529 |
def test_chain_escalation_to_second_priority_group(self): |
| 1530 |
"""A chain whose priority-0 group fails escalates to the priority-1 |
| 1531 |
group; the second group succeeds so notify() returns True.""" |
| 1532 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1533 |
|
| 1534 |
try: |
| 1535 |
asset = AppriseAsset(async_mode=False) |
| 1536 |
|
| 1537 |
|
| 1538 |
s_p0 = _FailThenSucceedNotify( |
| 1539 |
host="p0", asset=asset, fail_times=99 |
| 1540 |
) |
| 1541 |
s_p0.tags = {AppriseTag("alerts", priority=0, has_priority=False)} |
| 1542 |
|
| 1543 |
|
| 1544 |
s_p1 = _FailThenSucceedNotify(host="p1", asset=asset, fail_times=0) |
| 1545 |
s_p1.tags = {AppriseTag("alerts", priority=1, has_priority=True)} |
| 1546 |
|
| 1547 |
a = Apprise(asset=asset) |
| 1548 |
a.add(s_p0) |
| 1549 |
a.add(s_p1) |
| 1550 |
|
| 1551 |
result = a.notify(body="test", tag="alerts") |
| 1552 |
assert result is True |
| 1553 |
|
| 1554 |
assert s_p0._calls == 1 |
| 1555 |
|
| 1556 |
assert s_p1._calls == 1 |
| 1557 |
finally: |
| 1558 |
N_MGR.unload_modules() |
| 1559 |
|
| 1560 |
def test_all_priority_groups_exhausted_returns_false(self): |
| 1561 |
"""When every priority group in a chain fails, notify() returns False |
| 1562 |
and all groups were attempted in order.""" |
| 1563 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1564 |
|
| 1565 |
try: |
| 1566 |
asset = AppriseAsset(async_mode=False) |
| 1567 |
|
| 1568 |
|
| 1569 |
s_p0 = _FailThenSucceedNotify( |
| 1570 |
host="p0", asset=asset, fail_times=99 |
| 1571 |
) |
| 1572 |
s_p0.tags = {AppriseTag("alerts", priority=0, has_priority=False)} |
| 1573 |
|
| 1574 |
|
| 1575 |
s_p1 = _FailThenSucceedNotify( |
| 1576 |
host="p1", asset=asset, fail_times=99 |
| 1577 |
) |
| 1578 |
s_p1.tags = {AppriseTag("alerts", priority=1, has_priority=True)} |
| 1579 |
|
| 1580 |
a = Apprise(asset=asset) |
| 1581 |
a.add(s_p0) |
| 1582 |
a.add(s_p1) |
| 1583 |
|
| 1584 |
result = a.notify(body="test", tag="alerts") |
| 1585 |
assert result is False |
| 1586 |
|
| 1587 |
assert s_p0._calls == 1 |
| 1588 |
assert s_p1._calls == 1 |
| 1589 |
finally: |
| 1590 |
N_MGR.unload_modules() |
| 1591 |
|
| 1592 |
def test_chain_dispatch_future_exception_treated_as_failure(self): |
| 1593 |
"""An exception escaping _run_batch is caught in the chain-dispatch |
| 1594 |
ThreadPoolExecutor loop and treated as a delivery failure. |
| 1595 |
|
| 1596 |
Covers the 'except Exception: ok = False' branch (apprise.py:788-792) |
| 1597 |
that is only reachable when multiple chains are active simultaneously |
| 1598 |
(triggering the ThreadPoolExecutor path) and _run_batch raises. |
| 1599 |
Uses _ExplodingAsset (raises on async_mode access) instead of |
| 1600 |
patching a @staticmethod, which is unreliable across Python versions |
| 1601 |
when the patched method runs inside a ThreadPoolExecutor worker. |
| 1602 |
""" |
| 1603 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1604 |
|
| 1605 |
try: |
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
class _ExplodingAsset(AppriseAsset): |
| 1610 |
@property |
| 1611 |
def async_mode(self): |
| 1612 |
raise RuntimeError("injected async_mode access") |
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
s_a = _FailThenSucceedNotify( |
| 1617 |
host="a", asset=_ExplodingAsset(), fail_times=0 |
| 1618 |
) |
| 1619 |
s_a.tags = {"alpha"} |
| 1620 |
|
| 1621 |
s_b = _FailThenSucceedNotify( |
| 1622 |
host="b", asset=_ExplodingAsset(), fail_times=0 |
| 1623 |
) |
| 1624 |
s_b.tags = {"beta"} |
| 1625 |
|
| 1626 |
a = Apprise() |
| 1627 |
a.add(s_a) |
| 1628 |
a.add(s_b) |
| 1629 |
|
| 1630 |
result = a.notify(body="test", tag=["alpha", "beta"]) |
| 1631 |
|
| 1632 |
|
| 1633 |
assert result is False |
| 1634 |
finally: |
| 1635 |
N_MGR.unload_modules() |
| 1636 |
|
| 1637 |
|
| 1638 |
class TestAbortOnChainFailure: |
| 1639 |
"""AppriseAsset.abort_on_chain_failure controls early-abort behaviour. |
| 1640 |
|
| 1641 |
When False (default): all chains are allowed to complete even if one |
| 1642 |
has already failed, so every configured URL gets at least one attempt. |
| 1643 |
When True: as soon as any chain exhausts all its priority groups without |
| 1644 |
success, notify() returns False immediately without running further |
| 1645 |
escalation rounds for the other chains. |
| 1646 |
""" |
| 1647 |
|
| 1648 |
def _make_tagged(self, tag_str, asset, fail_times=0): |
| 1649 |
s = _FailThenSucceedNotify( |
| 1650 |
host=tag_str, asset=asset, fail_times=fail_times |
| 1651 |
) |
| 1652 |
s.tags = {tag_str} |
| 1653 |
return s |
| 1654 |
|
| 1655 |
def test_default_false_all_chains_complete(self): |
| 1656 |
"""With abort_on_chain_failure=False (default), all chains run to |
| 1657 |
completion even if one chain has already failed.""" |
| 1658 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1659 |
|
| 1660 |
try: |
| 1661 |
asset = AppriseAsset(async_mode=False) |
| 1662 |
|
| 1663 |
|
| 1664 |
s_a = self._make_tagged("alpha", asset, fail_times=0) |
| 1665 |
|
| 1666 |
|
| 1667 |
s_b = self._make_tagged("beta", asset, fail_times=99) |
| 1668 |
|
| 1669 |
|
| 1670 |
s_c = self._make_tagged("gamma", asset, fail_times=0) |
| 1671 |
|
| 1672 |
a = Apprise(asset=asset) |
| 1673 |
a.add(s_a) |
| 1674 |
a.add(s_b) |
| 1675 |
a.add(s_c) |
| 1676 |
|
| 1677 |
result = a.notify(body="test", tag=["alpha", "beta", "gamma"]) |
| 1678 |
assert result is False |
| 1679 |
|
| 1680 |
assert s_a._calls == 1 |
| 1681 |
assert s_b._calls == 1 |
| 1682 |
assert s_c._calls == 1 |
| 1683 |
finally: |
| 1684 |
N_MGR.unload_modules() |
| 1685 |
|
| 1686 |
def test_abort_on_chain_failure_true_stops_early(self): |
| 1687 |
"""With abort_on_chain_failure=True, once beta exhausts all priority |
| 1688 |
groups without success, further escalation rounds for other chains |
| 1689 |
are skipped.""" |
| 1690 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1691 |
|
| 1692 |
try: |
| 1693 |
asset = AppriseAsset(async_mode=False, abort_on_chain_failure=True) |
| 1694 |
|
| 1695 |
|
| 1696 |
s_a_p0 = _FailThenSucceedNotify( |
| 1697 |
host="a0", asset=asset, fail_times=99 |
| 1698 |
) |
| 1699 |
s_a_p0.tags = {AppriseTag("alpha", priority=0, has_priority=False)} |
| 1700 |
|
| 1701 |
s_a_p1 = _FailThenSucceedNotify( |
| 1702 |
host="a1", asset=asset, fail_times=0 |
| 1703 |
) |
| 1704 |
s_a_p1.tags = {AppriseTag("alpha", priority=1, has_priority=True)} |
| 1705 |
|
| 1706 |
|
| 1707 |
s_b = _FailThenSucceedNotify(host="b", asset=asset, fail_times=99) |
| 1708 |
s_b.tags = {"beta"} |
| 1709 |
|
| 1710 |
a = Apprise(asset=asset) |
| 1711 |
a.add(s_a_p0) |
| 1712 |
a.add(s_a_p1) |
| 1713 |
a.add(s_b) |
| 1714 |
|
| 1715 |
result = a.notify(body="test", tag=["alpha", "beta"]) |
| 1716 |
assert result is False |
| 1717 |
|
| 1718 |
|
| 1719 |
assert s_b._calls == 1 |
| 1720 |
assert s_a_p0._calls == 1 |
| 1721 |
assert s_a_p1._calls == 0 |
| 1722 |
finally: |
| 1723 |
N_MGR.unload_modules() |
| 1724 |
|
| 1725 |
def test_abort_on_chain_failure_true_async(self): |
| 1726 |
"""abort_on_chain_failure=True triggers the same early-abort in |
| 1727 |
async_notify()""" |
| 1728 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1729 |
|
| 1730 |
try: |
| 1731 |
asset = AppriseAsset(async_mode=True, abort_on_chain_failure=True) |
| 1732 |
|
| 1733 |
|
| 1734 |
s_a_p0 = _FailThenSucceedNotify( |
| 1735 |
host="a0", asset=asset, fail_times=99 |
| 1736 |
) |
| 1737 |
s_a_p0.tags = {AppriseTag("alpha", priority=0, has_priority=False)} |
| 1738 |
|
| 1739 |
s_a_p1 = _FailThenSucceedNotify( |
| 1740 |
host="a1", asset=asset, fail_times=0 |
| 1741 |
) |
| 1742 |
s_a_p1.tags = {AppriseTag("alpha", priority=1, has_priority=True)} |
| 1743 |
|
| 1744 |
|
| 1745 |
s_b = _FailThenSucceedNotify(host="b", asset=asset, fail_times=99) |
| 1746 |
s_b.tags = {"beta"} |
| 1747 |
|
| 1748 |
a = Apprise(asset=asset) |
| 1749 |
a.add(s_a_p0) |
| 1750 |
a.add(s_a_p1) |
| 1751 |
a.add(s_b) |
| 1752 |
|
| 1753 |
result = asyncio.run( |
| 1754 |
a.async_notify(body="test", tag=["alpha", "beta"]) |
| 1755 |
) |
| 1756 |
assert result is False |
| 1757 |
|
| 1758 |
assert s_b._calls == 1 |
| 1759 |
assert s_a_p0._calls == 1 |
| 1760 |
assert s_a_p1._calls == 0 |
| 1761 |
finally: |
| 1762 |
N_MGR.unload_modules() |
| 1763 |
|
| 1764 |
|
| 1765 |
class TestOptionalService: |
| 1766 |
"""optional=True silently absorbs per-service delivery failures. |
| 1767 |
|
| 1768 |
The overall notify()/async_notify() result is True even when an |
| 1769 |
optional service cannot be reached, so callers are not penalised |
| 1770 |
for "nice to have" endpoints (e.g. home screens, debug logging). |
| 1771 |
""" |
| 1772 |
|
| 1773 |
def test_optional_default_false(self): |
| 1774 |
"""optional defaults to False on a freshly constructed plugin.""" |
| 1775 |
nb = _TestNotify(host="localhost") |
| 1776 |
assert nb.optional is False |
| 1777 |
|
| 1778 |
def test_optional_url_includes_flag(self): |
| 1779 |
"""url_parameters() always emits optional=yes or optional=no.""" |
| 1780 |
nb = _TestNotify(host="localhost") |
| 1781 |
params = nb.url_parameters() |
| 1782 |
assert "optional" in params |
| 1783 |
assert params["optional"] == "no" |
| 1784 |
|
| 1785 |
nb_opt = _TestNotify(host="localhost", optional=True) |
| 1786 |
params_opt = nb_opt.url_parameters() |
| 1787 |
assert params_opt["optional"] == "yes" |
| 1788 |
|
| 1789 |
def test_optional_parse_url_yes(self): |
| 1790 |
"""parse_url propagates optional=yes into the results dict.""" |
| 1791 |
results = _TestNotify.parse_url("test://localhost?optional=yes") |
| 1792 |
assert results is not None |
| 1793 |
assert results.get("optional") is True |
| 1794 |
|
| 1795 |
def test_optional_parse_url_no(self): |
| 1796 |
"""parse_url propagates optional=no into the results dict.""" |
| 1797 |
results = _TestNotify.parse_url("test://localhost?optional=no") |
| 1798 |
assert results is not None |
| 1799 |
assert results.get("optional") is False |
| 1800 |
|
| 1801 |
def test_optional_kwarg_sets_attribute(self): |
| 1802 |
"""Passing optional=True/False to __init__ sets .optional.""" |
| 1803 |
nb_yes = _TestNotify(host="localhost", optional=True) |
| 1804 |
assert nb_yes.optional is True |
| 1805 |
|
| 1806 |
nb_no = _TestNotify(host="localhost", optional=False) |
| 1807 |
assert nb_no.optional is False |
| 1808 |
|
| 1809 |
def test_optional_sequential_failure_absorbed(self): |
| 1810 |
"""Sequential dispatch: failed optional server -> overall True.""" |
| 1811 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1812 |
|
| 1813 |
try: |
| 1814 |
asset = AppriseAsset(async_mode=False) |
| 1815 |
s = _FailThenSucceedNotify(host="s1", asset=asset, fail_times=99) |
| 1816 |
s.optional = True |
| 1817 |
|
| 1818 |
a = Apprise(asset=asset) |
| 1819 |
a.add(s) |
| 1820 |
|
| 1821 |
result = a.notify(body="test") |
| 1822 |
assert result is True |
| 1823 |
assert s._calls == 1 |
| 1824 |
finally: |
| 1825 |
N_MGR.unload_modules() |
| 1826 |
|
| 1827 |
def test_optional_threadpool_failure_absorbed(self): |
| 1828 |
"""Thread-pool dispatch: failed optional servers -> overall True. |
| 1829 |
|
| 1830 |
Two async_mode=True servers ensure _notify_parallel_threadpool |
| 1831 |
uses the actual thread pool (n_calls==1 falls back to sequential). |
| 1832 |
""" |
| 1833 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1834 |
|
| 1835 |
try: |
| 1836 |
asset = AppriseAsset(async_mode=True) |
| 1837 |
s1 = _FailThenSucceedNotify(host="s1", asset=asset, fail_times=99) |
| 1838 |
s1.optional = True |
| 1839 |
s2 = _FailThenSucceedNotify(host="s2", asset=asset, fail_times=99) |
| 1840 |
s2.optional = True |
| 1841 |
|
| 1842 |
a = Apprise(asset=asset) |
| 1843 |
a.add(s1) |
| 1844 |
a.add(s2) |
| 1845 |
|
| 1846 |
result = a.notify(body="test") |
| 1847 |
assert result is True |
| 1848 |
finally: |
| 1849 |
N_MGR.unload_modules() |
| 1850 |
|
| 1851 |
def test_optional_asyncio_failure_absorbed(self): |
| 1852 |
"""Asyncio dispatch: failed optional server -> overall True.""" |
| 1853 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1854 |
|
| 1855 |
try: |
| 1856 |
asset = AppriseAsset(async_mode=True) |
| 1857 |
s = _FailThenSucceedNotify(host="s1", asset=asset, fail_times=99) |
| 1858 |
s.optional = True |
| 1859 |
|
| 1860 |
a = Apprise(asset=asset) |
| 1861 |
a.add(s) |
| 1862 |
|
| 1863 |
result = asyncio.run(a.async_notify(body="test")) |
| 1864 |
assert result is True |
| 1865 |
finally: |
| 1866 |
N_MGR.unload_modules() |
| 1867 |
|
| 1868 |
def test_optional_all_fail_all_optional_returns_true(self): |
| 1869 |
"""When every server is optional and every server fails -> True.""" |
| 1870 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1871 |
|
| 1872 |
try: |
| 1873 |
asset = AppriseAsset(async_mode=False) |
| 1874 |
a = Apprise(asset=asset) |
| 1875 |
for i in range(3): |
| 1876 |
s = _FailThenSucceedNotify( |
| 1877 |
host=f"s{i}", asset=asset, fail_times=99 |
| 1878 |
) |
| 1879 |
s.optional = True |
| 1880 |
a.add(s) |
| 1881 |
|
| 1882 |
result = a.notify(body="test") |
| 1883 |
assert result is True |
| 1884 |
finally: |
| 1885 |
N_MGR.unload_modules() |
| 1886 |
|
| 1887 |
def test_optional_mixed_required_fails_returns_false(self): |
| 1888 |
"""Required server failure taints result even with optional peers.""" |
| 1889 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1890 |
|
| 1891 |
try: |
| 1892 |
asset = AppriseAsset(async_mode=False) |
| 1893 |
s_opt = _FailThenSucceedNotify( |
| 1894 |
host="opt", asset=asset, fail_times=99 |
| 1895 |
) |
| 1896 |
s_opt.optional = True |
| 1897 |
|
| 1898 |
s_req = _FailThenSucceedNotify( |
| 1899 |
host="req", asset=asset, fail_times=99 |
| 1900 |
) |
| 1901 |
|
| 1902 |
|
| 1903 |
a = Apprise(asset=asset) |
| 1904 |
a.add(s_opt) |
| 1905 |
a.add(s_req) |
| 1906 |
|
| 1907 |
result = a.notify(body="test") |
| 1908 |
assert result is False |
| 1909 |
finally: |
| 1910 |
N_MGR.unload_modules() |
| 1911 |
|
| 1912 |
def test_optional_success_unaffected(self): |
| 1913 |
"""An optional server that succeeds is still counted as True.""" |
| 1914 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1915 |
|
| 1916 |
try: |
| 1917 |
asset = AppriseAsset(async_mode=False) |
| 1918 |
s = _FailThenSucceedNotify(host="s1", asset=asset, fail_times=0) |
| 1919 |
s.optional = True |
| 1920 |
|
| 1921 |
a = Apprise(asset=asset) |
| 1922 |
a.add(s) |
| 1923 |
|
| 1924 |
result = a.notify(body="test") |
| 1925 |
assert result is True |
| 1926 |
assert s._calls == 1 |
| 1927 |
finally: |
| 1928 |
N_MGR.unload_modules() |
| 1929 |
|
| 1930 |
|
| 1931 |
class TestCreateNotifyCalls: |
| 1932 |
"""Cover Apprise._create_notify_calls() which splits servers into |
| 1933 |
sequential and parallel buckets based on their asset.async_mode flag.""" |
| 1934 |
|
| 1935 |
def test_splits_sequential_and_parallel(self): |
| 1936 |
"""Sequential and parallel servers are split into separate lists.""" |
| 1937 |
N_MGR["failpass"] = _FailThenSucceedNotify |
| 1938 |
try: |
| 1939 |
|
| 1940 |
|
| 1941 |
asset_seq = AppriseAsset(async_mode=False) |
| 1942 |
s_seq = _FailThenSucceedNotify( |
| 1943 |
host="s1", asset=asset_seq, fail_times=0 |
| 1944 |
) |
| 1945 |
|
| 1946 |
a = Apprise(asset=asset_seq) |
| 1947 |
a.add(s_seq) |
| 1948 |
|
| 1949 |
seq, par = a._create_notify_calls(body="test") |
| 1950 |
assert len(seq) == 1 |
| 1951 |
assert len(par) == 0 |
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
asset_par = AppriseAsset(async_mode=True) |
| 1956 |
s_par = _FailThenSucceedNotify( |
| 1957 |
host="s2", asset=asset_par, fail_times=0 |
| 1958 |
) |
| 1959 |
|
| 1960 |
b = Apprise(asset=asset_par) |
| 1961 |
b.add(s_par) |
| 1962 |
|
| 1963 |
seq2, par2 = b._create_notify_calls(body="test") |
| 1964 |
assert len(seq2) == 0 |
| 1965 |
assert len(par2) == 1 |
| 1966 |
finally: |
| 1967 |
N_MGR.unload_modules() |
| 1968 |
|
| 1969 |
|
| 1970 |
class TestAsyncioSafetyNet: |
| 1971 |
"""Cover the safety-net branch in _notify_parallel_asyncio that handles |
| 1972 |
exceptions which escape do_call's own try/except block.""" |
| 1973 |
|
| 1974 |
def test_exception_outside_try_block_triggers_safety_net(self): |
| 1975 |
"""An exception raised before the per-attempt try block (e.g. from |
| 1976 |
a misbehaving server.retry property) escapes do_call, is captured by |
| 1977 |
asyncio.gather(return_exceptions=True), and causes the batch to |
| 1978 |
return False via the safety-net handler. |
| 1979 |
|
| 1980 |
A second well-behaved server is included so that results contains |
| 1981 |
both an Exception and a bool value, exercising both branches of the |
| 1982 |
inner ``if isinstance(status, Exception)`` check in the loop.""" |
| 1983 |
|
| 1984 |
class _RaisingRetryServer: |
| 1985 |
"""Server whose retry property raises to simulate a broken plugin |
| 1986 |
that fails before the per-attempt try/except in do_call.""" |
| 1987 |
|
| 1988 |
service_name = "raiser" |
| 1989 |
asset = AppriseAsset(async_mode=True) |
| 1990 |
optional = False |
| 1991 |
|
| 1992 |
@property |
| 1993 |
def retry(self): |
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
raise RuntimeError("injected: property raised outside try") |
| 1999 |
|
| 2000 |
async def async_notify(self, **kwargs): |
| 2001 |
return True |
| 2002 |
|
| 2003 |
class _GoodServer: |
| 2004 |
"""Minimal well-behaved server that succeeds immediately.""" |
| 2005 |
|
| 2006 |
service_name = "good" |
| 2007 |
|
| 2008 |
async def async_notify(self, **kwargs): |
| 2009 |
return True |
| 2010 |
|
| 2011 |
raiser = _RaisingRetryServer() |
| 2012 |
good = _GoodServer() |
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
result = asyncio.run( |
| 2017 |
Apprise._notify_parallel_asyncio((raiser, {}), (good, {})) |
| 2018 |
) |
| 2019 |
assert result is False |