| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
# Disable logging for a cleaner testing output |
| 29 |
import logging |
| 30 |
import sys |
| 31 |
|
| 32 |
import pytest |
| 33 |
|
| 34 |
from apprise import Apprise, NotificationManager, NotifyBase, NotifyFormat |
| 35 |
|
| 36 |
logging.disable(logging.CRITICAL) |
| 37 |
|
| 38 |
# Grant access to our Notification Manager Singleton |
| 39 |
N_MGR = NotificationManager() |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.skipif( |
| 43 |
sys.version_info >= (3, 7), reason="Requires Python 3.0 to 3.6" |
| 44 |
) |
| 45 |
def test_apprise_asyncio_runtime_error(): |
| 46 |
""" |
| 47 |
API: Apprise() AsyncIO RuntimeError handling |
| 48 |
|
| 49 |
""" |
| 50 |
|
| 51 |
class GoodNotification(NotifyBase): |
| 52 |
def __init__(self, **kwargs): |
| 53 |
super().__init__(notify_format=NotifyFormat.HTML, **kwargs) |
| 54 |
|
| 55 |
def url(self, **kwargs): |
| 56 |
# Support URL |
| 57 |
return "" |
| 58 |
|
| 59 |
def send(self, **kwargs): |
| 60 |
# Pretend everything is okay |
| 61 |
return True |
| 62 |
|
| 63 |
@staticmethod |
| 64 |
def parse_url(url, *args, **kwargs): |
| 65 |
# always parseable |
| 66 |
return NotifyBase.parse_url(url, verify_host=False) |
| 67 |
|
| 68 |
# Store our good notification in our schema map |
| 69 |
N_MGR["good"] = GoodNotification |
| 70 |
|
| 71 |
# Create ourselves an Apprise object |
| 72 |
a = Apprise() |
| 73 |
|
| 74 |
# Add a few entries |
| 75 |
for _ in range(25): |
| 76 |
a.add("good://") |
| 77 |
|
| 78 |
# Python v3.6 and lower can't handle situations gracefully when an |
| 79 |
# event_loop isn't already established(). Test that Apprise can handle |
| 80 |
# these situations |
| 81 |
import asyncio |
| 82 |
|
| 83 |
# Get our event loop |
| 84 |
try: |
| 85 |
loop = asyncio.get_event_loop() |
| 86 |
|
| 87 |
except RuntimeError: |
| 88 |
loop = None |
| 89 |
|
| 90 |
# Adjust out event loop to not point at anything |
| 91 |
asyncio.set_event_loop(None) |
| 92 |
|
| 93 |
# With the event loop inactive, we'll fail trying to get the active loop |
| 94 |
with pytest.raises(RuntimeError): |
| 95 |
asyncio.get_event_loop() |
| 96 |
|
| 97 |
try: |
| 98 |
# Below, we internally will throw a RuntimeError() since there will |
| 99 |
# be no active event_loop in place. However internally it will be smart |
| 100 |
# enough to create a new event loop and continue... |
| 101 |
assert bool(a.notify(title="title", body="body")) is True |
| 102 |
|
| 103 |
finally: |
| 104 |
# Restore our event loop (in the event the above test failed) |
| 105 |
asyncio.set_event_loop(loop) |