mirror of
https://github.com/caronc/apprise.git
synced 2025-08-14 18:38:35 +02:00
Asynchronous Dynamic Module Loading Support (#1071)
This commit is contained in:
@ -29,8 +29,10 @@
|
||||
import re
|
||||
import pytest
|
||||
import types
|
||||
import threading
|
||||
from inspect import cleandoc
|
||||
|
||||
from apprise import Apprise
|
||||
from apprise.NotificationManager import NotificationManager
|
||||
from apprise.plugins.NotifyBase import NotifyBase
|
||||
|
||||
@ -248,6 +250,48 @@ def test_notification_manager_module_loading(tmpdir):
|
||||
N_MGR.load_modules()
|
||||
N_MGR.load_modules()
|
||||
|
||||
#
|
||||
# Thread Testing
|
||||
#
|
||||
|
||||
# This tests against a racing condition when the modules have not been
|
||||
# loaded. When multiple instances of Apprise are all instantiated,
|
||||
# the loading of the modules will occur for each instance if detected
|
||||
# having not been previously done, this tests that we can dynamically
|
||||
# support the loading of modules once whe multiple instances to apprise
|
||||
# are instantiated.
|
||||
thread_count = 10
|
||||
|
||||
def thread_test(result, no):
|
||||
"""
|
||||
Load our apprise object with valid URLs and store our result
|
||||
"""
|
||||
apobj = Apprise()
|
||||
result[no] = apobj.add('json://localhost') and \
|
||||
apobj.add('form://localhost') and \
|
||||
apobj.add('xml://localhost')
|
||||
|
||||
# Unload our modules
|
||||
N_MGR.unload_modules()
|
||||
|
||||
# Prepare threads to load
|
||||
results = [None] * thread_count
|
||||
threads = [
|
||||
threading.Thread(target=thread_test, args=(results, no))
|
||||
for no in range(thread_count)
|
||||
]
|
||||
|
||||
# Verify we can safely load our modules in a thread safe environment
|
||||
for t in threads:
|
||||
t.start()
|
||||
|
||||
for t in threads:
|
||||
t.join()
|
||||
|
||||
# Verify we loaded our urls in all threads successfully
|
||||
for result in results:
|
||||
assert result is True
|
||||
|
||||
|
||||
def test_notification_manager_decorators(tmpdir):
|
||||
"""
|
||||
@ -376,6 +420,10 @@ def test_notification_manager_decorators(tmpdir):
|
||||
"""))
|
||||
assert 'mytest' not in N_MGR
|
||||
N_MGR.load_modules(path=str(notify_base))
|
||||
|
||||
# It's still not loaded because the path has already been scanned
|
||||
assert 'mytest' not in N_MGR
|
||||
N_MGR.load_modules(path=str(notify_base), force=True)
|
||||
assert 'mytest' in N_MGR
|
||||
|
||||
# Could not be loaded because the filename did not align with the class
|
||||
@ -387,3 +435,7 @@ def test_notification_manager_decorators(tmpdir):
|
||||
N_MGR.load_modules(path=str(notify_base))
|
||||
# Our item is still loaded as expected
|
||||
assert 'mytest' in N_MGR
|
||||
|
||||
# Simple test to make sure we can handle duplicate entries loaded
|
||||
N_MGR.load_modules(path=str(notify_base), force=True)
|
||||
N_MGR.load_modules(path=str(notify_base), force=True)
|
||||
|
Reference in New Issue
Block a user