2021-02-22 20:16:35 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-10-13 23:08:01 +02:00
|
|
|
# BSD 2-Clause License
|
2021-02-22 20:16:35 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# Apprise - Push Notification Library.
|
2024-01-27 21:35:11 +01:00
|
|
|
# Copyright (c) 2024, Chris Caron <lead2gold@gmail.com>
|
2021-02-22 20:16:35 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
2021-02-22 20:16:35 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
2021-02-22 20:16:35 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
2021-02-22 20:16:35 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
2021-02-22 20:16:35 +01:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
from apprise import Apprise
|
|
|
|
from apprise import NotifyBase
|
|
|
|
from apprise import NotifyFormat
|
2023-12-27 20:23:13 +01:00
|
|
|
from apprise.NotificationManager import NotificationManager
|
2021-02-22 20:16:35 +01:00
|
|
|
|
|
|
|
# Disable logging for a cleaner testing output
|
|
|
|
import logging
|
|
|
|
logging.disable(logging.CRITICAL)
|
|
|
|
|
2023-12-27 20:23:13 +01:00
|
|
|
# Grant access to our Notification Manager Singleton
|
|
|
|
N_MGR = NotificationManager()
|
|
|
|
|
2021-02-22 20:16:35 +01:00
|
|
|
|
2022-10-08 02:28:36 +02:00
|
|
|
@pytest.mark.skipif(sys.version_info >= (3, 7),
|
2021-02-22 20:16:35 +01:00
|
|
|
reason="Requires Python 3.0 to 3.6")
|
|
|
|
def test_apprise_asyncio_runtime_error():
|
|
|
|
"""
|
|
|
|
API: Apprise() AsyncIO RuntimeError handling
|
|
|
|
|
|
|
|
"""
|
|
|
|
class GoodNotification(NotifyBase):
|
|
|
|
def __init__(self, **kwargs):
|
2022-10-09 16:00:24 +02:00
|
|
|
super().__init__(
|
2021-02-22 20:16:35 +01:00
|
|
|
notify_format=NotifyFormat.HTML, **kwargs)
|
|
|
|
|
2021-10-06 23:31:34 +02:00
|
|
|
def url(self, **kwargs):
|
2021-02-22 20:16:35 +01:00
|
|
|
# Support URL
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def send(self, **kwargs):
|
|
|
|
# Pretend everything is okay
|
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_url(url, *args, **kwargs):
|
|
|
|
# always parseable
|
|
|
|
return NotifyBase.parse_url(url, verify_host=False)
|
|
|
|
|
|
|
|
# Store our good notification in our schema map
|
2023-12-27 20:23:13 +01:00
|
|
|
N_MGR['good'] = GoodNotification
|
2021-02-22 20:16:35 +01:00
|
|
|
|
|
|
|
# Create ourselves an Apprise object
|
|
|
|
a = Apprise()
|
|
|
|
|
|
|
|
# Add a few entries
|
|
|
|
for _ in range(25):
|
|
|
|
a.add('good://')
|
|
|
|
|
|
|
|
# Python v3.6 and lower can't handle situations gracefully when an
|
|
|
|
# event_loop isn't already established(). Test that Apprise can handle
|
|
|
|
# these situations
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
# Get our event loop
|
2023-02-16 04:50:10 +01:00
|
|
|
try:
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
|
|
|
except RuntimeError:
|
|
|
|
loop = None
|
2021-02-22 20:16:35 +01:00
|
|
|
|
|
|
|
# Adjust out event loop to not point at anything
|
|
|
|
asyncio.set_event_loop(None)
|
|
|
|
|
|
|
|
# With the event loop inactive, we'll fail trying to get the active loop
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
asyncio.get_event_loop()
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Below, we internally will throw a RuntimeError() since there will
|
|
|
|
# be no active event_loop in place. However internally it will be smart
|
|
|
|
# enough to create a new event loop and continue...
|
|
|
|
assert a.notify(title="title", body="body") is True
|
|
|
|
|
|
|
|
finally:
|
|
|
|
# Restore our event loop (in the event the above test failed)
|
|
|
|
asyncio.set_event_loop(loop)
|