Cron expressions should follow the machine local time, not UTC [closes #173]
parent
71af6e87e0
commit
296458ece3
@ -0,0 +1 @@
|
||||
# Auto-generated __init__.py - do not remove
|
@ -0,0 +1,26 @@
|
||||
import datetime
|
||||
|
||||
from platypush.cron import cron
|
||||
|
||||
from tests.test_cron import tmp_files, tmp_files_ready, \
|
||||
test_timeout, expected_cron_file_content
|
||||
|
||||
# Prepare a cronjob that should start test_timeout/2 seconds from the application start
|
||||
cron_time = datetime.datetime.now() + datetime.timedelta(seconds=test_timeout/2)
|
||||
cron_expr = '{min} {hour} {day} {month} * {sec}'.format(
|
||||
min=cron_time.minute, hour=cron_time.hour, day=cron_time.day,
|
||||
month=cron_time.month, sec=cron_time.second)
|
||||
|
||||
|
||||
@cron(cron_expr)
|
||||
def cron_test(**_):
|
||||
"""
|
||||
Simple cronjob that awaits for ``../test_cron.py`` to be ready and writes the expected
|
||||
content to the monitored temporary file.
|
||||
"""
|
||||
files_ready = tmp_files_ready.wait(timeout=test_timeout)
|
||||
assert files_ready, \
|
||||
'The test did not prepare the temporary files within {} seconds'.format(test_timeout)
|
||||
|
||||
with open(tmp_files[0], 'w') as f:
|
||||
f.write(expected_cron_file_content)
|
@ -0,0 +1,46 @@
|
||||
import os
|
||||
import pytest
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
|
||||
tmp_files = []
|
||||
tmp_files_ready = threading.Event()
|
||||
test_timeout = 10
|
||||
expected_cron_file_content = 'The cronjob ran successfully!'
|
||||
|
||||
|
||||
@pytest.fixture(scope='module', autouse=True)
|
||||
def tmp_file(*_):
|
||||
tmp_file = tempfile.NamedTemporaryFile(prefix='platypush-test-cron-',
|
||||
suffix='.txt', delete=False)
|
||||
tmp_files.append(tmp_file.name)
|
||||
tmp_files_ready.set()
|
||||
yield tmp_file.name
|
||||
|
||||
if os.path.isfile(tmp_files[0]):
|
||||
os.unlink(tmp_files[0])
|
||||
|
||||
|
||||
def test_cron_execution(tmp_file):
|
||||
"""
|
||||
Test that the cronjob in ``../etc/scripts/test_cron.py`` runs successfully.
|
||||
"""
|
||||
actual_cron_file_content = None
|
||||
test_start = time.time()
|
||||
|
||||
while actual_cron_file_content != expected_cron_file_content and \
|
||||
time.time() - test_start < test_timeout:
|
||||
with open(tmp_file, 'r') as f:
|
||||
actual_cron_file_content = f.read()
|
||||
time.sleep(0.5)
|
||||
|
||||
assert actual_cron_file_content == expected_cron_file_content, \
|
||||
'cron_test failed to run within {} seconds'.format(test_timeout)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main()
|
||||
|
||||
|
||||
# vim:sw=4:ts=4:et:
|
Loading…
Reference in new issue