Skip to content

Commit

Permalink
Add tests that exercise threading (#806)
Browse files Browse the repository at this point in the history
Threading was the cause of the issue in #779, so adding a test that will
exercise multi-threading.

Co-authored-by: Thomas Perl <[email protected]>
  • Loading branch information
Jamstah and thp authored May 18, 2024
1 parent c60aa6b commit 8af9a1b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format mostly follows [Keep a Changelog](http://keepachangelog.com/en/1.0.0/
- Remove EOL'd Python 3.7 (new minimum requirement is Python 3.8), add Python 3.12 testing
- Adds optional `reply_to` option for email reporters (#794 by trevorshannon)
- Replace the dead dependency `appdirs` with `platformdirs` (#811 by Maxime Werlen, #819 via e-dschungel)
- New concurrency test (#806 by Jamstah)

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions lib/urlwatch/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,8 @@ def load(self, job, guid):

return data, timestamp, None, None

def save(self, job, guid, data, timestamp, etag=None):
# Timestamp and ETag are always ignored
def save(self, job, guid, data, timestamp, tries, etag=None):
# Timestamp, tries and ETag are always ignored
filename = self._get_filename(guid)
with open(filename, 'w+') as fp:
fp.write(data)
Expand Down
13 changes: 11 additions & 2 deletions lib/urlwatch/tests/data/disabled-job.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
name: "1"
url: "|echo job 1"
command: "echo job 1"
enabled: false
---
name: "2"
url: "|echo job 2"
command: "echo job 2"
---
name: "3"
command: "echo job 3"
---
name: "4"
command: "echo job 4"
---
name: "5"
command: "echo job 5"
2 changes: 1 addition & 1 deletion lib/urlwatch/tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def test_disabled_job():
urlwatcher = Urlwatch(urlwatch_config, config_storage, cache_storage, urls_storage)
urlwatcher.run_jobs()

assert len(urlwatcher.report.job_states) == 1
assert len(urlwatcher.report.job_states) == 4
finally:
cache_storage.close()

Expand Down
64 changes: 64 additions & 0 deletions lib/urlwatch/tests/test_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import sys
from glob import glob

from urlwatch.jobs import UrlJob, JobBase, ShellJob
from urlwatch.storage import UrlsYaml, UrlsTxt

import contextlib
import pytest

import tempfile
import os

from urlwatch import storage
from urlwatch.config import CommandConfig
from urlwatch.storage import YamlConfigStorage, CacheMiniDBStorage
from urlwatch.main import Urlwatch
from urlwatch.util import import_module_from_source

root = os.path.join(os.path.dirname(__file__), '..', '..', '..')
here = os.path.dirname(__file__)


class ConfigForTest(CommandConfig):
def __init__(self, config, urls, cache, hooks, verbose):
super().__init__([], 'urlwatch', os.path.dirname(__file__), root, config, urls, hooks, cache, verbose)


@contextlib.contextmanager
def teardown_func():
try:
yield
finally:
"tear down test fixtures"
cache = os.path.join(here, 'data', 'cache.db')
if os.path.exists(cache):
os.remove(cache)


def test_run_watcher():
with teardown_func():
urls = os.path.join(here, 'data', 'disabled-job.yaml')
config = os.path.join(here, 'data', 'urlwatch.yaml')
cache = os.path.join(here, 'data', 'cache.db')
hooks = ''

config_storage = YamlConfigStorage(config)
urls_storage = UrlsYaml(urls)
cache_storage = CacheMiniDBStorage(cache)
try:
urlwatch_config = ConfigForTest(config, urls, cache, hooks, True)

# Prime cache
urlwatcher = Urlwatch(urlwatch_config, config_storage, cache_storage, urls_storage)
urlwatcher.run_jobs()

# Run multiple times with clean report
for _ in range(100):
urlwatcher = Urlwatch(urlwatch_config, config_storage, cache_storage, urls_storage)
urlwatcher.run_jobs()
for job_state in urlwatcher.report.job_states:
assert job_state.exception is None, 'Job failed during threading test'
assert job_state.verb == 'unchanged', f'Job verb was "{job_state.verb}" for unchanged output during threading test'
finally:
cache_storage.close()

0 comments on commit 8af9a1b

Please sign in to comment.