[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [ooni-probe/master] Test and implement TasksWithTimeout



commit 2b4455c659dc4faf0b0085baeb63cf455583ac35
Author: Arturo Filastò <art@xxxxxxxxx>
Date:   Sat Jan 12 21:29:35 2013 +0100

    Test and implement TasksWithTimeout
    
    Do some refactoring of the unittesting logic
---
 ooni/director.py       |    8 ++--
 ooni/managers.py       |    6 +-
 ooni/tasks.py          |   12 ++--
 tests/test_managers.py |  135 ++++++++++++++++++------------------------------
 4 files changed, 64 insertions(+), 97 deletions(-)

diff --git a/ooni/director.py b/ooni/director.py
index cf23269..0707664 100644
--- a/ooni/director.py
+++ b/ooni/director.py
@@ -1,4 +1,4 @@
-from ooni.managers import ReportingEntryManager, MeasurementsManager
+from ooni.managers import ReportingEntryManager, MeasurementManager
 from ooni.nettest import NetTest
 
 class Director(object):
@@ -50,9 +50,9 @@ class Director(object):
 
         self.netTests = []
 
-        self.measurementsManager = MeasurementsManager(manager=self,
+        self.measurementManager = MeasurementManager(manager=self,
                 netTests=self.netTests)
-        self.measurementsManager.director = self
+        self.measurementManager.director = self
 
         self.reportEntryManager = ReportingEntryManager()
         self.reportEntryManager.director = self
@@ -67,7 +67,7 @@ class Director(object):
         net_test = NetTest(net_test_file, inputs, options, report)
         net_test.director = self
 
-        self.measurementsManager.schedule(net_test.generateMeasurements())
+        self.measurementManager.schedule(net_test.generateMeasurements())
 
     def measurementTimedOut(self, measurement):
         """
diff --git a/ooni/managers.py b/ooni/managers.py
index be11473..cee6086 100644
--- a/ooni/managers.py
+++ b/ooni/managers.py
@@ -117,7 +117,7 @@ class TaskManager(object):
     def succeeded(self, result, task):
         raise NotImplemented
 
-class MeasurementsManager(TaskManager):
+class MeasurementManager(TaskManager):
     """
     This is the Measurement Tracker. In here we keep track of active measurements
     and issue new measurements once the active ones have been completed.
@@ -137,10 +137,10 @@ class MeasurementsManager(TaskManager):
     director = None
 
     def succeeded(self, result, measurement):
-        pass
+        self.director.measurementSucceeded(measurement)
 
     def failed(self, failure, measurement):
-        pass
+        self.director.measurementFailed(failure, measurement)
 
 class Report(object):
     reportEntryManager = None
diff --git a/ooni/tasks.py b/ooni/tasks.py
index a53287e..703aec3 100644
--- a/ooni/tasks.py
+++ b/ooni/tasks.py
@@ -1,4 +1,4 @@
-from twisted.internet import defer
+from twisted.internet import defer, reactor
 
 class BaseTask(object):
     _timer = None
@@ -61,15 +61,15 @@ class TaskWithTimeout(BaseTask):
 
     def _succeeded(self, result):
         self._cancelTimer()
-        BaseTask._succeeded(self, result)
+        return BaseTask._succeeded(self, result)
 
     def _failed(self, failure):
         self._cancelTimer()
-        BaseTask._failed(self, failure)
+        return BaseTask._failed(self, failure)
 
-    def _run(self):
-        d = BaseTask._run(self)
+    def start(self):
         self._timer = reactor.callLater(self.timeout, self._timedOut)
+        d = BaseTask.start(self)
         return d
 
     def timedOut(self):
@@ -121,7 +121,7 @@ class ReportEntry(TaskWithTimeout):
     def __init__(self, reporter, measurement):
         self.reporter = reporter
         self.measurement = measurement
-        TimedOutTask.__init__(self)
+        TaskWithTimeout.__init__(self)
 
     def run(self):
         return self.reporter.writeReportEntry(self.measurement)
diff --git a/tests/test_managers.py b/tests/test_managers.py
index 59eba5f..a10933e 100644
--- a/tests/test_managers.py
+++ b/tests/test_managers.py
@@ -2,9 +2,8 @@ from twisted.trial import unittest
 from twisted.python import failure
 from twisted.internet import defer
 
-from ooni.tasks import BaseTask
-from ooni.managers import TaskManager
-
+from ooni.tasks import BaseTask, TaskWithTimeout
+from ooni.managers import TaskManager, MeasurementManager
 
 mockFailure = failure.Failure(Exception('mock'))
 
@@ -23,15 +22,19 @@ class MockFailOnceTask(BaseTask):
         else:
             return defer.fail(mockFailure)
 
+class MockSuccessTaskWithTimeout(TaskWithTimeout):
+    def run(self):
+        return defer.succeed(42)
+
+class MockFailTaskWithTimeout(TaskWithTimeout):
+    def run(self):
+        return defer.fail(mockFailure)
+
 class MockTaskManager(TaskManager):
     def __init__(self):
         self.successes = []
 
     def failed(self, failure, task):
-        # print "TASK"
-        # print task
-        # print "FAILURES (%s)" % task.failures
-        # print failure
         pass
 
     def succeeded(self, result, task):
@@ -39,117 +42,81 @@ class MockTaskManager(TaskManager):
 
 class TestTaskManager(unittest.TestCase):
     def setUp(self):
-        self.taskManager = MockTaskManager()
-        self.taskManager.concurrency = 10
-        self.taskManager.retries = 2
-
-        self.taskManager.start()
+        self.measurementManager = MockTaskManager()
+        self.measurementManager.concurrency = 10
+        self.measurementManager.retries = 2
 
-    def tearDown(self):
-        pass
-
-    def test_schedule_successful_one_task(self):
-        mock_task = MockSuccessTask()
-        self.taskManager.schedule(mock_task)
-
-        @mock_task.done.addCallback
-        def done(res):
-            self.assertEqual(self.taskManager.successes,
-                    [(42, mock_task)])
-        return mock_task.done
+        self.measurementManager.start()
 
-    def test_schedule_failing_one_task(self):
-        mock_task = MockFailTask()
-        self.taskManager.schedule(mock_task)
-
-        @mock_task.done.addCallback
-        def done(failure):
-            self.assertEqual(len(self.taskManager.failures), 3)
-
-            self.assertEqual(failure, (mockFailure, mock_task))
-
-        return mock_task.done
-
-    def test_schedule_successful_ten_tasks(self):
+    def schedule_failing_tasks(self, task_type, number=1):
         all_done = []
-        for x in range(10):
-            mock_task = MockSuccessTask()
+        for x in range(number):
+            mock_task = task_type()
             all_done.append(mock_task.done)
-            self.taskManager.schedule(mock_task)
-
-        d = defer.DeferredList(all_done)
-        @d.addCallback
-        def done(res):
-            for task_result, task_instance in self.taskManager.successes:
-                self.assertEqual(task_result, 42)
-                self.assertIsInstance(task_instance, MockSuccessTask)
-
-        return d
-
-    def test_schedule_failing_ten_tasks(self):
-        all_done = []
-        for x in range(10):
-            mock_task = MockFailTask()
-            all_done.append(mock_task.done)
-            self.taskManager.schedule(mock_task)
+            self.measurementManager.schedule(mock_task)
 
         d = defer.DeferredList(all_done)
         @d.addCallback
         def done(res):
             # 10*2 because 2 is the number of retries
-            self.assertEqual(len(self.taskManager.failures), 10*3)
-            for task_result, task_instance in self.taskManager.failures:
+            self.assertEqual(len(self.measurementManager.failures), number*3)
+            for task_result, task_instance in self.measurementManager.failures:
                 self.assertEqual(task_result, mockFailure)
-                self.assertIsInstance(task_instance, MockFailTask)
+                self.assertIsInstance(task_instance, task_type)
 
         return d
 
-    def test_schedule_successful_27_tasks(self):
+    def schedule_successful_tasks(self, task_type, number=1):
         all_done = []
-        for x in range(27):
-            mock_task = MockSuccessTask()
+        for x in range(number):
+            mock_task = task_type()
             all_done.append(mock_task.done)
-            self.taskManager.schedule(mock_task)
+            self.measurementManager.schedule(mock_task)
 
         d = defer.DeferredList(all_done)
         @d.addCallback
         def done(res):
-            for task_result, task_instance in self.taskManager.successes:
+            for task_result, task_instance in self.measurementManager.successes:
                 self.assertEqual(task_result, 42)
-                self.assertIsInstance(task_instance, MockSuccessTask)
+                self.assertIsInstance(task_instance, task_type)
 
         return d
 
-    def test_schedule_failing_27_tasks(self):
-        all_done = []
-        for x in range(27):
-            mock_task = MockFailTask()
-            all_done.append(mock_task.done)
-            self.taskManager.schedule(mock_task)
+    def test_schedule_successful_one_task(self):
+        return self.schedule_successful_tasks(MockSuccessTask)
 
-        d = defer.DeferredList(all_done)
-        @d.addCallback
-        def done(res):
-            # 10*2 because 2 is the number of retries
-            self.assertEqual(len(self.taskManager.failures), 27*3)
-            for task_result, task_instance in self.taskManager.failures:
-                self.assertEqual(task_result, mockFailure)
-                self.assertIsInstance(task_instance, MockFailTask)
+    def test_schedule_successful_one_task_with_timeout(self):
+        return self.schedule_successful_tasks(MockSuccessTaskWithTimeout)
 
-        return d
+    def test_schedule_failing_one_task(self):
+        return self.schedule_failing_tasks(MockFailTask)
+
+    def test_schedule_failing_one_task_with_timeout(self):
+        return self.schedule_failing_tasks(MockFailTaskWithTimeout)
 
+    def test_schedule_successful_ten_tasks(self):
+        return self.schedule_successful_tasks(MockSuccessTask, number=10)
+
+    def test_schedule_failing_ten_tasks(self):
+        return self.schedule_failing_tasks(MockFailTask, number=10)
+
+    def test_schedule_successful_27_tasks(self):
+        return self.schedule_successful_tasks(MockSuccessTask, number=27)
+
+    def test_schedule_failing_27_tasks(self):
+        return self.schedule_failing_tasks(MockFailTask, number=27)
 
     def test_task_retry_and_succeed(self):
         mock_task = MockFailOnceTask()
-        self.taskManager.schedule(mock_task)
+        self.measurementManager.schedule(mock_task)
 
         @mock_task.done.addCallback
         def done(res):
-            self.assertEqual(len(self.taskManager.failures), 1)
+            self.assertEqual(len(self.measurementManager.failures), 1)
 
-            self.assertEqual(self.taskManager.failures,
+            self.assertEqual(self.measurementManager.failures,
                     [(mockFailure, mock_task)])
-            self.assertEqual(self.taskManager.successes,
+            self.assertEqual(self.measurementManager.successes,
                     [(42, mock_task)])
 
         return mock_task.done



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