commit c0b9626242ba7627286da6512a8522f519d76556 Author: Arturo Filastò arturo@filasto.net Date: Tue Oct 11 18:01:58 2016 +0200
Change to scheduler and deck_store to better support unittesting --- ooni/agent/scheduler.py | 11 ++++++++--- ooni/deck/store.py | 7 ++++--- ooni/utils/__init__.py | 11 ++++++++++- 3 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/ooni/agent/scheduler.py b/ooni/agent/scheduler.py index 3e93d25..bda8510 100644 --- a/ooni/agent/scheduler.py +++ b/ooni/agent/scheduler.py @@ -5,7 +5,7 @@ from hashlib import md5 from datetime import datetime
from twisted.application import service -from twisted.internet import defer +from twisted.internet import defer, reactor from twisted.internet.task import LoopingCall from twisted.python.filepath import FilePath
@@ -71,7 +71,9 @@ class ScheduledTask(object): identifier = None
def __init__(self, schedule=None, identifier=None, - scheduler_directory=config.scheduler_directory): + scheduler_directory=None): + if scheduler_directory is None: + scheduler_directory = config.scheduler_directory if schedule is not None: self.schedule = schedule if identifier is not None: @@ -346,11 +348,14 @@ class SchedulerService(service.MultiService): """ This service is responsible for running the periodic tasks. """ - def __init__(self, director, interval=30): + def __init__(self, director, interval=30, _reactor=reactor): service.MultiService.__init__(self) self.director = director self.interval = interval + self._looping_call = LoopingCall(self._should_run) + self._looping_call.clock = _reactor + self._scheduled_tasks = []
def schedule(self, task): diff --git a/ooni/deck/store.py b/ooni/deck/store.py index 1e4785e..8fdb856 100644 --- a/ooni/deck/store.py +++ b/ooni/deck/store.py @@ -130,9 +130,10 @@ class InputStore(object):
class DeckStore(object): - def __init__(self): - self.enabled_directory = FilePath(config.decks_enabled_directory) - self.available_directory = FilePath(config.decks_available_directory) + def __init__(self, enabled_directory=config.decks_enabled_directory, + available_directory=config.decks_available_directory): + self.enabled_directory = FilePath(enabled_directory) + self.available_directory = FilePath(available_directory) self._cache = {} self._cache_stale = True
diff --git a/ooni/utils/__init__.py b/ooni/utils/__init__.py index 99f1985..def533a 100644 --- a/ooni/utils/__init__.py +++ b/ooni/utils/__init__.py @@ -6,7 +6,7 @@ import errno import gzip import os
-from datetime import datetime +from datetime import datetime, timedelta from zipfile import ZipFile
from twisted.python.filepath import FilePath @@ -91,6 +91,15 @@ def randomStr(length, num=True): chars += string.digits return ''.join(random.choice(chars) for x in range(length))
+def randomDate(start, end): + """ + From: http://stackoverflow.com/a/553448 + """ + delta = end - start + int_delta = (delta.days * 24 * 60 * 60) + random_second = random.randrange(int_delta) + return start + timedelta(seconds=random_second) + LONG_DATE = "%Y-%m-%d %H:%M:%S" SHORT_DATE = "%Y%m%dT%H%M%SZ"
tor-commits@lists.torproject.org