commit 5723658b20d834dfd02b04264bf3f9e4cd8c9f50
Author: Arturo Filastò <art...@filasto.net>
Date:   Tue Sep 6 16:50:03 2016 +0200

    Add some basic unittests for the ScheduledTasks
---
 ooni/agent/scheduler.py      |  6 +++---
 ooni/settings.py             |  2 --
 ooni/tests/test_scheduler.py | 51 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 5 deletions(-)

diff --git a/ooni/agent/scheduler.py b/ooni/agent/scheduler.py
index 21dd1cd..98b395b 100644
--- a/ooni/agent/scheduler.py
+++ b/ooni/agent/scheduler.py
@@ -53,7 +53,8 @@ class ScheduledTask(object):
     schedule = None
     identifier = None
 
-    def __init__(self, schedule=None, identifier=None):
+    def __init__(self, schedule=None, identifier=None,
+                 scheduler_directory=config.scheduler_directory):
         if schedule is not None:
             self.schedule = schedule
         if identifier is not None:
@@ -61,7 +62,6 @@ class ScheduledTask(object):
 
         assert self.identifier is not None, "self.identifier must be set"
         assert self.schedule is not None, "self.schedule must be set"
-        scheduler_directory = config.scheduler_directory
 
         self._last_run = FilePath(scheduler_directory).child(self.identifier)
         self._last_run_lock = FileSystemlockAndMutex(
@@ -91,7 +91,7 @@ class ScheduledTask(object):
             out_file.write(current_time.strftime(self._time_format))
 
     def task(self):
-        raise NotImplemented
+        raise NotImplementedError
 
     def first_run(self):
         """
diff --git a/ooni/settings.py b/ooni/settings.py
index a1c55fc..409e81e 100644
--- a/ooni/settings.py
+++ b/ooni/settings.py
@@ -3,7 +3,6 @@ import sys
 import yaml
 import errno
 import getpass
-from pkg_resources import parse_version
 from ConfigParser import SafeConfigParser
 
 from twisted.internet import defer, reactor
@@ -11,7 +10,6 @@ from twisted.internet.endpoints import TCP4ClientEndpoint
 
 from os.path import abspath, expanduser
 
-from ooni import __version__ as ooniprobe_version
 from ooni.utils import Storage, log, get_ooni_root
 
 CONFIG_FILE_TEMPLATE = """\
diff --git a/ooni/tests/test_scheduler.py b/ooni/tests/test_scheduler.py
new file mode 100644
index 0000000..cb90a30
--- /dev/null
+++ b/ooni/tests/test_scheduler.py
@@ -0,0 +1,51 @@
+import os
+import shutil
+import tempfile
+
+from twisted.internet import defer
+from twisted.trial import unittest
+
+from ooni.agent.scheduler import ScheduledTask, DidNotRun
+
+class TestScheduler(unittest.TestCase):
+    def test_scheduled_task(self):
+        schedule = "@daily"
+        identifier = "dummy"
+        scheduler_directory = tempfile.mkdtemp()
+        scheduled_task = ScheduledTask(schedule=schedule,
+                                       identifier=identifier,
+                                       scheduler_directory=scheduler_directory
+        )
+        self.assertEqual(scheduled_task.should_run, True)
+        self.assertFailure(scheduled_task.run(), NotImplementedError)
+        self.assertEqual(scheduled_task.should_run, True)
+        shutil.rmtree(scheduler_directory)
+
+    @defer.inlineCallbacks
+    def test_call_twice_scheduled_task(self):
+        """
+        If we call the scheduled task twice in a row the second time it will 
not run.
+        Tests for possible race conditions.
+        """
+        scheduler_directory = tempfile.mkdtemp()
+        spam_path = os.path.join(scheduler_directory, 'spam.txt')
+        class DummyST(ScheduledTask):
+            def task(self):
+                with open(spam_path, 'w') as out_file:
+                    out_file.write("I ran\n")
+
+        schedule = "@daily"
+        identifier = "dummy"
+        dummy_st = DummyST(schedule=schedule,
+                           identifier=identifier,
+                           scheduler_directory=scheduler_directory
+        )
+
+        dummy_st.run()
+        yield self.assertFailure(dummy_st.run(), DidNotRun)
+
+        with open(spam_path) as in_file:
+            self.assertEqual(len(in_file.readlines()), 1)
+
+        self.assertEqual(dummy_st.should_run, False)
+        shutil.rmtree(scheduler_directory)



_______________________________________________
tor-commits mailing list
tor-commits@lists.torproject.org
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits

Reply via email to