mirror of
https://github.com/caronc/apprise.git
synced 2025-08-10 08:47:53 +02:00
Refactored async notification handling (#741)
This commit is contained in:
@ -31,6 +31,7 @@
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
from __future__ import print_function
|
||||
import asyncio
|
||||
import re
|
||||
import sys
|
||||
import pytest
|
||||
@ -56,12 +57,9 @@ from apprise import common
|
||||
from apprise.plugins import __load_matrix
|
||||
from apprise.plugins import __reset_matrix
|
||||
from apprise.utils import parse_list
|
||||
from helpers import OuterEventLoop
|
||||
import inspect
|
||||
|
||||
# Sending notifications requires the coroutines to be awaited, so we need to
|
||||
# wrap the original function when mocking it.
|
||||
import apprise.py3compat.asyncio as py3aio
|
||||
|
||||
# Disable logging for a cleaner testing output
|
||||
import logging
|
||||
logging.disable(logging.CRITICAL)
|
||||
@ -86,10 +84,12 @@ def test_apprise_async():
|
||||
API: Apprise() object asynchronous methods
|
||||
|
||||
"""
|
||||
def do_notify(server, *args, **kwargs):
|
||||
return py3aio.tosync(server.async_notify(*args, **kwargs))
|
||||
with OuterEventLoop() as loop:
|
||||
def do_notify(server, *args, **kwargs):
|
||||
return loop.run_until_complete(
|
||||
server.async_notify(*args, **kwargs))
|
||||
|
||||
apprise_test(do_notify)
|
||||
apprise_test(do_notify)
|
||||
|
||||
|
||||
def apprise_test(do_notify):
|
||||
@ -299,6 +299,10 @@ def apprise_test(do_notify):
|
||||
# Pretend everything is okay
|
||||
raise TypeError()
|
||||
|
||||
async def async_notify(self, **kwargs):
|
||||
# Pretend everything is okay (async)
|
||||
raise TypeError()
|
||||
|
||||
def url(self, **kwargs):
|
||||
# Support URL
|
||||
return ''
|
||||
@ -308,6 +312,10 @@ def apprise_test(do_notify):
|
||||
# Pretend everything is okay
|
||||
raise RuntimeError()
|
||||
|
||||
async def async_notify(self, **kwargs):
|
||||
# Pretend everything is okay (async)
|
||||
raise TypeError()
|
||||
|
||||
def url(self, **kwargs):
|
||||
# Support URL
|
||||
return ''
|
||||
@ -318,6 +326,10 @@ def apprise_test(do_notify):
|
||||
# Pretend everything is okay
|
||||
return False
|
||||
|
||||
async def async_notify(self, **kwargs):
|
||||
# Pretend everything is okay (async)
|
||||
raise TypeError()
|
||||
|
||||
def url(self, **kwargs):
|
||||
# Support URL
|
||||
return ''
|
||||
@ -546,10 +558,12 @@ def test_apprise_tagging_async(mock_post, mock_get):
|
||||
API: Apprise() object tagging functionality asynchronous methods
|
||||
|
||||
"""
|
||||
def do_notify(server, *args, **kwargs):
|
||||
return py3aio.tosync(server.async_notify(*args, **kwargs))
|
||||
with OuterEventLoop() as loop:
|
||||
def do_notify(server, *args, **kwargs):
|
||||
return loop.run_until_complete(
|
||||
server.async_notify(*args, **kwargs))
|
||||
|
||||
apprise_tagging_test(mock_post, mock_get, do_notify)
|
||||
apprise_tagging_test(mock_post, mock_get, do_notify)
|
||||
|
||||
|
||||
def apprise_tagging_test(mock_post, mock_get, do_notify):
|
||||
@ -733,7 +747,10 @@ def test_apprise_notify_formats(tmpdir):
|
||||
# other if/else parts of the code that aren't otherwise called
|
||||
__load_matrix()
|
||||
|
||||
a = Apprise()
|
||||
# Need to set async_mode=False to call notify() instead of async_notify().
|
||||
asset = AppriseAsset(async_mode=False)
|
||||
|
||||
a = Apprise(asset=asset)
|
||||
|
||||
# no items
|
||||
assert len(a) == 0
|
||||
@ -743,7 +760,7 @@ def test_apprise_notify_formats(tmpdir):
|
||||
notify_format = NotifyFormat.TEXT
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__()
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def notify(self, **kwargs):
|
||||
# Pretend everything is okay
|
||||
@ -759,7 +776,7 @@ def test_apprise_notify_formats(tmpdir):
|
||||
notify_format = NotifyFormat.HTML
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__()
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def notify(self, **kwargs):
|
||||
# Pretend everything is okay
|
||||
@ -775,7 +792,7 @@ def test_apprise_notify_formats(tmpdir):
|
||||
notify_format = NotifyFormat.MARKDOWN
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__()
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def notify(self, **kwargs):
|
||||
# Pretend everything is okay
|
||||
@ -1763,8 +1780,8 @@ def test_apprise_details_plugin_verification():
|
||||
|
||||
|
||||
@mock.patch('requests.post')
|
||||
@mock.patch('apprise.py3compat.asyncio.notify', wraps=py3aio.notify)
|
||||
def test_apprise_async_mode(mock_async_notify, mock_post, tmpdir):
|
||||
@mock.patch('asyncio.gather', wraps=asyncio.gather)
|
||||
def test_apprise_async_mode(mock_gather, mock_post, tmpdir):
|
||||
"""
|
||||
API: Apprise() async_mode tests
|
||||
|
||||
@ -1798,8 +1815,8 @@ def test_apprise_async_mode(mock_async_notify, mock_post, tmpdir):
|
||||
assert a.notify("async") is True
|
||||
|
||||
# Verify our async code got executed
|
||||
assert mock_async_notify.call_count == 1
|
||||
mock_async_notify.reset_mock()
|
||||
assert mock_gather.call_count > 0
|
||||
mock_gather.reset_mock()
|
||||
|
||||
# Provide an over-ride now
|
||||
asset = AppriseAsset(async_mode=False)
|
||||
@ -1823,9 +1840,9 @@ def test_apprise_async_mode(mock_async_notify, mock_post, tmpdir):
|
||||
|
||||
# Send Notifications Syncronously
|
||||
assert a.notify("sync") is True
|
||||
# Verify our async code got called
|
||||
assert mock_async_notify.call_count == 1
|
||||
mock_async_notify.reset_mock()
|
||||
# Sequential send doesn't require a gather
|
||||
assert mock_gather.call_count == 0
|
||||
mock_gather.reset_mock()
|
||||
|
||||
# another way of looking a our false set asset configuration
|
||||
assert a[0].asset.async_mode is False
|
||||
@ -1847,8 +1864,8 @@ def test_apprise_async_mode(mock_async_notify, mock_post, tmpdir):
|
||||
assert a.notify("a mixed batch") is True
|
||||
|
||||
# Verify our async code got called
|
||||
assert mock_async_notify.call_count == 1
|
||||
mock_async_notify.reset_mock()
|
||||
assert mock_gather.call_count > 0
|
||||
mock_gather.reset_mock()
|
||||
|
||||
|
||||
def test_notify_matrix_dynamic_importing(tmpdir):
|
||||
|
Reference in New Issue
Block a user